5 See <http://mochikit.com/> for documentation, downloads, license, etc.
7 (c) 2005 Bob Ippolito. All rights Reserved.
11 if (typeof(dojo) != 'undefined') {
12 dojo.provide('MochiKit.DateTime');
15 if (typeof(MochiKit) == 'undefined') {
19 if (typeof(MochiKit.DateTime) == 'undefined') {
20 MochiKit.DateTime = {};
23 MochiKit.DateTime.NAME = "MochiKit.DateTime";
24 MochiKit.DateTime.VERSION = "1.4";
25 MochiKit.DateTime.__repr__ = function () {
26 return "[" + this.NAME + " " + this.VERSION + "]";
28 MochiKit.DateTime.toString = function () {
29 return this.__repr__();
32 /** @id MochiKit.DateTime.isoDate */
33 MochiKit.DateTime.isoDate = function (str) {
35 if (typeof(str) != "string" || str.length === 0) {
38 var iso = str.split('-');
39 if (iso.length === 0) {
42 return new Date(iso[0], iso[1] - 1, iso[2]);
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}))?)?)?)?)?/;
47 /** @id MochiKit.DateTime.isoTimestamp */
48 MochiKit.DateTime.isoTimestamp = function (str) {
50 if (typeof(str) != "string" || str.length === 0) {
53 var res = str.match(MochiKit.DateTime._isoRegexp);
54 if (typeof(res) == "undefined" || res === null) {
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);
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);
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]));
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);
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;
90 return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs);
93 /** @id MochiKit.DateTime.toISOTime */
94 MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) {
95 if (typeof(date) == "undefined" || date === null) {
98 var hh = date.getHours();
99 var mm = date.getMinutes();
100 var ss = date.getSeconds();
102 ((realISO && (hh < 10)) ? "0" + hh : hh),
103 ((mm < 10) ? "0" + mm : mm),
104 ((ss < 10) ? "0" + ss : ss)
106 return lst.join(":");
109 /** @id MochiKit.DateTime.toISOTimeStamp */
110 MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) {
111 if (typeof(date) == "undefined" || date === null) {
114 var sep = realISO ? "T" : " ";
115 var foot = realISO ? "Z" : "";
117 date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000));
119 return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot;
122 /** @id MochiKit.DateTime.toISODate */
123 MochiKit.DateTime.toISODate = function (date) {
124 if (typeof(date) == "undefined" || date === null) {
127 var _padTwo = MochiKit.DateTime._padTwo;
130 _padTwo(date.getMonth() + 1),
131 _padTwo(date.getDate())
135 /** @id MochiKit.DateTime.americanDate */
136 MochiKit.DateTime.americanDate = function (d) {
138 if (typeof(d) != "string" || d.length === 0) {
141 var a = d.split('/');
142 return new Date(a[2], a[0] - 1, a[1]);
145 MochiKit.DateTime._padTwo = function (n) {
146 return (n > 9) ? n : "0" + n;
149 /** @id MochiKit.DateTime.toPaddedAmericanDate */
150 MochiKit.DateTime.toPaddedAmericanDate = function (d) {
151 if (typeof(d) == "undefined" || d === null) {
154 var _padTwo = MochiKit.DateTime._padTwo;
156 _padTwo(d.getMonth() + 1),
157 _padTwo(d.getDate()),
162 /** @id MochiKit.DateTime.toAmericanDate */
163 MochiKit.DateTime.toAmericanDate = function (d) {
164 if (typeof(d) == "undefined" || d === null) {
167 return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/');
170 MochiKit.DateTime.EXPORT = [
177 "toPaddedAmericanDate",
181 MochiKit.DateTime.EXPORT_OK = [];
182 MochiKit.DateTime.EXPORT_TAGS = {
183 ":common": MochiKit.DateTime.EXPORT,
184 ":all": MochiKit.DateTime.EXPORT
187 MochiKit.DateTime.__new__ = function () {
188 // MochiKit.Base.nameFunctions(this);
189 var base = this.NAME + ".";
190 for (var k in this) {
192 if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
202 MochiKit.DateTime.__new__();
204 if (typeof(MochiKit.Base) != "undefined") {
205 MochiKit.Base._exportSymbols(this, MochiKit.DateTime);
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]];
215 })(this, MochiKit.DateTime);