testing/mochitest/MochiKit/DateTime.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial