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