1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mochitest/MochiKit/DateTime.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,216 @@ 1.4 +/*** 1.5 + 1.6 +MochiKit.DateTime 1.4 1.7 + 1.8 +See <http://mochikit.com/> for documentation, downloads, license, etc. 1.9 + 1.10 +(c) 2005 Bob Ippolito. All rights Reserved. 1.11 + 1.12 +***/ 1.13 + 1.14 +if (typeof(dojo) != 'undefined') { 1.15 + dojo.provide('MochiKit.DateTime'); 1.16 +} 1.17 + 1.18 +if (typeof(MochiKit) == 'undefined') { 1.19 + MochiKit = {}; 1.20 +} 1.21 + 1.22 +if (typeof(MochiKit.DateTime) == 'undefined') { 1.23 + MochiKit.DateTime = {}; 1.24 +} 1.25 + 1.26 +MochiKit.DateTime.NAME = "MochiKit.DateTime"; 1.27 +MochiKit.DateTime.VERSION = "1.4"; 1.28 +MochiKit.DateTime.__repr__ = function () { 1.29 + return "[" + this.NAME + " " + this.VERSION + "]"; 1.30 +}; 1.31 +MochiKit.DateTime.toString = function () { 1.32 + return this.__repr__(); 1.33 +}; 1.34 + 1.35 +/** @id MochiKit.DateTime.isoDate */ 1.36 +MochiKit.DateTime.isoDate = function (str) { 1.37 + str = str + ""; 1.38 + if (typeof(str) != "string" || str.length === 0) { 1.39 + return null; 1.40 + } 1.41 + var iso = str.split('-'); 1.42 + if (iso.length === 0) { 1.43 + return null; 1.44 + } 1.45 + return new Date(iso[0], iso[1] - 1, iso[2]); 1.46 +}; 1.47 + 1.48 +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}))?)?)?)?)?/; 1.49 + 1.50 +/** @id MochiKit.DateTime.isoTimestamp */ 1.51 +MochiKit.DateTime.isoTimestamp = function (str) { 1.52 + str = str + ""; 1.53 + if (typeof(str) != "string" || str.length === 0) { 1.54 + return null; 1.55 + } 1.56 + var res = str.match(MochiKit.DateTime._isoRegexp); 1.57 + if (typeof(res) == "undefined" || res === null) { 1.58 + return null; 1.59 + } 1.60 + var year, month, day, hour, min, sec, msec; 1.61 + year = parseInt(res[1], 10); 1.62 + if (typeof(res[2]) == "undefined" || res[2] === '') { 1.63 + return new Date(year); 1.64 + } 1.65 + month = parseInt(res[2], 10) - 1; 1.66 + day = parseInt(res[3], 10); 1.67 + if (typeof(res[4]) == "undefined" || res[4] === '') { 1.68 + return new Date(year, month, day); 1.69 + } 1.70 + hour = parseInt(res[4], 10); 1.71 + min = parseInt(res[5], 10); 1.72 + sec = (typeof(res[6]) != "undefined" && res[6] !== '') ? parseInt(res[6], 10) : 0; 1.73 + if (typeof(res[7]) != "undefined" && res[7] !== '') { 1.74 + msec = Math.round(1000.0 * parseFloat("0." + res[7])); 1.75 + } else { 1.76 + msec = 0; 1.77 + } 1.78 + if ((typeof(res[8]) == "undefined" || res[8] === '') && (typeof(res[9]) == "undefined" || res[9] === '')) { 1.79 + return new Date(year, month, day, hour, min, sec, msec); 1.80 + } 1.81 + var ofs; 1.82 + if (typeof(res[9]) != "undefined" && res[9] !== '') { 1.83 + ofs = parseInt(res[10], 10) * 3600000; 1.84 + if (typeof(res[11]) != "undefined" && res[11] !== '') { 1.85 + ofs += parseInt(res[11], 10) * 60000; 1.86 + } 1.87 + if (res[9] == "-") { 1.88 + ofs = -ofs; 1.89 + } 1.90 + } else { 1.91 + ofs = 0; 1.92 + } 1.93 + return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs); 1.94 +}; 1.95 + 1.96 +/** @id MochiKit.DateTime.toISOTime */ 1.97 +MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) { 1.98 + if (typeof(date) == "undefined" || date === null) { 1.99 + return null; 1.100 + } 1.101 + var hh = date.getHours(); 1.102 + var mm = date.getMinutes(); 1.103 + var ss = date.getSeconds(); 1.104 + var lst = [ 1.105 + ((realISO && (hh < 10)) ? "0" + hh : hh), 1.106 + ((mm < 10) ? "0" + mm : mm), 1.107 + ((ss < 10) ? "0" + ss : ss) 1.108 + ]; 1.109 + return lst.join(":"); 1.110 +}; 1.111 + 1.112 +/** @id MochiKit.DateTime.toISOTimeStamp */ 1.113 +MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) { 1.114 + if (typeof(date) == "undefined" || date === null) { 1.115 + return null; 1.116 + } 1.117 + var sep = realISO ? "T" : " "; 1.118 + var foot = realISO ? "Z" : ""; 1.119 + if (realISO) { 1.120 + date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000)); 1.121 + } 1.122 + return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot; 1.123 +}; 1.124 + 1.125 +/** @id MochiKit.DateTime.toISODate */ 1.126 +MochiKit.DateTime.toISODate = function (date) { 1.127 + if (typeof(date) == "undefined" || date === null) { 1.128 + return null; 1.129 + } 1.130 + var _padTwo = MochiKit.DateTime._padTwo; 1.131 + return [ 1.132 + date.getFullYear(), 1.133 + _padTwo(date.getMonth() + 1), 1.134 + _padTwo(date.getDate()) 1.135 + ].join("-"); 1.136 +}; 1.137 + 1.138 +/** @id MochiKit.DateTime.americanDate */ 1.139 +MochiKit.DateTime.americanDate = function (d) { 1.140 + d = d + ""; 1.141 + if (typeof(d) != "string" || d.length === 0) { 1.142 + return null; 1.143 + } 1.144 + var a = d.split('/'); 1.145 + return new Date(a[2], a[0] - 1, a[1]); 1.146 +}; 1.147 + 1.148 +MochiKit.DateTime._padTwo = function (n) { 1.149 + return (n > 9) ? n : "0" + n; 1.150 +}; 1.151 + 1.152 +/** @id MochiKit.DateTime.toPaddedAmericanDate */ 1.153 +MochiKit.DateTime.toPaddedAmericanDate = function (d) { 1.154 + if (typeof(d) == "undefined" || d === null) { 1.155 + return null; 1.156 + } 1.157 + var _padTwo = MochiKit.DateTime._padTwo; 1.158 + return [ 1.159 + _padTwo(d.getMonth() + 1), 1.160 + _padTwo(d.getDate()), 1.161 + d.getFullYear() 1.162 + ].join('/'); 1.163 +}; 1.164 + 1.165 +/** @id MochiKit.DateTime.toAmericanDate */ 1.166 +MochiKit.DateTime.toAmericanDate = function (d) { 1.167 + if (typeof(d) == "undefined" || d === null) { 1.168 + return null; 1.169 + } 1.170 + return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/'); 1.171 +}; 1.172 + 1.173 +MochiKit.DateTime.EXPORT = [ 1.174 + "isoDate", 1.175 + "isoTimestamp", 1.176 + "toISOTime", 1.177 + "toISOTimestamp", 1.178 + "toISODate", 1.179 + "americanDate", 1.180 + "toPaddedAmericanDate", 1.181 + "toAmericanDate" 1.182 +]; 1.183 + 1.184 +MochiKit.DateTime.EXPORT_OK = []; 1.185 +MochiKit.DateTime.EXPORT_TAGS = { 1.186 + ":common": MochiKit.DateTime.EXPORT, 1.187 + ":all": MochiKit.DateTime.EXPORT 1.188 +}; 1.189 + 1.190 +MochiKit.DateTime.__new__ = function () { 1.191 + // MochiKit.Base.nameFunctions(this); 1.192 + var base = this.NAME + "."; 1.193 + for (var k in this) { 1.194 + var o = this[k]; 1.195 + if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { 1.196 + try { 1.197 + o.NAME = base + k; 1.198 + } catch (e) { 1.199 + // pass 1.200 + } 1.201 + } 1.202 + } 1.203 +}; 1.204 + 1.205 +MochiKit.DateTime.__new__(); 1.206 + 1.207 +if (typeof(MochiKit.Base) != "undefined") { 1.208 + MochiKit.Base._exportSymbols(this, MochiKit.DateTime); 1.209 +} else { 1.210 + (function (globals, module) { 1.211 + if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') 1.212 + || (MochiKit.__export__ === false)) { 1.213 + var all = module.EXPORT_TAGS[":all"]; 1.214 + for (var i = 0; i < all.length; i++) { 1.215 + globals[all[i]] = module[all[i]]; 1.216 + } 1.217 + } 1.218 + })(this, MochiKit.DateTime); 1.219 +}