DateTime.js (6169B)
1 /*** 2 3 MochiKit.DateTime 1.4 4 5 See <http://mochikit.com/> for documentation, downloads, license, etc. 6 7 (c) 2005 Bob Ippolito. All rights Reserved. 8 9 ***/ 10 11 if (typeof(dojo) != 'undefined') { 12 dojo.provide('MochiKit.DateTime'); 13 } 14 15 if (typeof(MochiKit) == 'undefined') { 16 MochiKit = {}; 17 } 18 19 if (typeof(MochiKit.DateTime) == 'undefined') { 20 MochiKit.DateTime = {}; 21 } 22 23 MochiKit.DateTime.NAME = "MochiKit.DateTime"; 24 MochiKit.DateTime.VERSION = "1.4"; 25 MochiKit.DateTime.__repr__ = function () { 26 return "[" + this.NAME + " " + this.VERSION + "]"; 27 }; 28 MochiKit.DateTime.toString = function () { 29 return this.__repr__(); 30 }; 31 32 /** @id MochiKit.DateTime.isoDate */ 33 MochiKit.DateTime.isoDate = function (str) { 34 str = str + ""; 35 if (typeof(str) != "string" || str.length === 0) { 36 return null; 37 } 38 var iso = str.split('-'); 39 if (iso.length === 0) { 40 return null; 41 } 42 return new Date(iso[0], iso[1] - 1, iso[2]); 43 }; 44 45 MochiKit.DateTime._isoRegexp = /(\d{4,})(?:-(\d{1,2})(?:-(\d{1,2})(?:[T ](\d{1,2}):(\d{1,2})(?::(\d{1,2})(?:\.(\d+))?)?(?:(Z)|([+-])(\d{1,2})(?::(\d{1,2}))?)?)?)?)?/; 46 47 /** @id MochiKit.DateTime.isoTimestamp */ 48 MochiKit.DateTime.isoTimestamp = function (str) { 49 str = str + ""; 50 if (typeof(str) != "string" || str.length === 0) { 51 return null; 52 } 53 var res = str.match(MochiKit.DateTime._isoRegexp); 54 if (typeof(res) == "undefined" || res === null) { 55 return null; 56 } 57 var year, month, day, hour, min, sec, msec; 58 year = parseInt(res[1], 10); 59 if (typeof(res[2]) == "undefined" || res[2] === '') { 60 return new Date(year); 61 } 62 month = parseInt(res[2], 10) - 1; 63 day = parseInt(res[3], 10); 64 if (typeof(res[4]) == "undefined" || res[4] === '') { 65 return new Date(year, month, day); 66 } 67 hour = parseInt(res[4], 10); 68 min = parseInt(res[5], 10); 69 sec = (typeof(res[6]) != "undefined" && res[6] !== '') ? parseInt(res[6], 10) : 0; 70 if (typeof(res[7]) != "undefined" && res[7] !== '') { 71 msec = Math.round(1000.0 * parseFloat("0." + res[7])); 72 } else { 73 msec = 0; 74 } 75 if ((typeof(res[8]) == "undefined" || res[8] === '') && (typeof(res[9]) == "undefined" || res[9] === '')) { 76 return new Date(year, month, day, hour, min, sec, msec); 77 } 78 var ofs; 79 if (typeof(res[9]) != "undefined" && res[9] !== '') { 80 ofs = parseInt(res[10], 10) * 3600000; 81 if (typeof(res[11]) != "undefined" && res[11] !== '') { 82 ofs += parseInt(res[11], 10) * 60000; 83 } 84 if (res[9] == "-") { 85 ofs = -ofs; 86 } 87 } else { 88 ofs = 0; 89 } 90 return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs); 91 }; 92 93 /** @id MochiKit.DateTime.toISOTime */ 94 MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) { 95 if (typeof(date) == "undefined" || date === null) { 96 return null; 97 } 98 var hh = date.getHours(); 99 var mm = date.getMinutes(); 100 var ss = date.getSeconds(); 101 var lst = [ 102 ((realISO && (hh < 10)) ? "0" + hh : hh), 103 ((mm < 10) ? "0" + mm : mm), 104 ((ss < 10) ? "0" + ss : ss) 105 ]; 106 return lst.join(":"); 107 }; 108 109 /** @id MochiKit.DateTime.toISOTimeStamp */ 110 MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) { 111 if (typeof(date) == "undefined" || date === null) { 112 return null; 113 } 114 var sep = realISO ? "T" : " "; 115 var foot = realISO ? "Z" : ""; 116 if (realISO) { 117 date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000)); 118 } 119 return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot; 120 }; 121 122 /** @id MochiKit.DateTime.toISODate */ 123 MochiKit.DateTime.toISODate = function (date) { 124 if (typeof(date) == "undefined" || date === null) { 125 return null; 126 } 127 var _padTwo = MochiKit.DateTime._padTwo; 128 return [ 129 date.getFullYear(), 130 _padTwo(date.getMonth() + 1), 131 _padTwo(date.getDate()) 132 ].join("-"); 133 }; 134 135 /** @id MochiKit.DateTime.americanDate */ 136 MochiKit.DateTime.americanDate = function (d) { 137 d = d + ""; 138 if (typeof(d) != "string" || d.length === 0) { 139 return null; 140 } 141 var a = d.split('/'); 142 return new Date(a[2], a[0] - 1, a[1]); 143 }; 144 145 MochiKit.DateTime._padTwo = function (n) { 146 return (n > 9) ? n : "0" + n; 147 }; 148 149 /** @id MochiKit.DateTime.toPaddedAmericanDate */ 150 MochiKit.DateTime.toPaddedAmericanDate = function (d) { 151 if (typeof(d) == "undefined" || d === null) { 152 return null; 153 } 154 var _padTwo = MochiKit.DateTime._padTwo; 155 return [ 156 _padTwo(d.getMonth() + 1), 157 _padTwo(d.getDate()), 158 d.getFullYear() 159 ].join('/'); 160 }; 161 162 /** @id MochiKit.DateTime.toAmericanDate */ 163 MochiKit.DateTime.toAmericanDate = function (d) { 164 if (typeof(d) == "undefined" || d === null) { 165 return null; 166 } 167 return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/'); 168 }; 169 170 MochiKit.DateTime.EXPORT = [ 171 "isoDate", 172 "isoTimestamp", 173 "toISOTime", 174 "toISOTimestamp", 175 "toISODate", 176 "americanDate", 177 "toPaddedAmericanDate", 178 "toAmericanDate" 179 ]; 180 181 MochiKit.DateTime.EXPORT_OK = []; 182 MochiKit.DateTime.EXPORT_TAGS = { 183 ":common": MochiKit.DateTime.EXPORT, 184 ":all": MochiKit.DateTime.EXPORT 185 }; 186 187 MochiKit.DateTime.__new__ = function () { 188 // MochiKit.Base.nameFunctions(this); 189 var base = this.NAME + "."; 190 for (var k in this) { 191 var o = this[k]; 192 if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { 193 try { 194 o.NAME = base + k; 195 } catch (e) { 196 // pass 197 } 198 } 199 } 200 }; 201 202 MochiKit.DateTime.__new__(); 203 204 if (typeof(MochiKit.Base) != "undefined") { 205 MochiKit.Base._exportSymbols(this, MochiKit.DateTime); 206 } else { 207 (function (globals, module) { 208 if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') 209 || (MochiKit.__export__ === false)) { 210 var all = module.EXPORT_TAGS[":all"]; 211 for (var i = 0; i < all.length; i++) { 212 globals[all[i]] = module[all[i]]; 213 } 214 } 215 })(this, MochiKit.DateTime); 216 }