1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
function test_Date_toISOString(t) {
t.plan(3);
var date, str;
// check valid date
date = new Date(Date.UTC(2010, 10, 27, 18, 19, 15, 123));
str = OpenLayers.Date.toISOString(date);
t.eq(str, "2010-11-27T18:19:15.123Z", "valid date");
// check zero padding
date = new Date(Date.UTC(2010, 7, 7, 18, 9, 5, 12));
str = OpenLayers.Date.toISOString(date);
t.eq(str, "2010-08-07T18:09:05.012Z", "zero padding");
// check invalid date
date = new Date("foo");
try {
str = OpenLayers.Date.toISOString(date);
} catch (err) {
// some implementations throw RangeError
// see https://bugzilla.mozilla.org/show_bug.cgi?id=649575
if (err instanceof RangeError) {
str = "Invalid Date";
}
}
t.eq(str, "Invalid Date", "invalid date");
}
function test_Date_parse(t) {
t.plan(114);
var cases = {
"2000": {
year: 2000,
month: 0,
date: 1
},
"2005-10": {
year: 2005,
month: 9,
date: 1
},
"1971-07-23": {
year: 1971,
month: 6,
date: 23
},
"1801-11-20T04:30:15Z": {
year: 1801,
month: 10,
date: 20,
hour: 4,
minutes: 30,
seconds: 15
},
"1989-06-15T18:30:15.91Z": {
year: 1989,
month: 5,
date: 15,
hour: 18,
minutes: 30,
seconds: 15,
milliseconds: 910
},
"1989-06-15T18:30:15.091Z": {
year: 1989,
month: 5,
date: 15,
hour: 18,
minutes: 30,
seconds: 15,
milliseconds: 91
},
"1989-06-15T13:30:15.091-05": {
year: 1989,
month: 5,
date: 15,
hour: 18,
minutes: 30,
seconds: 15,
milliseconds: 91
},
"2010-08-06T15:21:25-06": { // MDT
year: 2010,
month: 7,
date: 6,
hour: 21,
minutes: 21,
seconds: 25
},
"2010-08-07T06:21:25+9": { // JSP
year: 2010,
month: 7,
date: 6,
hour: 21,
minutes: 21,
seconds: 25
},
"2010-08-07T02:51:25+05:30": { // IST
year: 2010,
month: 7,
date: 6,
hour: 21,
minutes: 21,
seconds: 25
},
"T21:51:25Z": {
hour: 21,
minutes: 51,
seconds: 25
},
"T02:51:25+05:30": { // IST
hour: 21,
minutes: 21,
seconds: 25
},
"T2:51:25.1234-7": { // lenient
hour: 9,
minutes: 51,
seconds: 25,
milliseconds: 123
},
"2000Z": { // lenient (Chrome accepts this)
year: 2000
},
"2000-02Z": { // lenient (Chrome accepts this)
year: 2000,
month: 1
},
"2000-04-15Z": { // lenient (Chrome accepts this)
year: 2000,
month: 3,
date: 15
}
};
var o, got, exp;
for (var str in cases) {
o = cases[str];
got = OpenLayers.Date.parse(str);
exp = new Date(Date.UTC(o.year || 0, o.month || 0, o.date || 1, o.hour || 0, o.minutes || 0, o.seconds || 0, o.milliseconds || 0));
if ("year" in o) {
t.eq(got.getUTCFullYear(), exp.getUTCFullYear(), str + ": correct UTCFullYear");
t.eq(got.getUTCMonth(), exp.getUTCMonth(), str + ": correct UTCMonth");
t.eq(got.getUTCDate(), exp.getUTCDate(), str + ": correct UTCDate");
} else {
t.ok(true, str + ": ECMA doesn't specify how years are handled in time only strings");
t.ok(true, str + ": ECMA doesn't specify how months are handled in time only strings");
t.ok(true, str + ": ECMA doesn't specify how days are handled in time only strings");
}
if ("hour" in o) {
t.eq(got.getUTCHours(), exp.getUTCHours(), str + ": correct UTCHours");
t.eq(got.getUTCMinutes(), exp.getUTCMinutes(), str + ": correct UTCMinutes");
t.eq(got.getUTCSeconds(), exp.getUTCSeconds(), str + ": correct UTCSeconds");
t.eq(got.getUTCMilliseconds(), exp.getUTCMilliseconds(), str + ": correct UTCMilliseconds");
} else {
t.ok(true, str + ": ECMA doesn't specify how hours are handled in date only strings");
t.ok(true, str + ": ECMA doesn't specify how minutes are handled in date only strings");
t.ok(true, str + ": ECMA doesn't specify how seconds are handled in date only strings");
t.ok(true, str + ": ECMA doesn't specify how milliseconds are handled in date only strings");
}
}
// check invalid date parsing
var invalid = OpenLayers.Date.parse("foo");
t.ok(invalid instanceof Date, "invalid is a date");
t.ok(isNaN(invalid.getTime()), "invalid has no time");
}
function test_regex(t) {
t.plan(1);
var regex = OpenLayers.Date.dateRegEx;
OpenLayers.Date.dateRegEx = /^(?:(-?\d{4})(?:-(\d{2})(?:-(\d{2}))?)?)?(?:(?:T(\d{1,2}):(\d{2}):(\d{2}(?:\.\d+)?)(Z|(?:[+-]\d{1,2}(?::(\d{2}))?)))|Z)?$/;
var date = OpenLayers.Date.parse("-0501-03-01T00:00:00.000Z");
t.ok(!isNaN(date.getTime()), "date with negative year is parsed when providing alternative regex");
OpenLayers.Date.dateRegEx = regex;
}
</script>
</head>
<body>
</body>
</html>
|