| 1 |
define(["dojo/_base/kernel", "dojo/_base/lang","dojo/date","dojox/string/tokenize"], function(dojo,dlang,ddate,dxst){ |
| 2 |
|
| 3 |
var php = dojo.getObject("date.php", true, dojox); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
php.format = function( date, format){ |
| 11 |
|
| 12 |
|
| 13 |
var df = new php.DateFormat(format); |
| 14 |
return df.format(date); |
| 15 |
}; |
| 16 |
|
| 17 |
php.DateFormat = function( format){ |
| 18 |
|
| 19 |
|
| 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 |
|
| 45 |
dojo.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){ |
| 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 |
|
| 61 |
|
| 62 |
d: function(){ |
| 63 |
|
| 64 |
|
| 65 |
var j = this.j(); |
| 66 |
return (j.length == 1) ? "0" + j : j; |
| 67 |
}, |
| 68 |
|
| 69 |
D: function(){ |
| 70 |
|
| 71 |
|
| 72 |
return this.weekdays_3[this.date.getDay()]; |
| 73 |
}, |
| 74 |
|
| 75 |
j: function(){ |
| 76 |
|
| 77 |
|
| 78 |
return this.date.getDate() + ""; |
| 79 |
}, |
| 80 |
|
| 81 |
l: function(){ |
| 82 |
|
| 83 |
|
| 84 |
return this.weekdays[this.date.getDay()]; |
| 85 |
}, |
| 86 |
|
| 87 |
N: function(){ |
| 88 |
|
| 89 |
|
| 90 |
var w = this.w(); |
| 91 |
return (!w) ? 7 : w; |
| 92 |
}, |
| 93 |
|
| 94 |
S: function(){ |
| 95 |
|
| 96 |
|
| 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 |
|
| 108 |
|
| 109 |
return this.date.getDay() + ""; |
| 110 |
}, |
| 111 |
|
| 112 |
z: function(){ |
| 113 |
|
| 114 |
|
| 115 |
var millis = this.date.getTime() - new Date(this.date.getFullYear(), 0, 1).getTime(); |
| 116 |
return Math.floor(millis/86400000) + ""; |
| 117 |
}, |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
W: function(){ |
| 122 |
|
| 123 |
|
| 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 |
|
| 158 |
|
| 159 |
F: function(){ |
| 160 |
|
| 161 |
|
| 162 |
return this.months[this.date.getMonth()]; |
| 163 |
}, |
| 164 |
|
| 165 |
m: function(){ |
| 166 |
|
| 167 |
|
| 168 |
var n = this.n(); |
| 169 |
return (n.length == 1) ? "0" + n : n; |
| 170 |
}, |
| 171 |
|
| 172 |
M: function(){ |
| 173 |
|
| 174 |
|
| 175 |
return this.months_3[this.date.getMonth()]; |
| 176 |
}, |
| 177 |
|
| 178 |
n: function(){ |
| 179 |
|
| 180 |
|
| 181 |
return this.date.getMonth() + 1 + ""; |
| 182 |
}, |
| 183 |
|
| 184 |
t: function(){ |
| 185 |
|
| 186 |
|
| 187 |
return (Boolean(this.L()) && this.date.getMonth() == 1) ? 29 : this.monthdays[this.getMonth()]; |
| 188 |
}, |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
L: function(){ |
| 193 |
|
| 194 |
|
| 195 |
return (ddate.isLeapYear(this.date)) ? "1" : "0"; |
| 196 |
}, |
| 197 |
|
| 198 |
o: function(){ |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
}, |
| 205 |
|
| 206 |
Y: function(){ |
| 207 |
|
| 208 |
|
| 209 |
return this.date.getFullYear() + ""; |
| 210 |
}, |
| 211 |
|
| 212 |
y: function(){ |
| 213 |
|
| 214 |
|
| 215 |
return this.Y().slice(-2); |
| 216 |
}, |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
a: function(){ |
| 221 |
|
| 222 |
|
| 223 |
return this.date.getHours() >= 12 ? "pm" : "am"; |
| 224 |
}, |
| 225 |
|
| 226 |
b: function(){ |
| 227 |
|
| 228 |
|
| 229 |
return this.a().toUpperCase(); |
| 230 |
}, |
| 231 |
|
| 232 |
B: function(){ |
| 233 |
|
| 234 |
|
| 235 |
|
| 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 |
|
| 245 |
|
| 246 |
return (this.date.getHours() % 12 || 12) + ""; |
| 247 |
}, |
| 248 |
|
| 249 |
G: function(){ |
| 250 |
|
| 251 |
|
| 252 |
return this.date.getHours() + ""; |
| 253 |
}, |
| 254 |
|
| 255 |
h: function(){ |
| 256 |
|
| 257 |
|
| 258 |
var g = this.g(); |
| 259 |
return (g.length == 1) ? "0" + g : g; |
| 260 |
}, |
| 261 |
|
| 262 |
H: function(){ |
| 263 |
|
| 264 |
|
| 265 |
var G = this.G(); |
| 266 |
return (G.length == 1) ? "0" + G : G; |
| 267 |
}, |
| 268 |
|
| 269 |
i: function(){ |
| 270 |
|
| 271 |
|
| 272 |
var mins = this.date.getMinutes() + ""; |
| 273 |
return (mins.length == 1) ? "0" + mins : mins; |
| 274 |
}, |
| 275 |
|
| 276 |
s: function(){ |
| 277 |
|
| 278 |
|
| 279 |
var secs = this.date.getSeconds() + ""; |
| 280 |
return (secs.length == 1) ? "0" + secs : secs; |
| 281 |
}, |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
e: function(){ |
| 286 |
|
| 287 |
|
| 288 |
return ddate.getTimezoneName(this.date); |
| 289 |
}, |
| 290 |
|
| 291 |
I: function(){ |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
}, |
| 297 |
|
| 298 |
O: function(){ |
| 299 |
|
| 300 |
|
| 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 |
|
| 311 |
|
| 312 |
var O = this.O(); |
| 313 |
return O.substring(0, 2) + ":" + O.substring(2, 4); |
| 314 |
}, |
| 315 |
|
| 316 |
T: function(){ |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
return this.e().substring(0, 3); |
| 322 |
}, |
| 323 |
|
| 324 |
Z: function(){ |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
return this.date.getTimezoneOffset() * -60; |
| 329 |
}, |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
c: function(){ |
| 334 |
|
| 335 |
|
| 336 |
return this.Y() + "-" + this.m() + "-" + this.d() + "T" + this.h() + ":" + this.i() + ":" + this.s() + this.P(); |
| 337 |
}, |
| 338 |
|
| 339 |
r: function(){ |
| 340 |
|
| 341 |
|
| 342 |
return this.D() + ", " + this.d() + " " + this.M() + " " + this.Y() + " " + this.H() + ":" + this.i() + ":" + this.s() + " " + this.O(); |
| 343 |
}, |
| 344 |
|
| 345 |
U: function(){ |
| 346 |
|
| 347 |
|
| 348 |
return Math.floor(this.date.getTime() / 1000); |
| 349 |
} |
| 350 |
|
| 351 |
}); |
| 352 |
return php; |
| 353 |
}); |