annotate js/modules/date-utils.js @ 100:c486d35fad27

added more mocks; black-box now talks to a really simple fake bugzilla 'server'.
author Atul Varma <avarma@mozilla.com>
date Tue, 27 Apr 2010 23:06:29 -0700
parents 544d339d2b4c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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) {
99
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
2 // Dynamically replace this when QA testing, for determinism.
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
3 exports.now = function now() {
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
4 return new Date();
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
5 };
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
6
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
7 // 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
8 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
9 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
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
11 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
12 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
13 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
14 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
15 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
16 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
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
18
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 // 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
20 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
21 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
22 "(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
23 "(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
24 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
25
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 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
27 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
28
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[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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
38 }
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 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
41 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
42 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
43 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
44 };
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 * 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
48 * 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
49 * 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
50 */
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
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 // 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
53 // 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
54 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
55 if (!now)
99
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
56 now = exports.now().getTime();
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
57
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 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
59 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
60 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
61
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 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
63 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
64
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 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
66 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
67 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
68 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
69 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
70 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
71 ) ||
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
72 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
73 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
74 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
75 };
99
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
76
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
77 exports.timeAgo = function timeAgo(ms) {
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
78 var then = new Date(exports.now() - ms);
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
79 return exports.dateToISO8601(then);
544d339d2b4c Added the beginnings of a black-box app that can be used to generate/run functional tests.
Atul Varma <avarma@mozilla.com>
parents: 36
diff changeset
80 };
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
81 };