testing/mochitest/tests/MochiKit-1.4.2/MochiKit/DateTime.js

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

mercurial