2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.dtl.utils.date"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.dtl.utils.date"] = true;
|
|
|
3 |
dojo.provide("dojox.dtl.utils.date");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.date.php");
|
|
|
6 |
|
|
|
7 |
dojo.mixin(dojox.dtl.utils.date, {
|
|
|
8 |
format: function(/*Date*/ date, /*String*/ format){
|
|
|
9 |
return dojox.date.php.format(date, format, dojox.dtl.utils.date._overrides);
|
|
|
10 |
},
|
|
|
11 |
timesince: function(d, now){
|
|
|
12 |
// summary:
|
|
|
13 |
// Takes two datetime objects and returns the time between then and now
|
|
|
14 |
// as a nicely formatted string, e.g "10 minutes"
|
|
|
15 |
// description:
|
|
|
16 |
// Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
|
|
|
17 |
if(!(d instanceof Date)){
|
|
|
18 |
d = new Date(d.year, d.month, d.day);
|
|
|
19 |
}
|
|
|
20 |
if(!now){
|
|
|
21 |
now = new Date();
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
var delta = Math.abs(now.getTime() - d.getTime());
|
|
|
25 |
for(var i = 0, chunk; chunk = dojox.dtl.utils.date._chunks[i]; i++){
|
|
|
26 |
var count = Math.floor(delta / chunk[0]);
|
|
|
27 |
if(count) break;
|
|
|
28 |
}
|
|
|
29 |
return count + " " + chunk[1](count);
|
|
|
30 |
},
|
|
|
31 |
_chunks: [
|
|
|
32 |
[60 * 60 * 24 * 365 * 1000, function(n){ return (n == 1) ? 'year' : 'years'; }],
|
|
|
33 |
[60 * 60 * 24 * 30 * 1000, function(n){ return (n == 1) ? 'month' : 'months'; }],
|
|
|
34 |
[60 * 60 * 24 * 7 * 1000, function(n){ return (n == 1) ? 'week' : 'weeks'; }],
|
|
|
35 |
[60 * 60 * 24 * 1000, function(n){ return (n == 1) ? 'day' : 'days'; }],
|
|
|
36 |
[60 * 60 * 1000, function(n){ return (n == 1) ? 'hour' : 'hours'; }],
|
|
|
37 |
[60 * 1000, function(n){ return (n == 1) ? 'minute' : 'minutes'; }]
|
|
|
38 |
],
|
|
|
39 |
_months_ap: ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."],
|
|
|
40 |
_overrides: {
|
|
|
41 |
f: function(){
|
|
|
42 |
// summary:
|
|
|
43 |
// Time, in 12-hour hours and minutes, with minutes left off if they're zero.
|
|
|
44 |
// description:
|
|
|
45 |
// Examples: '1', '1:30', '2:05', '2'
|
|
|
46 |
// Proprietary extension.
|
|
|
47 |
if(!this.date.getMinutes()) return this.g();
|
|
|
48 |
},
|
|
|
49 |
N: function(){
|
|
|
50 |
// summary: Month abbreviation in Associated Press style. Proprietary extension.
|
|
|
51 |
return dojox.dtl.utils.date._months_ap[this.date.getMonth()];
|
|
|
52 |
},
|
|
|
53 |
P: function(){
|
|
|
54 |
// summary:
|
|
|
55 |
// Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off
|
|
|
56 |
// if they're zero and the strings 'midnight' and 'noon' if appropriate.
|
|
|
57 |
// description:
|
|
|
58 |
// Examples: '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.'
|
|
|
59 |
// Proprietary extension.
|
|
|
60 |
if(!this.date.getMinutes() && !this.date.getHours()) return 'midnight';
|
|
|
61 |
if(!this.date.getMinutes() && this.date.getHours() == 12) return 'noon';
|
|
|
62 |
return self.f() + " " + self.a();
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
});
|
|
|
66 |
|
|
|
67 |
}
|