michael@0: /*** michael@0: michael@0: MochiKit.DateTime 1.4.2 michael@0: michael@0: See for documentation, downloads, license, etc. michael@0: michael@0: (c) 2005 Bob Ippolito. All rights Reserved. michael@0: michael@0: ***/ michael@0: michael@0: MochiKit.Base._deps('DateTime', ['Base']); michael@0: michael@0: MochiKit.DateTime.NAME = "MochiKit.DateTime"; michael@0: MochiKit.DateTime.VERSION = "1.4.2"; michael@0: MochiKit.DateTime.__repr__ = function () { michael@0: return "[" + this.NAME + " " + this.VERSION + "]"; michael@0: }; michael@0: MochiKit.DateTime.toString = function () { michael@0: return this.__repr__(); michael@0: }; michael@0: michael@0: /** @id MochiKit.DateTime.isoDate */ michael@0: MochiKit.DateTime.isoDate = function (str) { michael@0: str = str + ""; michael@0: if (typeof(str) != "string" || str.length === 0) { michael@0: return null; michael@0: } michael@0: var iso = str.split('-'); michael@0: if (iso.length === 0) { michael@0: return null; michael@0: } michael@0: var date = new Date(iso[0], iso[1] - 1, iso[2]); michael@0: date.setFullYear(iso[0]); michael@0: date.setMonth(iso[1] - 1); michael@0: date.setDate(iso[2]); michael@0: return date; michael@0: }; michael@0: michael@0: 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}))?)?)?)?)?/; michael@0: michael@0: /** @id MochiKit.DateTime.isoTimestamp */ michael@0: MochiKit.DateTime.isoTimestamp = function (str) { michael@0: str = str + ""; michael@0: if (typeof(str) != "string" || str.length === 0) { michael@0: return null; michael@0: } michael@0: var res = str.match(MochiKit.DateTime._isoRegexp); michael@0: if (typeof(res) == "undefined" || res === null) { michael@0: return null; michael@0: } michael@0: var year, month, day, hour, min, sec, msec; michael@0: year = parseInt(res[1], 10); michael@0: if (typeof(res[2]) == "undefined" || res[2] === '') { michael@0: return new Date(year); michael@0: } michael@0: month = parseInt(res[2], 10) - 1; michael@0: day = parseInt(res[3], 10); michael@0: if (typeof(res[4]) == "undefined" || res[4] === '') { michael@0: return new Date(year, month, day); michael@0: } michael@0: hour = parseInt(res[4], 10); michael@0: min = parseInt(res[5], 10); michael@0: sec = (typeof(res[6]) != "undefined" && res[6] !== '') ? parseInt(res[6], 10) : 0; michael@0: if (typeof(res[7]) != "undefined" && res[7] !== '') { michael@0: msec = Math.round(1000.0 * parseFloat("0." + res[7])); michael@0: } else { michael@0: msec = 0; michael@0: } michael@0: if ((typeof(res[8]) == "undefined" || res[8] === '') && (typeof(res[9]) == "undefined" || res[9] === '')) { michael@0: return new Date(year, month, day, hour, min, sec, msec); michael@0: } michael@0: var ofs; michael@0: if (typeof(res[9]) != "undefined" && res[9] !== '') { michael@0: ofs = parseInt(res[10], 10) * 3600000; michael@0: if (typeof(res[11]) != "undefined" && res[11] !== '') { michael@0: ofs += parseInt(res[11], 10) * 60000; michael@0: } michael@0: if (res[9] == "-") { michael@0: ofs = -ofs; michael@0: } michael@0: } else { michael@0: ofs = 0; michael@0: } michael@0: return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs); michael@0: }; michael@0: michael@0: /** @id MochiKit.DateTime.toISOTime */ michael@0: MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) { michael@0: if (typeof(date) == "undefined" || date === null) { michael@0: return null; michael@0: } michael@0: var hh = date.getHours(); michael@0: var mm = date.getMinutes(); michael@0: var ss = date.getSeconds(); michael@0: var lst = [ michael@0: ((realISO && (hh < 10)) ? "0" + hh : hh), michael@0: ((mm < 10) ? "0" + mm : mm), michael@0: ((ss < 10) ? "0" + ss : ss) michael@0: ]; michael@0: return lst.join(":"); michael@0: }; michael@0: michael@0: /** @id MochiKit.DateTime.toISOTimeStamp */ michael@0: MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) { michael@0: if (typeof(date) == "undefined" || date === null) { michael@0: return null; michael@0: } michael@0: var sep = realISO ? "T" : " "; michael@0: var foot = realISO ? "Z" : ""; michael@0: if (realISO) { michael@0: date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000)); michael@0: } michael@0: return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot; michael@0: }; michael@0: michael@0: /** @id MochiKit.DateTime.toISODate */ michael@0: MochiKit.DateTime.toISODate = function (date) { michael@0: if (typeof(date) == "undefined" || date === null) { michael@0: return null; michael@0: } michael@0: var _padTwo = MochiKit.DateTime._padTwo; michael@0: var _padFour = MochiKit.DateTime._padFour; michael@0: return [ michael@0: _padFour(date.getFullYear()), michael@0: _padTwo(date.getMonth() + 1), michael@0: _padTwo(date.getDate()) michael@0: ].join("-"); michael@0: }; michael@0: michael@0: /** @id MochiKit.DateTime.americanDate */ michael@0: MochiKit.DateTime.americanDate = function (d) { michael@0: d = d + ""; michael@0: if (typeof(d) != "string" || d.length === 0) { michael@0: return null; michael@0: } michael@0: var a = d.split('/'); michael@0: return new Date(a[2], a[0] - 1, a[1]); michael@0: }; michael@0: michael@0: MochiKit.DateTime._padTwo = function (n) { michael@0: return (n > 9) ? n : "0" + n; michael@0: }; michael@0: michael@0: MochiKit.DateTime._padFour = function(n) { michael@0: switch(n.toString().length) { michael@0: case 1: return "000" + n; break; michael@0: case 2: return "00" + n; break; michael@0: case 3: return "0" + n; break; michael@0: case 4: michael@0: default: michael@0: return n; michael@0: } michael@0: }; michael@0: michael@0: /** @id MochiKit.DateTime.toPaddedAmericanDate */ michael@0: MochiKit.DateTime.toPaddedAmericanDate = function (d) { michael@0: if (typeof(d) == "undefined" || d === null) { michael@0: return null; michael@0: } michael@0: var _padTwo = MochiKit.DateTime._padTwo; michael@0: return [ michael@0: _padTwo(d.getMonth() + 1), michael@0: _padTwo(d.getDate()), michael@0: d.getFullYear() michael@0: ].join('/'); michael@0: }; michael@0: michael@0: /** @id MochiKit.DateTime.toAmericanDate */ michael@0: MochiKit.DateTime.toAmericanDate = function (d) { michael@0: if (typeof(d) == "undefined" || d === null) { michael@0: return null; michael@0: } michael@0: return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/'); michael@0: }; michael@0: michael@0: MochiKit.DateTime.EXPORT = [ michael@0: "isoDate", michael@0: "isoTimestamp", michael@0: "toISOTime", michael@0: "toISOTimestamp", michael@0: "toISODate", michael@0: "americanDate", michael@0: "toPaddedAmericanDate", michael@0: "toAmericanDate" michael@0: ]; michael@0: michael@0: MochiKit.DateTime.EXPORT_OK = []; michael@0: MochiKit.DateTime.EXPORT_TAGS = { michael@0: ":common": MochiKit.DateTime.EXPORT, michael@0: ":all": MochiKit.DateTime.EXPORT michael@0: }; michael@0: michael@0: MochiKit.DateTime.__new__ = function () { michael@0: // MochiKit.Base.nameFunctions(this); michael@0: var base = this.NAME + "."; michael@0: for (var k in this) { michael@0: var o = this[k]; michael@0: if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { michael@0: try { michael@0: o.NAME = base + k; michael@0: } catch (e) { michael@0: // pass michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: MochiKit.DateTime.__new__(); michael@0: michael@0: if (typeof(MochiKit.Base) != "undefined") { michael@0: MochiKit.Base._exportSymbols(this, MochiKit.DateTime); michael@0: } else { michael@0: (function (globals, module) { michael@0: if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') michael@0: || (MochiKit.__export__ === false)) { michael@0: var all = module.EXPORT_TAGS[":all"]; michael@0: for (var i = 0; i < all.length; i++) { michael@0: globals[all[i]] = module[all[i]]; michael@0: } michael@0: } michael@0: })(this, MochiKit.DateTime); michael@0: }