This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.html
209 lines (209 loc) · 5.71 KB
/
tests.html
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!doctype html>
<html>
<head>
<title>js-date-format tests</title>
<style type="text/css">
.failed {
background-color: #f00;
}
.passed {
background-color: #0f0;
}
</style>
<script type="text/javascript" src="js-date-format.js"></script>
<script type="text/javascript" src="locales/eo.js"></script>
<script type="text/javascript">
function testFormat(date, unit, format, start, end, test){
var passed = true;
document.write("<p>" + format + " " + start + "-" + end + ": ");
for(var i = start; i <= end; i++) {
if(date == null){
var d = new Date();
} else {
var d = new Date(date);
}
if(unit != null) {
d["set" + unit](i);
}
if(test(i) !== d.format(format)) {
document.write("<br/><span class='failed'>" + i + " failed: '" + test(i) + "' !== '" + d.format(format) + "'</span>");
passed = false;
}
}
if(passed){
document.write("<span class='passed'>Passed!</span>");
}
document.write("</p>");
}
function testMethod(date, method, value, expected, test){
var d;
document.write("<p>set" + method + "(" + value + "): ");
if(date == null){
d = new Date();
} else {
d = new Date(date);
}
d["set" + method](value);
if(test(d) === expected) {
document.write("<span class='passed'>Passed!</span>");
} else {
document.write("<br/><span class='failed'>failed: '" + test(d) + "' !== '" + expected + "'</span>");
}
document.write("</p>");
}
</script>
</head>
<body>
<h1>Tests</h1>
<script type="text/javascript">
testFormat(null, "Year", "YYYY", 1900, 2100, function(i){
return String(i);
});
testFormat(null, "Year", "YY", 1900, 2100, function(i){
return String(i % 100)
});
testFormat("1/1/2000", "Month", "M", 0, 11, function(i){
return String(i + 1);
});
testFormat("1/1/2000", "Month", "MM", 0, 11, function(i){
i = i + 1;
return String((i < 10)? "0" + i : i);
});
testFormat("1/1/2000", "Month", "MMM", 0, 11, function(i){
return Date.locales.en.month_names_short[i];
});
testFormat("1/1/2000", "Month", "MMMM", 0, 11, function(i){
return Date.locales.en.month_names[i];
});
testFormat("1/1/2014", "Date", "D", 1, 31, function(i){
return String(i);
});
testFormat("1/1/2014", "Date", "DD", 1, 31, function(i){
return String((i < 10)? "0" + i : i);
});
testFormat("1/1/2014", "Date", "DDD", 1, 31, function(i){
var d = new Date("1/1/2014");
d.setDate(i);
return Date.locales.en.day_names_short[d.getDay()];
});
testFormat("1/1/2014", "Date", "DDDD", 1, 31, function(i){
var d = new Date("1/1/2014");
d.setDate(i);
return Date.locales.en.day_names[d.getDay()];
});
testFormat("1/1/2014", "Date", "S", 1, 31, function(i){
var suffix = "";
var i10 = Math.floor(i/10);
var i1 = i % 10;
if(i10 == 1){
suffix = "th";
} else {
switch(i1) {
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default:
suffix = "th";
break;
}
}
return suffix;
});
testFormat(null, "Hours", "H", 0, 23, function(i){
return String(i);
});
testFormat(null, "Hours", "HH", 0, 23, function(i){
return String((i < 10)? "0" + i : i);
});
testFormat(null, "Hours", "h", 0, 23, function(i){
if(i == 0) {
i = 12
} else if(i > 12) {
i -= 12;
}
return String(i);
});
testFormat(null, "Hours", "hh", 0, 23, function(i){
if(i == 0) {
i = 12
} else if(i > 12) {
i -= 12;
}
return String((i < 10)? "0" + i : i);
});
testFormat(null, "Minutes", "m", 0, 59, function(i){
return String(i);
});
testFormat(null, "Minutes", "mm", 0, 59, function(i){
return String((i < 10)? "0" + i : i);
});
testFormat(null, "Seconds", "s", 0, 59, function(i){
return String(i);
});
testFormat(null, "Seconds", "ss", 0, 59, function(i){
return String((i < 10)? "0" + i : i);
});
testFormat(null, "Milliseconds", "f", 0, 999, function(i){
return String(Math.floor(i / 100));
});
testFormat(null, "Milliseconds", "ff", 0, 999, function(i){
var f = Math.floor(i / 10);
return String(f < 10? "0" + f : f);
});
testFormat(null, "Milliseconds", "fff", 0, 999, function(i){
return String(i < 10? "00" + i : (i < 100? "0" + i : i));
});
testFormat(null, null, "z", 0, 0, function(i){
var z = (new Date()).getTimezoneOffset();
var zh = -Math.floor(z / 60);
return String(zh);
});
testFormat(null, null, "zz", 0, 0, function(i){
var z = (new Date()).getTimezoneOffset();
var zh = -Math.floor(z / 60);
return String((zh < 0? "-" : "") + (Math.abs(zh) < 10? "0" : "") + Math.abs(zh));
});
testFormat(null, null, "zzz", 0, 0, function(i){
var z = (new Date()).getTimezoneOffset();
var zh = -Math.floor(z / 60);
var zm = Math.abs(z % 60);
return String(zh + ":" + (zm < 10? "0" : "") + zm);
});
testFormat(null, null, "zzzz", 0, 0, function(i){
var z = (new Date()).getTimezoneOffset();
var zh = -Math.floor(z / 60);
var zm = Math.abs(z % 60);
return String((zh < 0? "-" : "") + (Math.abs(zh) < 10? "0" : "") + Math.abs(zh) + ":" + (zm < 10? "0" : "") + zm);
});
testFormat(null, "Hours", "tt", 0, 23, function(i){
return (i < 12? "am" : "pm");
});
testFormat(null, "Hours", "t", 0, 23, function(i){
return (i < 12? "a" : "p");
});
testFormat(null, "Hours", "TT", 0, 23, function(i){
return (i < 12? "AM" : "PM");
});
testFormat(null, "Hours", "T", 0, 23, function(i){
return (i < 12? "A" : "P");
});
testMethod(null, "Locale", "eo", "A.T.M.", function(d){
d.setHours(1);
return d.getMeridiem(false);
});
testMethod(null, "Locale", "eo", "a.t.m.", function(d){
d.setHours(1);
return d.getMeridiem(true);
});
testMethod(null, "Locale", "eo", "a", function(d){
return d.getDateSuffix();
});
</script>
</body>
</html>