Mercurial > urweb
comparison lib/js/urweb.js @ 1609:c6cc104a47ac
Client-side timef
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Thu, 24 Nov 2011 11:27:51 -0500 |
parents | 252e05bf199d |
children | 37f5a23446b2 |
comparison
equal
deleted
inserted
replaced
1608:77180224f1f9 | 1609:c6cc104a47ac |
---|---|
154 return t * 1000; | 154 return t * 1000; |
155 } catch (e) { | 155 } catch (e) { |
156 return null; | 156 return null; |
157 } | 157 } |
158 } | 158 } |
159 | |
160 /* | |
161 strftime for Javascript | |
162 Copyright (c) 2008, Philip S Tellis <philip@bluesmoon.info> | |
163 All rights reserved. | |
164 | |
165 This code is distributed under the terms of the BSD licence | |
166 | |
167 Redistribution and use of this software in source and binary forms, with or without modification, | |
168 are permitted provided that the following conditions are met: | |
169 | |
170 * Redistributions of source code must retain the above copyright notice, this list of conditions | |
171 and the following disclaimer. | |
172 * Redistributions in binary form must reproduce the above copyright notice, this list of | |
173 conditions and the following disclaimer in the documentation and/or other materials provided | |
174 with the distribution. | |
175 * The names of the contributors to this file may not be used to endorse or promote products | |
176 derived from this software without specific prior written permission. | |
177 | |
178 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | |
179 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
180 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
181 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
182 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
183 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
184 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
185 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
186 */ | |
187 | |
188 Date.ext = {}; | |
189 Date.ext.util = {}; | |
190 Date.ext.util.xPad=function(x, pad, r) | |
191 { | |
192 if(typeof(r) == 'undefined') | |
193 { | |
194 r=10; | |
195 } | |
196 for( ; parseInt(x, 10)<r && r>1; r/=10) | |
197 x = pad.toString() + x; | |
198 return x.toString(); | |
199 }; | |
200 Date.prototype.locale = 'en-US'; | |
201 if(document.getElementsByTagName('html') && document.getElementsByTagName('html')[0].lang) | |
202 { | |
203 Date.prototype.locale = document.getElementsByTagName('html')[0].lang; | |
204 } | |
205 Date.ext.locales = { }; | |
206 Date.ext.locales.en = { | |
207 a: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | |
208 A: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], | |
209 b: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], | |
210 B: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | |
211 c: '%a %d %b %Y %T %Z', | |
212 p: ['AM', 'PM'], | |
213 P: ['am', 'pm'], | |
214 x: '%d/%m/%y', | |
215 X: '%T' | |
216 }; | |
217 Date.ext.locales['en-US'] = Date.ext.locales.en; | |
218 Date.ext.locales['en-US'].c = '%a %d %b %Y %r %Z'; | |
219 Date.ext.locales['en-US'].x = '%D'; | |
220 Date.ext.locales['en-US'].X = '%r'; | |
221 Date.ext.locales['en-GB'] = Date.ext.locales.en; | |
222 Date.ext.locales['en-AU'] = Date.ext.locales['en-GB']; | |
223 Date.ext.formats = { | |
224 a: function(d) { return Date.ext.locales[d.locale].a[d.getDay()]; }, | |
225 A: function(d) { return Date.ext.locales[d.locale].A[d.getDay()]; }, | |
226 b: function(d) { return Date.ext.locales[d.locale].b[d.getMonth()]; }, | |
227 B: function(d) { return Date.ext.locales[d.locale].B[d.getMonth()]; }, | |
228 c: 'toLocaleString', | |
229 C: function(d) { return Date.ext.util.xPad(parseInt(d.getFullYear()/100, 10), 0); }, | |
230 d: ['getDate', '0'], | |
231 e: ['getDate', ' '], | |
232 g: function(d) { return Date.ext.util.xPad(parseInt(Date.ext.util.G(d)/100, 10), 0); }, | |
233 G: function(d) { | |
234 var y = d.getFullYear(); | |
235 var V = parseInt(Date.ext.formats.V(d), 10); | |
236 var W = parseInt(Date.ext.formats.W(d), 10); | |
237 | |
238 if(W > V) { | |
239 y++; | |
240 } else if(W===0 && V>=52) { | |
241 y--; | |
242 } | |
243 | |
244 return y; | |
245 }, | |
246 H: ['getHours', '0'], | |
247 I: function(d) { var I=d.getHours()%12; return Date.ext.util.xPad(I===0?12:I, 0); }, | |
248 j: function(d) { | |
249 var ms = d - new Date('' + d.getFullYear() + '/1/1 GMT'); | |
250 ms += d.getTimezoneOffset()*60000; | |
251 var doy = parseInt(ms/60000/60/24, 10)+1; | |
252 return Date.ext.util.xPad(doy, 0, 100); | |
253 }, | |
254 m: function(d) { return Date.ext.util.xPad(d.getMonth()+1, 0); }, | |
255 M: ['getMinutes', '0'], | |
256 p: function(d) { return Date.ext.locales[d.locale].p[d.getHours() >= 12 ? 1 : 0 ]; }, | |
257 P: function(d) { return Date.ext.locales[d.locale].P[d.getHours() >= 12 ? 1 : 0 ]; }, | |
258 S: ['getSeconds', '0'], | |
259 u: function(d) { var dow = d.getDay(); return dow===0?7:dow; }, | |
260 U: function(d) { | |
261 var doy = parseInt(Date.ext.formats.j(d), 10); | |
262 var rdow = 6-d.getDay(); | |
263 var woy = parseInt((doy+rdow)/7, 10); | |
264 return Date.ext.util.xPad(woy, 0); | |
265 }, | |
266 V: function(d) { | |
267 var woy = parseInt(Date.ext.formats.W(d), 10); | |
268 var dow1_1 = (new Date('' + d.getFullYear() + '/1/1')).getDay(); | |
269 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1); | |
270 if(idow == 53 && (new Date('' + d.getFullYear() + '/12/31')).getDay() < 4) | |
271 { | |
272 idow = 1; | |
273 } | |
274 else if(idow === 0) | |
275 { | |
276 idow = Date.ext.formats.V(new Date('' + (d.getFullYear()-1) + '/12/31')); | |
277 } | |
278 | |
279 return Date.ext.util.xPad(idow, 0); | |
280 }, | |
281 w: 'getDay', | |
282 W: function(d) { | |
283 var doy = parseInt(Date.ext.formats.j(d), 10); | |
284 var rdow = 7-Date.ext.formats.u(d); | |
285 var woy = parseInt((doy+rdow)/7, 10); | |
286 return Date.ext.util.xPad(woy, 0, 10); | |
287 }, | |
288 y: function(d) { return Date.ext.util.xPad(d.getFullYear()%100, 0); }, | |
289 Y: 'getFullYear', | |
290 z: function(d) { | |
291 var o = d.getTimezoneOffset(); | |
292 var H = Date.ext.util.xPad(parseInt(Math.abs(o/60), 10), 0); | |
293 var M = Date.ext.util.xPad(o%60, 0); | |
294 return (o>0?'-':'+') + H + M; | |
295 }, | |
296 Z: function(d) { return d.toString().replace(/^.*\(([^)]+)\)$/, '$1'); }, | |
297 '%': function(d) { return '%'; } | |
298 }; | |
299 Date.ext.aggregates = { | |
300 c: 'locale', | |
301 D: '%m/%d/%y', | |
302 h: '%b', | |
303 n: '\n', | |
304 r: '%I:%M:%S %p', | |
305 R: '%H:%M', | |
306 t: '\t', | |
307 T: '%H:%M:%S', | |
308 x: 'locale', | |
309 X: 'locale' | |
310 }; | |
311 Date.ext.aggregates.z = Date.ext.formats.z(new Date()); | |
312 Date.ext.aggregates.Z = Date.ext.formats.Z(new Date()); | |
313 Date.ext.unsupported = { }; | |
314 function strftime(fmt, thisTime) | |
315 { | |
316 var thisDate = new Date(); | |
317 thisDate.setTime(thisTime / 1000); | |
318 | |
319 if(!(thisDate.locale in Date.ext.locales)) | |
320 { | |
321 if(thisDate.locale.replace(/-[a-zA-Z]+$/, '') in Date.ext.locales) | |
322 { | |
323 thisDate.locale = thisDate.locale.replace(/-[a-zA-Z]+$/, ''); | |
324 } | |
325 else | |
326 { | |
327 thisDate.locale = 'en-US'; | |
328 } | |
329 } | |
330 | |
331 var d = thisDate; | |
332 while(fmt.match(/%[cDhnrRtTxXzZ]/)) | |
333 { | |
334 fmt = fmt.replace(/%([cDhnrRtTxXzZ])/g, function(m0, m1) | |
335 { | |
336 var f = Date.ext.aggregates[m1]; | |
337 return (f == 'locale' ? Date.ext.locales[d.locale][m1] : f); | |
338 }); | |
339 } | |
340 | |
341 var str = fmt.replace(/%([aAbBCdegGHIjmMpPSuUVwWyY%])/g, function(m0, m1) | |
342 { | |
343 var f = Date.ext.formats[m1]; | |
344 if(typeof(f) == 'string') { | |
345 return d[f](); | |
346 } else if(typeof(f) == 'function') { | |
347 return f.call(d, d); | |
348 } else if(typeof(f) == 'object' && typeof(f[0]) == 'string') { | |
349 return Date.ext.util.xPad(d[f[0]](), f[1]); | |
350 } else { | |
351 return m1; | |
352 } | |
353 }); | |
354 d=null; | |
355 return str; | |
356 }; | |
357 | |
358 // End of code from Philip S Tellis | |
159 | 359 |
160 | 360 |
161 // Error handling | 361 // Error handling |
162 | 362 |
163 function whine(msg) { | 363 function whine(msg) { |