]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/date/php.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / date / php.js
1 if(!dojo._hasResource["dojox.date.php"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.date.php"] = true;
3 dojo.provide("dojox.date.php");
4 dojo.require("dojo.date");
5 dojo.require("dojox.string.tokenize");
6
7 dojox.date.php.format = function(/*Date*/ date, /*String*/ format){
8         // summary: Get a formatted string for a given date object
9         var df = new dojox.date.php.DateFormat(format);
10         return df.format(date); 
11 }
12
13 dojox.date.php.DateFormat = function(/*String*/ format){
14         // summary: Format the internal date object
15         if(!this.regex){
16                 var keys = [];
17                 for(var key in this.constructor.prototype){
18                         if(dojo.isString(key) && key.length == 1 && dojo.isFunction(this[key])){
19                                 keys.push(key);
20                         }
21                 }
22                 this.constructor.prototype.regex = new RegExp("(?:(\\\\.)|([" + keys.join("") + "]))", "g");
23         }
24
25         var replacements = [];
26
27         this.tokens = dojox.string.tokenize(format, this.regex, function(escape, token, i){
28                 if(token){
29                         replacements.push([i, token]);
30                         return token;
31                 }
32                 if(escape){
33                         return escape.charAt(1);
34                 }
35         });
36
37         this.replacements = replacements;
38 }
39 dojo.extend(dojox.date.php.DateFormat, {
40         weekdays: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
41         weekdays_3: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
42         months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
43         months_3: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
44         monthdays: [31,28,31,30,31,30,31,31,30,31,30,31],
45
46         format: function(/*Date*/ date){
47                 this.date = date;
48                 for(var i = 0, replacement; replacement = this.replacements[i]; i++){
49                         this.tokens[replacement[0]] = this[replacement[1]]();
50                 }
51                 return this.tokens.join("");
52         },
53
54         // Day
55
56         d: function(){
57                 // summary: Day of the month, 2 digits with leading zeros
58                 var j = this.j();
59                 return (j.length == 1) ? "0" + j : j;
60         },
61
62         D: function(){
63                 // summary: A textual representation of a day, three letters
64                 return this.weekdays_3[this.date.getDay()];
65         },
66
67         j: function(){
68                 // summary: Day of the month without leading zeros
69                 return this.date.getDate() + "";
70         },
71
72         l: function(){
73                 // summary: A full textual representation of the day of the week
74                 return this.weekdays[this.date.getDay()];
75         },
76         
77         N: function(){
78                 // summary: ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
79                 var w = this.w();
80                 return (!w) ? 7 : w;
81         },
82
83         S: function(){
84                 // summary: English ordinal suffix for the day of the month, 2 characters
85                 switch(this.date.getDate()){
86                         case 11: case 12: case 13: return "th";
87                         case 1: case 21: case 31: return "st";
88                         case 2: case 22: return "nd";
89                         case 3: case 23: return "rd";
90                         default: return "th";
91                 }
92         },
93
94         w: function(){
95                 // summary: Numeric representation of the day of the week
96                 return this.date.getDay() + "";
97         },
98
99         z: function(){
100                 // summary: The day of the year (starting from 0)
101                 var millis = this.date.getTime() - new Date(this.date.getFullYear(), 0, 1).getTime();
102                 return Math.floor(millis/86400000) + "";
103         },
104
105         // Week
106
107         W: function(){
108                 // summary: ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
109                 var week;
110                 var jan1_w = new Date(this.date.getFullYear(), 0, 1).getDay() + 1;
111                 var w = this.date.getDay() + 1;
112                 var z = parseInt(this.z());
113
114                 if(z <= (8 - jan1_w) && jan1_w > 4){
115                         var last_year = new Date(this.date.getFullYear() - 1, this.date.getMonth(), this.date.getDate());
116                         if(jan1_w == 5 || (jan1_w == 6 && dojo.date.isLeapYear(last_year))){
117                                 week = 53;
118                         }else{
119                                 week = 52;
120                         }
121                 }else{
122                         var i;
123                         if(Boolean(this.L())){
124                                 i = 366;
125                         }else{
126                                 i = 365;
127                         }
128                         if((i - z) < (4 - w)){
129                                 week = 1;
130                         }else{
131                                 var j = z + (7 - w) + (jan1_w - 1);
132                                 week = Math.ceil(j / 7);
133                                 if(jan1_w > 4){
134                                         --week;
135                                 }
136                         }
137                 }
138                 
139                 return week;
140         },
141
142         // Month
143
144         F: function(){
145                 // summary: A full textual representation of a month, such as January or March
146                 return this.months[this.date.getMonth()];
147         },
148
149         m: function(){
150                 // summary: Numeric representation of a month, with leading zeros
151                 var n = this.n();
152                 return (n.length == 1) ? "0" + n : n;
153         },
154
155         M: function(){
156                 // summary: A short textual representation of a month, three letters
157                 return this.months_3[this.date.getMonth()];
158         },
159
160         n: function(){
161                 // summary: Numeric representation of a month, without leading zeros
162                 return this.date.getMonth() + 1 + "";
163         },
164
165         t: function(){
166                 // summary: Number of days in the given month
167                 return (Boolean(this.L()) && this.date.getMonth() == 1) ? 29 : this.monthdays[this.getMonth()];
168         },
169
170         // Year
171
172         L: function(){
173                 // summary: Whether it's a leap year
174                 return (dojo.date.isLeapYear(this.date)) ? "1" : "0";
175         },
176
177         o: function(){
178                 // summary:
179                 //              ISO-8601 year number. This has the same value as Y, except that if
180                 //              the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
181                 // TODO: Figure out what this means
182         },
183
184         Y: function(){
185                 // summary: A full numeric representation of a year, 4 digits
186                 return this.date.getFullYear() + "";
187         },
188
189         y: function(){
190                 // summary: A two digit representation of a year
191                 return this.Y().slice(-2);
192         },
193
194         // Time
195
196         a: function(){
197                 // summary: Lowercase Ante meridiem and Post meridiem
198                 return this.date.getHours() >= 12 ? "pm" : "am";
199         },
200
201         b: function(){
202                 // summary: Uppercase Ante meridiem and Post meridiem
203                 return this.a().toUpperCase();
204         },
205
206         B: function(){
207                 // summary:
208                 //      Swatch Internet time
209                 //      A day is 1,000 beats. All time is measured from GMT + 1
210                 var off = this.date.getTimezoneOffset() + 60;
211                 var secs = (this.date.getHours() * 3600) + (this.date.getMinutes() * 60) + this.getSeconds() + (off * 60);
212                 var beat = Math.abs(Math.floor(secs / 86.4) % 1000) + "";
213                 while(beat.length <  2) beat = "0" + beat;
214                 return beat;
215         },
216
217         g: function(){
218                 // summary: 12-hour format of an hour without leading zeros
219                 return (this.date.getHours() > 12) ? this.date.getHours() - 12 + "" : this.date.getHours() + "";
220         },
221
222         G: function(){
223                 // summary: 24-hour format of an hour without leading zeros
224                 return this.date.getHours() + "";
225         },
226
227         h: function(){
228                 // summary: 12-hour format of an hour with leading zeros
229                 var g = this.g();
230                 return (g.length == 1) ? "0" + g : g;
231         },
232
233         H: function(){
234                 // summary: 24-hour format of an hour with leading zeros
235                 var G = this.G();
236                 return (G.length == 1) ? "0" + G : G;
237         },
238
239         i: function(){
240                 // summary: Minutes with leading zeros
241                 var mins = this.date.getMinutes() + "";
242                 return (mins.length == 1) ? "0" + mins : mins;
243         },
244
245         s: function(){
246                 // summary: Seconds, with leading zeros
247                 var secs = this.date.getSeconds() + "";
248                 return (secs.length == 1) ? "0" + secs : secs;
249         },
250
251         // Timezone
252
253         e: function(){
254                 // summary: Timezone identifier (added in PHP 5.1.0)
255                 return dojo.date.getTimezoneName(this.date);
256         },
257
258         I: function(){
259                 // summary: Whether or not the date is in daylight saving time
260                 // TODO: Can dojo.date do this?
261         },
262
263         O: function(){
264                 // summary: Difference to Greenwich time (GMT) in hours
265                 var off = Math.abs(this.date.getTimezoneOffset());
266                 var hours = Math.floor(off / 60) + "";
267                 var mins = (off % 60) + "";
268                 if(hours.length == 1) hours = "0" + hours;
269                 if(mins.length == 1) hours = "0" + mins;
270                 return ((this.date.getTimezoneOffset() < 0) ? "+" : "-") + hours + mins;
271         },
272
273         P: function(){
274                 // summary: Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)
275                 var O = this.O();
276                 return O.substring(0, 2) + ":" + O.substring(2, 4);
277         },
278
279         T: function(){
280                 // summary: Timezone abbreviation
281
282                 // Guess...
283                 return this.e().substring(0, 3);
284         },
285
286         Z: function(){
287                 // summary:
288                 //              Timezone offset in seconds. The offset for timezones west of UTC is always negative,
289                 //              and for those east of UTC is always positive.
290                 return this.date.getTimezoneOffset() * -60;
291         },
292
293         // Full Date/Time
294
295         c: function(){
296                 // summary: ISO 8601 date (added in PHP 5)
297                 return this.Y() + "-" + this.m() + "-" + this.d() + "T" + this.h() + ":" + this.i() + ":" + this.s() + this.P();
298         },
299
300         r: function(){
301                 // summary: RFC 2822 formatted date
302                 return this.D() + ", " + this.d() + " " + this.M() + " " + this.Y() + " " + this.H() + ":" + this.i() + ":" + this.s() + " " + this.O();
303         },
304
305         U: function(){
306                 // summary: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
307                 return Math.floor(this.date.getTime() / 1000);
308         }
309
310 });
311
312 }