source: Dev/trunk/src/client/dojox/date/php.js @ 531

Last change on this file since 531 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

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