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