[ Index ] |
PHP Cross Reference of phpwcms V1.4.7 _r403 (01.11.10) |
[Summary view] [Print] [Text view]
1 /* 2 --- 3 4 script: Date.js 5 6 name: Date 7 8 description: Extends the Date native object to include methods useful in managing dates. 9 10 license: MIT-style license 11 12 authors: 13 - Aaron Newton 14 - Nicholas Barthelemy - https://svn.nbarthelemy.com/date-js/ 15 - Harald Kirshner - mail [at] digitarald.de; http://digitarald.de 16 - Scott Kyle - scott [at] appden.com; http://appden.com 17 18 requires: 19 - Core/Array 20 - Core/String 21 - Core/Number 22 - /Lang 23 - /Date.English.US 24 - /MooTools.More 25 26 provides: [Date] 27 28 ... 29 */ 30 31 (function(){ 32 33 var Date = this.Date; 34 35 if (!Date.now) Date.now = $time; 36 37 Date.Methods = { 38 ms: 'Milliseconds', 39 year: 'FullYear', 40 min: 'Minutes', 41 mo: 'Month', 42 sec: 'Seconds', 43 hr: 'Hours' 44 }; 45 46 ['Date', 'Day', 'FullYear', 'Hours', 'Milliseconds', 'Minutes', 'Month', 'Seconds', 'Time', 'TimezoneOffset', 47 'Week', 'Timezone', 'GMTOffset', 'DayOfYear', 'LastMonth', 'LastDayOfMonth', 'UTCDate', 'UTCDay', 'UTCFullYear', 48 'AMPM', 'Ordinal', 'UTCHours', 'UTCMilliseconds', 'UTCMinutes', 'UTCMonth', 'UTCSeconds', 'UTCMilliseconds'].each(function(method){ 49 Date.Methods[method.toLowerCase()] = method; 50 }); 51 52 var pad = function(what, length){ 53 return new Array(length - String(what).length + 1).join('0') + what; 54 }; 55 56 Date.implement({ 57 58 set: function(prop, value){ 59 switch ($type(prop)){ 60 case 'object': 61 for (var p in prop) this.set(p, prop[p]); 62 break; 63 case 'string': 64 prop = prop.toLowerCase(); 65 var m = Date.Methods; 66 if (m[prop]) this['set' + m[prop]](value); 67 } 68 return this; 69 }, 70 71 get: function(prop){ 72 prop = prop.toLowerCase(); 73 var m = Date.Methods; 74 if (m[prop]) return this['get' + m[prop]](); 75 return null; 76 }, 77 78 clone: function(){ 79 return new Date(this.get('time')); 80 }, 81 82 increment: function(interval, times){ 83 interval = interval || 'day'; 84 times = $pick(times, 1); 85 86 switch (interval){ 87 case 'year': 88 return this.increment('month', times * 12); 89 case 'month': 90 var d = this.get('date'); 91 this.set('date', 1).set('mo', this.get('mo') + times); 92 return this.set('date', d.min(this.get('lastdayofmonth'))); 93 case 'week': 94 return this.increment('day', times * 7); 95 case 'day': 96 return this.set('date', this.get('date') + times); 97 } 98 99 if (!Date.units[interval]) throw new Error(interval + ' is not a supported interval'); 100 101 return this.set('time', this.get('time') + times * Date.units[interval]()); 102 }, 103 104 decrement: function(interval, times){ 105 return this.increment(interval, -1 * $pick(times, 1)); 106 }, 107 108 isLeapYear: function(){ 109 return Date.isLeapYear(this.get('year')); 110 }, 111 112 clearTime: function(){ 113 return this.set({hr: 0, min: 0, sec: 0, ms: 0}); 114 }, 115 116 diff: function(date, resolution){ 117 if ($type(date) == 'string') date = Date.parse(date); 118 119 return ((date - this) / Date.units[resolution || 'day'](3, 3)).round(); // non-leap year, 30-day month 120 }, 121 122 getLastDayOfMonth: function(){ 123 return Date.daysInMonth(this.get('mo'), this.get('year')); 124 }, 125 126 getDayOfYear: function(){ 127 return (Date.UTC(this.get('year'), this.get('mo'), this.get('date') + 1) 128 - Date.UTC(this.get('year'), 0, 1)) / Date.units.day(); 129 }, 130 131 getWeek: function(){ 132 return (this.get('dayofyear') / 7).ceil(); 133 }, 134 135 getOrdinal: function(day){ 136 return Date.getMsg('ordinal', day || this.get('date')); 137 }, 138 139 getTimezone: function(){ 140 return this.toString() 141 .replace(/^.*? ([A-Z]{3}).[0-9]{4}.*$/, '$1') 142 .replace(/^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, '$1$2$3'); 143 }, 144 145 getGMTOffset: function(){ 146 var off = this.get('timezoneOffset'); 147 return ((off > 0) ? '-' : '+') + pad((off.abs() / 60).floor(), 2) + pad(off % 60, 2); 148 }, 149 150 setAMPM: function(ampm){ 151 ampm = ampm.toUpperCase(); 152 var hr = this.get('hr'); 153 if (hr > 11 && ampm == 'AM') return this.decrement('hour', 12); 154 else if (hr < 12 && ampm == 'PM') return this.increment('hour', 12); 155 return this; 156 }, 157 158 getAMPM: function(){ 159 return (this.get('hr') < 12) ? 'AM' : 'PM'; 160 }, 161 162 parse: function(str){ 163 this.set('time', Date.parse(str)); 164 return this; 165 }, 166 167 isValid: function(date) { 168 return !isNaN((date || this).valueOf()); 169 }, 170 171 format: function(f){ 172 if (!this.isValid()) return 'invalid date'; 173 f = f || '%x %X'; 174 f = formats[f.toLowerCase()] || f; // replace short-hand with actual format 175 var d = this; 176 return f.replace(/%([a-z%])/gi, 177 function($0, $1){ 178 switch ($1){ 179 case 'a': return Date.getMsg('days')[d.get('day')].substr(0, 3); 180 case 'A': return Date.getMsg('days')[d.get('day')]; 181 case 'b': return Date.getMsg('months')[d.get('month')].substr(0, 3); 182 case 'B': return Date.getMsg('months')[d.get('month')]; 183 case 'c': return d.toString(); 184 case 'd': return pad(d.get('date'), 2); 185 case 'D': return d.get('date'); 186 case 'e': return d.get('date'); 187 case 'H': return pad(d.get('hr'), 2); 188 case 'I': return ((d.get('hr') % 12) || 12); 189 case 'j': return pad(d.get('dayofyear'), 3); 190 case 'm': return pad((d.get('mo') + 1), 2); 191 case 'M': return pad(d.get('min'), 2); 192 case 'o': return d.get('ordinal'); 193 case 'p': return Date.getMsg(d.get('ampm')); 194 case 's': return Math.round(d / 1000); 195 case 'S': return pad(d.get('seconds'), 2); 196 case 'U': return pad(d.get('week'), 2); 197 case 'w': return d.get('day'); 198 case 'x': return d.format(Date.getMsg('shortDate')); 199 case 'X': return d.format(Date.getMsg('shortTime')); 200 case 'y': return d.get('year').toString().substr(2); 201 case 'Y': return d.get('year'); 202 case 'T': return d.get('GMTOffset'); 203 case 'Z': return d.get('Timezone'); 204 case 'z': return pad(d.get('ms'), 3); 205 } 206 return $1; 207 } 208 ); 209 }, 210 211 toISOString: function(){ 212 return this.format('iso8601'); 213 } 214 215 }); 216 217 Date.alias('toISOString', 'toJSON'); 218 Date.alias('diff', 'compare'); 219 Date.alias('format', 'strftime'); 220 221 var formats = { 222 db: '%Y-%m-%d %H:%M:%S', 223 compact: '%Y%m%dT%H%M%S', 224 iso8601: '%Y-%m-%dT%H:%M:%S%T', 225 rfc822: '%a, %d %b %Y %H:%M:%S %Z', 226 'short': '%d %b %H:%M', 227 'long': '%B %d, %Y %H:%M' 228 }; 229 230 var parsePatterns = []; 231 var nativeParse = Date.parse; 232 233 var parseWord = function(type, word, num){ 234 var ret = -1; 235 var translated = Date.getMsg(type + 's'); 236 switch ($type(word)){ 237 case 'object': 238 ret = translated[word.get(type)]; 239 break; 240 case 'number': 241 ret = translated[word]; 242 if (!ret) throw new Error('Invalid ' + type + ' index: ' + word); 243 break; 244 case 'string': 245 var match = translated.filter(function(name){ 246 return this.test(name); 247 }, new RegExp('^' + word, 'i')); 248 if (!match.length) throw new Error('Invalid ' + type + ' string'); 249 if (match.length > 1) throw new Error('Ambiguous ' + type); 250 ret = match[0]; 251 } 252 253 return (num) ? translated.indexOf(ret) : ret; 254 }; 255 256 Date.extend({ 257 258 getMsg: function(key, args) { 259 return MooTools.lang.get('Date', key, args); 260 }, 261 262 units: { 263 ms: $lambda(1), 264 second: $lambda(1000), 265 minute: $lambda(60000), 266 hour: $lambda(3600000), 267 day: $lambda(86400000), 268 week: $lambda(608400000), 269 month: function(month, year){ 270 var d = new Date; 271 return Date.daysInMonth($pick(month, d.get('mo')), $pick(year, d.get('year'))) * 86400000; 272 }, 273 year: function(year){ 274 year = year || new Date().get('year'); 275 return Date.isLeapYear(year) ? 31622400000 : 31536000000; 276 } 277 }, 278 279 daysInMonth: function(month, year){ 280 return [31, Date.isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]; 281 }, 282 283 isLeapYear: function(year){ 284 return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0); 285 }, 286 287 parse: function(from){ 288 var t = $type(from); 289 if (t == 'number') return new Date(from); 290 if (t != 'string') return from; 291 from = from.clean(); 292 if (!from.length) return null; 293 294 var parsed; 295 parsePatterns.some(function(pattern){ 296 var bits = pattern.re.exec(from); 297 return (bits) ? (parsed = pattern.handler(bits)) : false; 298 }); 299 300 return parsed || new Date(nativeParse(from)); 301 }, 302 303 parseDay: function(day, num){ 304 return parseWord('day', day, num); 305 }, 306 307 parseMonth: function(month, num){ 308 return parseWord('month', month, num); 309 }, 310 311 parseUTC: function(value){ 312 var localDate = new Date(value); 313 var utcSeconds = Date.UTC( 314 localDate.get('year'), 315 localDate.get('mo'), 316 localDate.get('date'), 317 localDate.get('hr'), 318 localDate.get('min'), 319 localDate.get('sec'), 320 localDate.get('ms') 321 ); 322 return new Date(utcSeconds); 323 }, 324 325 orderIndex: function(unit){ 326 return Date.getMsg('dateOrder').indexOf(unit) + 1; 327 }, 328 329 defineFormat: function(name, format){ 330 formats[name] = format; 331 }, 332 333 defineFormats: function(formats){ 334 for (var name in formats) Date.defineFormat(name, formats[name]); 335 }, 336 337 parsePatterns: parsePatterns, // this is deprecated 338 339 defineParser: function(pattern){ 340 parsePatterns.push((pattern.re && pattern.handler) ? pattern : build(pattern)); 341 }, 342 343 defineParsers: function(){ 344 Array.flatten(arguments).each(Date.defineParser); 345 }, 346 347 define2DigitYearStart: function(year){ 348 startYear = year % 100; 349 startCentury = year - startYear; 350 } 351 352 }); 353 354 var startCentury = 1900; 355 var startYear = 70; 356 357 var regexOf = function(type){ 358 return new RegExp('(?:' + Date.getMsg(type).map(function(name){ 359 return name.substr(0, 3); 360 }).join('|') + ')[a-z]*'); 361 }; 362 363 var replacers = function(key){ 364 switch(key){ 365 case 'x': // iso8601 covers yyyy-mm-dd, so just check if month is first 366 return ((Date.orderIndex('month') == 1) ? '%m[-./]%d' : '%d[-./]%m') + '([-./]%y)?'; 367 case 'X': 368 return '%H([.:]%M)?([.:]%S([.:]%s)?)? ?%p? ?%T?'; 369 } 370 return null; 371 }; 372 373 var keys = { 374 d: /[0-2]?[0-9]|3[01]/, 375 H: /[01]?[0-9]|2[0-3]/, 376 I: /0?[1-9]|1[0-2]/, 377 M: /[0-5]?\d/, 378 s: /\d+/, 379 o: /[a-z]*/, 380 p: /[ap]\.?m\.?/, 381 y: /\d{2}|\d{4}/, 382 Y: /\d{4}/, 383 T: /Z|[+-]\d{2}(?::?\d{2})?/ 384 }; 385 386 keys.m = keys.I; 387 keys.S = keys.M; 388 389 var currentLanguage; 390 391 var recompile = function(language){ 392 currentLanguage = language; 393 394 keys.a = keys.A = regexOf('days'); 395 keys.b = keys.B = regexOf('months'); 396 397 parsePatterns.each(function(pattern, i){ 398 if (pattern.format) parsePatterns[i] = build(pattern.format); 399 }); 400 }; 401 402 var build = function(format){ 403 if (!currentLanguage) return {format: format}; 404 405 var parsed = []; 406 var re = (format.source || format) // allow format to be regex 407 .replace(/%([a-z])/gi, 408 function($0, $1){ 409 return replacers($1) || $0; 410 } 411 ).replace(/\((?!\?)/g, '(?:') // make all groups non-capturing 412 .replace(/ (?!\?|\*)/g, ',? ') // be forgiving with spaces and commas 413 .replace(/%([a-z%])/gi, 414 function($0, $1){ 415 var p = keys[$1]; 416 if (!p) return $1; 417 parsed.push($1); 418 return '(' + p.source + ')'; 419 } 420 ).replace(/\[a-z\]/gi, '[a-z\\u00c0-\\uffff]'); // handle unicode words 421 422 return { 423 format: format, 424 re: new RegExp('^' + re + '$', 'i'), 425 handler: function(bits){ 426 bits = bits.slice(1).associate(parsed); 427 var date = new Date().clearTime(), 428 year = bits.y || bits.Y; 429 430 if (year != null) handle.call(date, 'y', year); // need to start in the right year 431 if ('d' in bits) handle.call(date, 'd', 1); 432 if ('m' in bits || 'b' in bits || 'B' in bits) handle.call(date, 'm', 1); 433 434 for (var key in bits) handle.call(date, key, bits[key]); 435 return date; 436 } 437 }; 438 }; 439 440 var handle = function(key, value){ 441 if (!value) return this; 442 443 switch(key){ 444 case 'a': case 'A': return this.set('day', Date.parseDay(value, true)); 445 case 'b': case 'B': return this.set('mo', Date.parseMonth(value, true)); 446 case 'd': return this.set('date', value); 447 case 'H': case 'I': return this.set('hr', value); 448 case 'm': return this.set('mo', value - 1); 449 case 'M': return this.set('min', value); 450 case 'p': return this.set('ampm', value.replace(/\./g, '')); 451 case 'S': return this.set('sec', value); 452 case 's': return this.set('ms', ('0.' + value) * 1000); 453 case 'w': return this.set('day', value); 454 case 'Y': return this.set('year', value); 455 case 'y': 456 value = +value; 457 if (value < 100) value += startCentury + (value < startYear ? 100 : 0); 458 return this.set('year', value); 459 case 'T': 460 if (value == 'Z') value = '+00'; 461 var offset = value.match(/([+-])(\d{2}):?(\d{2})?/); 462 offset = (offset[1] + '1') * (offset[2] * 60 + (+offset[3] || 0)) + this.getTimezoneOffset(); 463 return this.set('time', this - offset * 60000); 464 } 465 466 return this; 467 }; 468 469 Date.defineParsers( 470 '%Y([-./]%m([-./]%d((T| )%X)?)?)?', // "1999-12-31", "1999-12-31 11:59pm", "1999-12-31 23:59:59", ISO8601 471 '%Y%m%d(T%H(%M%S?)?)?', // "19991231", "19991231T1159", compact 472 '%x( %X)?', // "12/31", "12.31.99", "12-31-1999", "12/31/2008 11:59 PM" 473 '%d%o( %b( %Y)?)?( %X)?', // "31st", "31st December", "31 Dec 1999", "31 Dec 1999 11:59pm" 474 '%b( %d%o)?( %Y)?( %X)?', // Same as above with month and day switched 475 '%Y %b( %d%o( %X)?)?', // Same as above with year coming first 476 '%o %b %d %X %T %Y' // "Thu Oct 22 08:11:23 +0000 2009" 477 ); 478 479 MooTools.lang.addEvent('langChange', function(language){ 480 if (MooTools.lang.get('Date')) recompile(language); 481 }).fireEvent('langChange', MooTools.lang.getCurrentLanguage()); 482 483 })();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Nov 16 22:51:00 2010 | Cross-referenced by PHPXref 0.7 |