UNPKG

13.8 kBJavaScriptView Raw
1import { CraftAiTimeError } from '../src/errors';
2import { expect } from 'chai';
3import moment from 'moment';
4import momentTimezone from 'moment-timezone';
5import Time from '../src/time';
6
7describe('Time(...)', function() {
8 it('works with no parameters', function() {
9 expect(Time()).to.have.property('timestamp');
10 expect(Time()).to.have.property('day_of_week');
11 expect(Time()).to.have.property('time_of_day');
12 expect(Time()).to.have.property('day_of_month');
13 expect(Time()).to.have.property('month_of_year');
14 expect(Time()).to.have.property('timezone');
15
16 expect(new Time()).to.have.property('timestamp');
17 expect(new Time()).to.have.property('day_of_week');
18 expect(new Time()).to.have.property('time_of_day');
19 expect(new Time()).to.have.property('day_of_month');
20 expect(new Time()).to.have.property('month_of_year');
21 expect(new Time()).to.have.property('timezone');
22 });
23
24 it('creates instances of Time', function() {
25 expect(Time(5623879)).to.be.an.instanceof(Time);
26
27 expect(new Time('2016-06-30 05:30')).to.be.an.instanceof(Time);
28 });
29
30 describe('from unix timestamps', function() {
31 it('works with a plain timestamp', function() {
32 if (moment.unix(1465496929)
33 .utcOffset() === 120) {
34 // Test depends on locale
35 expect(Time(1465496929)).to.be.deep.equal({
36 utc: '2016-06-09T18:28:49.000Z',
37 timestamp: 1465496929,
38 day_of_week: 3,
39 day_of_month: 9,
40 month_of_year: 6,
41 time_of_day: 20.480277777777776,
42 timezone: '+02:00'
43 });
44 }
45 expect(Time(1465496929).timestamp).to.be.equal(1465496929);
46 expect(Time(230536800).timestamp).to.be.equal(230536800);
47 });
48
49 it('works with a timestamp as a float', function() {
50 expect(Time(1465496929.3).timestamp).to.be.equal(1465496929);
51 expect(Time(1465496867.9).timestamp).to.be.equal(1465496867);
52 expect(Time(230536800.0).timestamp).to.be.equal(230536800);
53 });
54
55 it('works with a timestamp and specified positive timezone', function() {
56 expect(Time(1465496929, '+02:00')).to.be.deep.equal({
57 utc: '2016-06-09T18:28:49.000Z',
58 timestamp: 1465496929,
59 day_of_week: 3,
60 day_of_month: 9,
61 month_of_year: 6,
62 time_of_day: 20.480277777777776,
63 timezone: '+02:00'
64 });
65 });
66
67 it('works with a timestamp and specified negative timezone', function() {
68 expect(Time(1465496929, '+10:00')).to.be.deep.equal({
69 utc: '2016-06-09T18:28:49.000Z',
70 timestamp: 1465496929,
71 day_of_week: 4,
72 day_of_month: 10,
73 month_of_year: 6,
74 time_of_day: 4.480277777777778,
75 timezone: '+10:00'
76 });
77 });
78 });
79
80 describe('from ISO 8601 strings', function() {
81 if (moment('2010-01-01T05:06:30')
82 .utcOffset() === 60) {
83 // Test depends on locale
84 it('works with a date having no specified timezone', function() {
85 expect(Time('2010-01-01T05:06:30')).to.be.deep.equal({
86 utc: '2010-01-01T04:06:30.000Z',
87 timestamp: 1262318790,
88 day_of_week: 4,
89 day_of_month: 1,
90 month_of_year: 1,
91 time_of_day: 5.108333333333333,
92 timezone: '+01:00'
93 });
94 });
95 }
96
97 if (moment('2010-01-01T05:06:30')
98 .utcOffset() === 60) {
99 // Test depends on locale
100 it('works with a date having no specified timezone and an explicit timezone', function() {
101 expect(Time('2010-01-01T05:06:30', '-10:00')).to.be.deep.equal({
102 utc: '2010-01-01T04:06:30.000Z',
103 timestamp: 1262318790,
104 day_of_week: 3,
105 day_of_month: 31,
106 month_of_year: 12,
107 time_of_day: 18.108333333333334,
108 timezone: '-10:00'
109 });
110 });
111 }
112
113 it('works with a date having a specified timezone', function() {
114 expect(Time('1977-04-22T01:00:00-05:00')).to.be.deep.equal({
115 utc: '1977-04-22T06:00:00.000Z',
116 timestamp: 230536800,
117 day_of_week: 4,
118 time_of_day: 1,
119 day_of_month: 22,
120 month_of_year: 4,
121 timezone: '-05:00'
122 });
123 });
124
125 it('works with a date having a specified UTC timezone', function() {
126 expect(Time('1977-04-22T01:00:00+00:00')).to.be.deep.equal({
127 utc: '1977-04-22T01:00:00.000Z',
128 timestamp: 230518800,
129 day_of_week: 4,
130 time_of_day: 1,
131 day_of_month: 22,
132 month_of_year: 4,
133 timezone: '+00:00'
134 });
135 });
136
137 it('works with a date having a specified timezone and an explicit timezone', function() {
138 expect(Time('1977-04-22T01:00:00-05:00', '+02:00')).to.be.deep.equal({
139 utc: '1977-04-22T06:00:00.000Z',
140 timestamp: 230536800,
141 day_of_week: 4,
142 time_of_day: 8,
143 day_of_month: 22,
144 month_of_year: 4,
145 timezone: '+02:00'
146 });
147 });
148 });
149
150 describe('from new Date(...)', function() {
151 if (moment(new Date('Wed Mar 25 2015 09:50:00 CDT'))
152 .utcOffset() === 60) {
153 // Test depends on locale
154 it('works with a date having a specified timezone', function() {
155 expect(Time(new Date('Wed Mar 25 2015 09:50:00 CDT'))).to.be.deep.equal({
156 utc: '2015-03-25T14:50:00.000Z',
157 timestamp: 1427295000,
158 day_of_week: 2,
159 time_of_day: 15.833333333333334,
160 day_of_month: 25,
161 month_of_year: 3,
162 timezone: '+01:00'
163 });
164 });
165 }
166
167 if (moment(new Date('Wed Mar 25 2015 09:50:00 CDT'))
168 .utcOffset() === 60) {
169 // Test depends on locale
170 it('works with a date having a specified timezone and a given timezone', function() {
171 expect(Time(new Date('Wed Mar 25 2015 09:50:00 CDT'), '+05:30')).to.be.deep.equal({
172 utc: '2015-03-25T14:50:00.000Z',
173 timestamp: 1427295000,
174 day_of_week: 2,
175 time_of_day: 20.333333333333332,
176 day_of_month: 25,
177 month_of_year: 3,
178 timezone: '+05:30'
179 });
180 });
181 }
182
183 if (moment('2015-03-29T09:15:30.000Z')
184 .utcOffset() === 120) {
185 // Test depends on locale
186 it('considers sunday to be the sixth day', function() {
187 expect(Time(new Date('Sun Mar 29 2015 10:15:30 GMT+0100 (W. Europe Standard Time)'))).to.be.deep.equal({
188 utc: '2015-03-29T09:15:30.000Z',
189 timestamp: 1427620530,
190 day_of_week: 6,
191 time_of_day: 11.258333333333333,
192 day_of_month: 29,
193 month_of_year: 3,
194 timezone: '+02:00'
195 });
196 });
197 }
198 });
199
200 describe('from a moment(...)', function() {
201 if (moment('2013-07-23')
202 .utcOffset() === 120) {
203 // Test depends on locale
204 it('works with a date having no specified timezone', function() {
205 expect(new Time(moment('2013-07-23'))).to.be.deep.equal({
206 utc: '2013-07-22T22:00:00.000Z',
207 timestamp: 1374530400,
208 day_of_week: 1,
209 day_of_month: 23,
210 month_of_year: 7,
211 time_of_day: 0,
212 timezone: '+02:00'
213 });
214 });
215 }
216
217 if (moment('2016-06-13 08:59')
218 .utcOffset() === 120) {
219 // Test depends on locale
220 it('works with a date having a given timezone', function() {
221 expect(new Time(moment('2016-06-13 08:59'), '+02:00')).to.be.deep.equal({
222 utc: '2016-06-13T06:59:00.000Z',
223 timestamp: 1465801140,
224 day_of_week: 0,
225 time_of_day: 8.983333333333333,
226 day_of_month: 13,
227 month_of_year: 6,
228 timezone: '+02:00'
229 });
230 });
231 }
232
233 it('works with a date having a specified timezone', function() {
234 expect(new Time(moment.parseZone('1977-04-22T01:00:00-05:00'))).to.be.deep.equal({
235 utc: '1977-04-22T06:00:00.000Z',
236 timestamp: 230536800,
237 day_of_week: 4,
238 day_of_month: 22,
239 month_of_year: 4,
240 time_of_day: 1,
241 timezone: '-05:00'
242 });
243 });
244
245 it('works with a date having a specified timezone and a given timezone', function() {
246 expect(new Time(moment.parseZone('2017-05-31T08:30:00+08:00'), '-08:00')).to.be.deep.equal({
247 utc: '2017-05-31T00:30:00.000Z',
248 timestamp: 1496190600,
249 day_of_week: 1,
250 day_of_month: 30,
251 month_of_year: 5,
252 time_of_day: 16.5,
253 timezone: '-08:00'
254 });
255 });
256 });
257
258 describe('from a momentTimezone(...)', function() {
259 it('works with a date having a specified timezone', function() {
260 expect(new Time(momentTimezone.tz('1977-04-22', 'America/Santiago'))).to.be.deep.equal({
261 utc: '1977-04-22T04:00:00.000Z',
262 timestamp: 230529600,
263 day_of_week: 4,
264 day_of_month: 22,
265 month_of_year: 4,
266 time_of_day: 0,
267 timezone: '-04:00'
268 });
269
270 expect(new Time(momentTimezone.tz('2017-02-01 12:30', 'Europe/Paris'))).to.be.deep.equal({
271 utc: '2017-02-01T11:30:00.000Z',
272 timestamp: 1485948600,
273 day_of_week: 2,
274 day_of_month: 1,
275 month_of_year: 2,
276 time_of_day: 12.5,
277 timezone: '+01:00'
278 });
279
280 expect(new Time(momentTimezone.tz('2017-05-01 12:30', 'Europe/Paris'))).to.be.deep.equal({
281 utc: '2017-05-01T10:30:00.000Z',
282 timestamp: 1493634600,
283 day_of_week: 0,
284 day_of_month: 1,
285 month_of_year: 5,
286 time_of_day: 12.5,
287 timezone: '+02:00'
288 });
289 });
290
291 it('works with a date having a specified timezone and a given timezone', function() {
292 expect(new Time(momentTimezone.tz('2017-05-31 12:45:00', 'Asia/Dubai'), '-08:00')).to.be.deep.equal({
293 utc: '2017-05-31T08:45:00.000Z',
294 timestamp: 1496220300,
295 day_of_week: 2,
296 day_of_month: 31,
297 month_of_year: 5,
298 time_of_day: 0.75,
299 timezone: '-08:00'
300 });
301 });
302 });
303
304 describe('from a Time(...)', function() {
305 it('works with a Time having a specified timezone', function() {
306 expect(new Time(Time('1977-04-22T01:00:00-05:00'))).to.be.deep.equal({
307 utc: '1977-04-22T06:00:00.000Z',
308 timestamp: 230536800,
309 day_of_week: 4,
310 time_of_day: 1,
311 day_of_month: 22,
312 month_of_year: 4,
313 timezone: '-05:00'
314 });
315 });
316
317 it('works with a Time having a specified timezone and a given timezone', function() {
318 expect(new Time(Time('1977-04-22T01:00:00-05:00'), '+02:30')).to.be.deep.equal({
319 utc: '1977-04-22T06:00:00.000Z',
320 timestamp: 230536800,
321 day_of_week: 4,
322 time_of_day: 8.5,
323 day_of_month: 22,
324 month_of_year: 4,
325 timezone: '+02:30'
326 });
327 });
328
329 it('works with a Time having a specified timezone and a given timezone (+hhmm)', function() {
330 expect(new Time(Time('1977-04-22T01:00:00-05:00'), '+0230')).to.be.deep.equal({
331 utc: '1977-04-22T06:00:00.000Z',
332 timestamp: 230536800,
333 day_of_week: 4,
334 time_of_day: 8.5,
335 day_of_month: 22,
336 month_of_year: 4,
337 timezone: '+02:30'
338 });
339 });
340
341 it('works with a Time having a specified timezone and a given timezone (+hh)', function() {
342 expect(new Time(Time('1977-04-22T01:00:00-05:00'), '+02')).to.be.deep.equal({
343 utc: '1977-04-22T06:00:00.000Z',
344 timestamp: 230536800,
345 day_of_week: 4,
346 time_of_day: 8,
347 day_of_month: 22,
348 month_of_year: 4,
349 timezone: '+02:00'
350 });
351 });
352
353 it('works with a Time having a specified timezone and a given timezone (CET)', function() {
354 expect(new Time(Time('1977-04-22T01:00:00-05:00'), 'CET')).to.be.deep.equal({
355 utc: '1977-04-22T06:00:00.000Z',
356 timestamp: 230536800,
357 day_of_week: 4,
358 time_of_day: 7,
359 day_of_month: 22,
360 month_of_year: 4,
361 timezone: '+01:00'
362 });
363 });
364
365 it('works with a Time having a specified timezone and a given timezone (320)', function() {
366 expect(new Time(Time('1977-04-22T01:00:00-05:00'), 330)).to.be.deep.equal({
367 utc: '1977-04-22T06:00:00.000Z',
368 timestamp: 230536800,
369 day_of_week: 4,
370 time_of_day: 11.5,
371 day_of_month: 22,
372 month_of_year: 4,
373 timezone: '+05:30'
374 });
375 });
376
377 it('works with a Time having a specified timezone and a given timezone (-320)', function() {
378 expect(new Time(Time('1977-04-22T01:00:00-05:00'), -330)).to.be.deep.equal({
379 utc: '1977-04-22T06:00:00.000Z',
380 timestamp: 230536800,
381 day_of_week: 4,
382 time_of_day: 0.5,
383 day_of_month: 22,
384 month_of_year: 4,
385 timezone: '-05:30'
386 });
387 });
388 });
389
390 describe('from anythings(...)', function() {
391 it('doesn\'t work with non time string', function() {
392 expect(() => Time('toto')).to.throw(CraftAiTimeError);
393 });
394
395 it('doesn\'t work with object', function() {
396 expect(() => Time({ toto: 'toto' })).to.throw(CraftAiTimeError);
397 });
398
399 it('doesn\'t work with array containing alphabetical string', function() {
400 expect(() => Time(['aaa152'])).to.throw(CraftAiTimeError);
401 });
402
403 it('doesn\'t work with a Time having an invalid specified timezone (900)', function() {
404 expect(() => new Time(Time('1977-04-22T01:00:00-05:00'), 900)).to.throw(CraftAiTimeError);
405 });
406
407 it('doesn\'t work with a Time having an invalid specified timezone (-730)', function() {
408 expect(() => new Time(Time('1977-04-22T01:00:00-05:00'), -730)).to.throw(CraftAiTimeError);
409 });
410
411 it('doesn\'t work with a Time having an invalid specified timezone ("aaaa")', function() {
412 expect(() => new Time(Time('1977-04-22T01:00:00-05:00'), 'aaaa')).to.throw(CraftAiTimeError);
413 });
414 });
415});