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");
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);
13 dojox.date.php.DateFormat = function(/*String*/ format){
14 // summary: Format the internal date object
17 for(var key in this.constructor.prototype){
18 if(dojo.isString(key) && key.length == 1 && dojo.isFunction(this[key])){
22 this.constructor.prototype.regex = new RegExp("(?:(\\\\.)|([" + keys.join("") + "]))", "g");
25 var replacements = [];
27 this.tokens = dojox.string.tokenize(format, this.regex, function(escape, token, i){
29 replacements.push([i, token]);
33 return escape.charAt(1);
37 this.replacements = replacements;
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],
46 format: function(/*Date*/ date){
48 for(var i = 0, replacement; replacement = this.replacements[i]; i++){
49 this.tokens[replacement[0]] = this[replacement[1]]();
51 return this.tokens.join("");
57 // summary: Day of the month, 2 digits with leading zeros
59 return (j.length == 1) ? "0" + j : j;
63 // summary: A textual representation of a day, three letters
64 return this.weekdays_3[this.date.getDay()];
68 // summary: Day of the month without leading zeros
69 return this.date.getDate() + "";
73 // summary: A full textual representation of the day of the week
74 return this.weekdays[this.date.getDay()];
78 // summary: ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
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";
95 // summary: Numeric representation of the day of the week
96 return this.date.getDay() + "";
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) + "";
108 // summary: ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
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());
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))){
123 if(Boolean(this.L())){
128 if((i - z) < (4 - w)){
131 var j = z + (7 - w) + (jan1_w - 1);
132 week = Math.ceil(j / 7);
145 // summary: A full textual representation of a month, such as January or March
146 return this.months[this.date.getMonth()];
150 // summary: Numeric representation of a month, with leading zeros
152 return (n.length == 1) ? "0" + n : n;
156 // summary: A short textual representation of a month, three letters
157 return this.months_3[this.date.getMonth()];
161 // summary: Numeric representation of a month, without leading zeros
162 return this.date.getMonth() + 1 + "";
166 // summary: Number of days in the given month
167 return (Boolean(this.L()) && this.date.getMonth() == 1) ? 29 : this.monthdays[this.getMonth()];
173 // summary: Whether it's a leap year
174 return (dojo.date.isLeapYear(this.date)) ? "1" : "0";
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
185 // summary: A full numeric representation of a year, 4 digits
186 return this.date.getFullYear() + "";
190 // summary: A two digit representation of a year
191 return this.Y().slice(-2);
197 // summary: Lowercase Ante meridiem and Post meridiem
198 return this.date.getHours() >= 12 ? "pm" : "am";
202 // summary: Uppercase Ante meridiem and Post meridiem
203 return this.a().toUpperCase();
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;
218 // summary: 12-hour format of an hour without leading zeros
219 return (this.date.getHours() > 12) ? this.date.getHours() - 12 + "" : this.date.getHours() + "";
223 // summary: 24-hour format of an hour without leading zeros
224 return this.date.getHours() + "";
228 // summary: 12-hour format of an hour with leading zeros
230 return (g.length == 1) ? "0" + g : g;
234 // summary: 24-hour format of an hour with leading zeros
236 return (G.length == 1) ? "0" + G : G;
240 // summary: Minutes with leading zeros
241 var mins = this.date.getMinutes() + "";
242 return (mins.length == 1) ? "0" + mins : mins;
246 // summary: Seconds, with leading zeros
247 var secs = this.date.getSeconds() + "";
248 return (secs.length == 1) ? "0" + secs : secs;
254 // summary: Timezone identifier (added in PHP 5.1.0)
255 return dojo.date.getTimezoneName(this.date);
259 // summary: Whether or not the date is in daylight saving time
260 // TODO: Can dojo.date do this?
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;
274 // summary: Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)
276 return O.substring(0, 2) + ":" + O.substring(2, 4);
280 // summary: Timezone abbreviation
283 return this.e().substring(0, 3);
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;
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();
301 // summary: RFC 2822 formatted date
302 return this.D() + ", " + this.d() + " " + this.M() + " " + this.Y() + " " + this.H() + ":" + this.i() + ":" + this.s() + " " + this.O();
306 // summary: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
307 return Math.floor(this.date.getTime() / 1000);