Mercurial > bugzilla-dashboard
annotate js/modules/date-utils.js @ 71:4ec651cc606e
we now only make one request at a time to the bugzilla server, which reduces app responsiveness but makes us less of a douche to the server.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Sun, 25 Apr 2010 17:55:54 -0700 |
parents | eb2cc1f89869 |
children | 544d339d2b4c |
rev | line source |
---|---|
36
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
1 Require.modules["date-utils"] = function(exports) { |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
2 // Taken from MDC @ Core_JavaScript_1.5_Reference/Objects/Date. |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
3 exports.dateToISO8601 = function dateToISO8601(d) { |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
4 function pad(n) { return n < 10 ? '0' + n : n; } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
5 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
6 return (d.getUTCFullYear() + '-' + |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
7 pad(d.getUTCMonth() + 1) + '-' + |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
8 pad(d.getUTCDate()) + 'T' + |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
9 pad(d.getUTCHours()) + ':' + |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
10 pad(d.getUTCMinutes()) + ':' + |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
11 pad(d.getUTCSeconds()) + 'Z'); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
12 }; |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
13 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
14 // Taken from http://delete.me.uk/2005/03/iso8601.html |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
15 exports.dateFromISO8601 = function dateFromISO8601(string) { |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
16 var regexp = ("([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
17 "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" + |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
18 "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
19 var d = string.match(new RegExp(regexp)); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
20 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
21 var offset = 0; |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
22 var date = new Date(d[1], 0, 1); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
23 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
24 if (d[3]) { date.setMonth(d[3] - 1); } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
25 if (d[5]) { date.setDate(d[5]); } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
26 if (d[7]) { date.setHours(d[7]); } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
27 if (d[8]) { date.setMinutes(d[8]); } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
28 if (d[10]) { date.setSeconds(d[10]); } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
29 if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
30 if (d[14]) { |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
31 offset = (Number(d[16]) * 60) + Number(d[17]); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
32 offset *= ((d[15] == '-') ? 1 : -1); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
33 } |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
34 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
35 offset -= date.getTimezoneOffset(); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
36 var time = (Number(date) + (offset * 60 * 1000)); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
37 date.setTime(Number(time)); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
38 return date; |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
39 }; |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
40 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
41 /* |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
42 * JavaScript Pretty Date |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
43 * Copyright (c) 2008 John Resig (jquery.com) |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
44 * Licensed under the MIT license. |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
45 */ |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
46 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
47 // Takes an ISO time and returns a string representing how |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
48 // long ago the date represents. |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
49 exports.prettyDate = function prettyDate(time, now){ |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
50 if (!now) |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
51 now = (new Date()).getTime(); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
52 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
53 var date = exports.dateFromISO8601(time), |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
54 diff = ((now - date.getTime()) / 1000), |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
55 day_diff = Math.floor(diff / 86400); |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
56 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
57 if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 ) |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
58 return null; |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
59 |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
60 return day_diff == 0 && ( |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
61 diff < 60 && "just now" || |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
62 diff < 120 && "1 minute ago" || |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
63 diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" || |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
64 diff < 7200 && "1 hour ago" || |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
65 diff < 86400 && Math.floor( diff / 3600 ) + " hours ago" |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
66 ) || |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
67 day_diff == 1 && "Yesterday" || |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
68 day_diff < 7 && day_diff + " days ago" || |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
69 day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago"; |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
70 }; |
eb2cc1f89869
Added a test suite, basic CommonJS module loader, and a new main.html file that will be a unified UI to all three pages (dashboard, user-finder, bug-filer).
Atul Varma <avarma@mozilla.com>
parents:
diff
changeset
|
71 }; |