PageSourceSearch

https://vdiplomacy.com/javascript/timeHandler.js?ver=1.711

js vdiplomacy.com collected 2026-10-02 14:14:26 UTC 7,672 bytes, 255 lines download raw bytes

1/*
2    Copyright (C) 2004-2010 Kestas J. Kuliukas
3
4	This file is part of webDiplomacy.
5
6    webDiplomacy is free software: you can redistribute it and/or modify
7    it under the terms of the GNU Affero General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10
11    webDiplomacy is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU Affero General Public License
17    along with webDiplomacy.  If not, see <http://www.gnu.org/licenses/>.
18 */
19// See doc/javascript.txt for information on JavaScript in webDiplomacy
20
21/*
22 * Original timer.js written by jayp
23 */
24
25// Update class timestamps to be in the local time, taking the unixtime attribute and converting it via dateToText()
26var updateTimestamps = function () {
27	$$('.timestamp').map(function(c) {
28		var cDate = new Date( parseInt(c.getAttribute("unixtime"))*1000 );
29		c.update( dateToText(cDate) );
30	},this);
31}
32
33// update class timestampGames to be in the local time, taking the unixtime attribute and converting it via dateToText()
34var updateTimestampGames = function () {
35	$$('.timestampGames').map(function(c) {
36		const cDate = new Date( parseInt(c.getAttribute("unixtime"))*1000 );
37		c.update( dateToTextTimestampGames(cDate) );
38	},this);
39}
40
41var dayNames=["Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun"];
42var monthNames=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
43
44// Convert a JavaScript (new Date()) into text
45const dateToText = function (date) {
46	var secondDifference = ((date - (new Date()))/1000);
47	if( secondDifference < 0 ) secondDifference *= -1;
48
49	var a = l_t(dayNames[date.getDay()]);
50	
51	if ( secondDifference < 4*24*60*60 )
52	{
53		var I = date.getHours();
54		var M = date.getMinutes();
55		if( M<10 ) M="0"+M;
56	
57		var p = "AM";
58		if( I >= 12 ) {
59			I -= 12;
60			p = "PM";
61		}
62		
63		if( I==0 ) I="12";
64		
65		// apply leading zero to single digit hour
66		if( I<10 ) I="0"+I.toString();
67
68		if ( secondDifference < 22*60*60 ) // within 22 hours
69			return I+":"+M+" "+p; // HH:MM AM/PM
70		else
71			return a+" "+I+" "+p; // Day HH AM/PM
72	}
73	else
74	{
75		var d = date.getDate();
76		var b = l_t(monthNames[date.getMonth()]);
77		var y = date.getYear();
78		if ( y < 1900 ) y+=1900;
79		
80		if ( secondDifference < 3*7*22*60*60 ) // within 19 days, 6 hours
81			return a+" "+d+" "+b; // Day Day# Month
82		else
83			return d+" "+b+" "+y; // Day# Month Year
84	} 
85}
86
87const dateToTextTimestampGames = function (date)  {
88    var secondDifference = ((date - (new Date()))/1000);
89    if( secondDifference < 0 ) secondDifference *= -1;
90
91    var a = l_t(dayNames[date.getDay()]);
92    var d = date.getDate();
93    var b = l_t(monthNames[date.getMonth()]);
94    var y = date.getYear();
95    if ( y < 1900 ) y+=1900;
96    var I = date.getHours();
97    var M = date.getMinutes();
98    if( M<10 ) M="0"+M;
99
100    var p = "AM";
101    if( I >= 12 ) {
102        I -= 12;
103        p = "PM";
104    }
105    
106    if( I==0 ) I="12";
107    if( I<10 ) I="0"+I.toString();
108    
109    // If the difference is more than a month then show the year 
110    if ( secondDifference > 3*7*30*60*60 ) {
111    	return a+", "+I+":"+M+" "+p +", "+d+" "+b +" " + y; 
112    	// dayOfWeek HH:MM AM/PM, day# month year
113
114    // Otherwise show everything except the year 
115    } else {
116    	return a+", "+I+":"+M+" "+p +", "+d+" "+b; 
117    	// dayOfWeek HH:MM AM/PM, day# month
118    } 
119}
120
121// Update the timezone offset info in the footer, which tells the user which timezone the page's times are in
122function updateUTCOffset() {
123	// Time needed to add to UTC times to get our time
124	var UTCHoursOffset = -1*((new Date).getTimezoneOffset()/60);
125	var sign='+';
126	if( UTCHoursOffset < 0 )
127	{
128		UTCHoursOffset *= -1;
129		sign='-';
130	}
131	
132	var hours = Math.floor(UTCHoursOffset);
133	var minutes = (UTCHoursOffset*60 - hours*60);
134	
135	if( hours < 10 ) hours = '0'+hours.toString();
136	if( minutes < 10 ) minutes = '0'+minutes.toString();
137	
138	var utcE = $("UTCOffset");
139	if( !Object.isUndefined(utcE) && utcE != null )
140		utcE.update('UTC'+sign.toString()+hours.toString()+':'+minutes.toString());
141}
142
143
144// Above are timestamp functions, below are countdown functions:
145
146var timerCheck=false; // Stores the PeriodicalExecutor which updates countdowns
147var timerCheckMinTime=7*24*60*60; // The refresh time for the PeriodicalExecutor
148var newTimerCheckMinTime=7*24*60*60; // The new refresh time, to detect when it has to be restarted at a new rate
149
150// Update countdown timers, needs to be run repeatedly. The first time it is run it will set up future runs
151function updateTimers() {
152	
153	var timeFrom = Math.floor((new Date).getTime() / 1000);
154	
155	$$(".timeremaining").map(function(c) {
156		
157		var givenTime = parseInt(c.getAttribute("unixtime"));
158		var secondsRemaining = givenTime - timeFrom;
159
160		if( secondsRemaining < 300 )
161			c.setStyle({'color': '#f35f64'});
162		
163		c.update(remainingText(secondsRemaining));
164		
165	},this);
166	
167	// If the timer interval has changed update it
168	if( newTimerCheckMinTime != timerCheckMinTime )
169	{
170		timerCheckMinTime = newTimerCheckMinTime;
171		
172		if( typeof timerCheck == "object" )
173			timerCheck.stop();
174		
175		timerCheck = new PeriodicalExecuter(updateTimers, timerCheckMinTime);
176	}
177}
178
179// Update the timer update period, if 1 the countdowns are updated every second. The smallest update period has to be used
180function setMinimumTimerInterval(newInterval) {
181	if( newInterval<1.0 ) newInterval=1;
182	
183	if( newTimerCheckMinTime >= newInterval )
184		newTimerCheckMinTime = newInterval;
185}
186
187// Textual time remaining for a given number of seconds to pass. Also sets the minimum timer interval
188function remainingText(secondsRemaining)
189{
190	if ( secondsRemaining <= 0 ) return l_t('Now');
191
192	var seconds = Math.floor( secondsRemaining % 60);
193	var minutes = Math.floor(( secondsRemaining % (60*60) )/60);
194	var hours = Math.floor( secondsRemaining % (24*60*60)/(60*60) );
195	var days = Math.floor( secondsRemaining /(24*60*60) );
196
197	if ( days > 0 ) // D, H
198	{
199		minutes += Math.round(seconds/60); // Add a minute if the seconds almost give a minute
200		hours += Math.round(minutes/60); // Add an hour if the minutes almost gives an hour
201
202		if ( days < 2 )
203		{
204			setMinimumTimerInterval(60*minutes);
205			return l_t('1 day, %s hours', hours);
206		}
207		else
208		{
209			setMinimumTimerInterval(60*60*hours);
210			return l_t('%s days', days);
211		}
212	}
213	else if ( hours > 0 ) // H, M
214	{
215		minutes += Math.round(seconds/60); // Add a minute if the seconds almost give a minute)
216		
217		if ( hours < 2 )
218		{
219			setMinimumTimerInterval(seconds);
220			return l_t('1 hour, %s mins', minutes);
221		}
222		if ( hours < 4 )
223		{
224			setMinimumTimerInterval(seconds);
225			return l_t('%s hours, %s mins', hours, minutes);
226		}
227		else
228		{
229			setMinimumTimerInterval(minutes*60);
230			
231			hours += Math.round(minutes/60); // Add an hour if the minutes almost gives an hour
232			
233			return l_t('%s hours', hours);
234		}
235	}
236	else // M, S
237	{
238		if( minutes >= 5 )
239		{
240			setMinimumTimerInterval(seconds);
241			return l_t('%s mins',minutes);
242		}
243		else
244		{
245			setMinimumTimerInterval(1);
246			
247			if( minutes > 1 )
248				return l_t('%s mins, %s secs',minutes,seconds);
249			else if ( minutes > 0 )
250				return l_t('%s min, %s secs',minutes,seconds);
251			else
252				return l_t('%s secs',seconds);
253		}
254	}
255}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.