forked from vhkrausser/moment-hijri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
494 lines (425 loc) · 17.7 KB
/
test.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
'use strict';
var chai = require('chai')
, moment = require('./moment-hijri')
chai.should()
moment.locale('en'
, { week:
{ dow: 6
, doy: 12
}
, longDateFormat:
{ LTS: 'h:mm:ss A'
, LT: 'h:mm A'
, L: 'iYYYY/iMM/iDD'
, LL: 'iD iMMMM iYYYY'
, LLL: 'iD iMMMM iYYYY LT'
, LLLL: 'dddd, iD iMMMM iYYYY LT'
}
}
)
describe('moment', function() {
describe('#parse', function() {
it('should parse gregorian dates', function() {
var m = moment('1981/8/17 07:10:20', 'YYYY/M/D hh:mm:ss')
m.format('YYYY-MM-DD hh:mm:ss').should.be.equal('1981-08-17 07:10:20')
m.milliseconds().should.be.equal(0)
})
it('should parse correctly when input is only time', function() {
var m = moment('07:10:20', 'hh:mm:ss')
m.format('YYYY-MM-DD hh:mm:ss').should.be.equal('0000-01-01 07:10:20')
})
it('should parse when only Hijri year is in the format', function() {
var m = moment('08 1436 17', 'MM iYYYY DD')
m.format('YYYY-MM-DD').should.be.equal('2014-08-17')
m = moment('08 36 17', 'MM iYY DD')
m.format('YYYY-MM-DD').should.be.equal('2014-08-17')
})
it('should parse when only Hijri month is in the format', function() {
var m = moment('1981 5 17', 'YYYY iM D')
m.format('YYYY-MM-DD').should.be.equal('1981-03-17')
})
it('should parse when only Hijri month is in the format', function() {
var m = moment('1990 08 25', 'YYYY iM D')
m.format('YYYY-MM-DD').should.be.equal('1990-03-25')
})
it('should parse when only Hijri month string is in the format', function() {
var m = moment('1981 Raj 17', 'YYYY iMMM D')
m.format('YYYY-MM-DD').should.be.equal('1981-03-17')
m = moment('1981 Rajab 17', 'YYYY iMMMM D')
m.format('YYYY-MM-DD').should.be.equal('1981-03-17')
})
it('should parse when only Hijri month string is in the format', function() {
var m = moment('1990 Sha 25', 'YYYY iMMM D')
m.format('YYYY-MM-DD').should.be.equal('1990-03-25')
m = moment('1990 Sha’ban 25', 'YYYY iMMMM D')
m.format('YYYY-MM-DD').should.be.equal('1990-03-25')
})
it('should parse when only Hijri date is in the format', function() {
var m = moment('1981 26 8', 'YYYY iD M')
m.format('YYYY-MM-DD').should.be.equal('1981-08-01')
})
it('should parse when Hijri year and month are in the format', function() {
var m = moment('17 1436 5', 'D iYYYY iM')
m.format('YYYY-MM-DD').should.be.equal('2015-02-17')
m = moment('1432 4', 'iYYYY iM')
m.format('YYYY-MM-DD').should.be.equal('2011-03-06')
})
it('should parse when Hijri year and date are in the format', function() {
var m = moment('26 1436 8', 'iD iYYYY M')
m.format('YYYY-MM-DD').should.be.equal('2014-08-19')
})
it('should parse when Hijri month and date are in the format', function() {
var m = moment('26 1981 5', 'iD YYYY iM')
m.format('YYYY-MM-DD').should.be.equal('1981-03-01')
})
it('should parse when Hijri year, month and date are in the format', function() {
var m = moment('26 1430 5', 'iD iYYYY iM')
m.format('YYYY-MM-DD').should.be.equal('2009-05-21')
})
it('should parse with complex format', function() {
var m = moment('17 26 50 2014 50 8 12', 'D iD iYYYY YYYY M M jM')
m.format('YYYY-MM-DD').should.be.equal('2014-12-17')
})
it('should parse format result', function() {
var f = 'iYYYY/iM/iD hh:mm:ss.SSS a'
, m = moment()
moment(m.format(f), f).isSame(m).should.be.true
})
it('should be able to parse in utc', function() {
var m = moment.utc('1436/8/20 07:10:20', 'iYYYY/iM/iD hh:mm:ss')
m.format('YYYY-MM-DD hh:mm:ss Z').should.be.equal('2015-06-07 07:10:20 +00:00')
})
it('should parse with a format array', function() {
var p1 = 'iYY iM iD'
, p2 = 'iM iD iYY'
, p3 = 'iD iYY iM'
, m;
m = moment('60 11 12', ['D YY M', 'M D YY', 'YY M D']);
m.format('YY-MM-DD').should.be.equal('60-11-12')
m = moment('10 11 12', [p1, p2, p3])
m.format('iYY-iMM-iDD').should.be.equal('10-11-12')
m = moment('10 11 12', [p2, p3, p1])
m.format('iYY-iMM-iDD').should.be.equal('12-10-11')
m = moment('10 11 12', [p3, p1, p2])
m.format('iYY-iMM-iDD').should.be.equal('11-12-10')
m = moment('10 11 12', [p3, p2, p1])
m.format('iYY-iMM-iDD').should.be.equal('11-12-10')
m = moment('60-11-12', [p3, p2, p1])
m.format('iYY-iMM-iDD').should.be.equal('60-11-12')
m = moment('60 11 12', [p3, p2, p1])
m.format('iYY-iMM-iDD').should.be.equal('60-11-12')
m = moment('60 8 31', ['YY M D', 'iYY iM iD'])
m.format('YY-MM-DD').should.be.equal('60-08-31')
m = moment('60 8 31', ['iYY iM iD', 'YY M D'])
m.format('YY-MM-DD').should.be.equal('60-08-31')
m = moment('60 5 31', ['YY M D', 'iYY iM iD'])
m.format('YY-MM-DD').should.be.equal('60-05-31')
m = moment('60 5 30', ['iYY iM iD', 'YY M D'])
m.format('iYY-iMM-iDD').should.be.equal('60-05-30')
})
})
describe('#format', function() {
it('should work normally when there is no Hijri token', function() {
var m = moment('1981-08-17 07:10:20', 'YYYY-MM-DD hh:mm:ss')
m.format('YYYY-MM-DD hh:mm:ss').should.be.equal('1981-08-17 07:10:20')
})
it('should format to Hijri with Hijri tokens', function() {
var m = moment('1981-08-17 07:10:20', 'YYYY-MM-DD hh:mm:ss')
m.format('iYYYY-iMM-iDD hh:mm:ss').should.be.equal('1401-10-17 07:10:20')
})
it('should format with escaped and unescaped tokens', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('[My] birt\\h y[ea]r [is] iYYYY or YYYY').should.be.equal('My birth year is 1401 or 1981')
})
it('should format with mixed tokens', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iYYYY/iMM/iDD = YYYY-MM-DD').should.be.equal('1401/10/17 = 1981-08-17')
})
it('should format with iMo', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iMo').should.be.equal('10th')
})
it('should format with iM', function() {
var m = moment('1981-05-17', 'YYYY-MM-DD')// Note: The date is different here
m.format('iM').should.be.equal('7')
})
it('should format with iMM', function() {
var m = moment('1981-05-17', 'YYYY-MM-DD')// Note: The date is different here
m.format('iMM').should.be.equal('07')
})
it('should format with iMMM', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iMMM').should.be.equal('Shw')
})
it('should format with iMMMM', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iMMMM').should.be.equal('Shawwal')
})
it('should format with iDo', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iDo').should.be.equal('17th')
})
it('should format with iD', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iD').should.be.equal('17')
})
it('should format with iDD', function() {
var m = moment('1981-05-17', 'YYYY-MM-DD')// Note: The date is different here
m.format('iDD').should.be.equal('13')
m = moment('1981-05-13', 'YYYY-MM-DD')// Note: The date is different here
m.format('iDD').should.be.equal('09')
})
it('should format with iDDD', function() {
var m = moment('1981-11-17', 'YYYY-MM-DD')// Note: The date is different here
m.format('iDDD').should.be.equal('21')
})
it('should format with iDDDo', function() {
var m = moment('1981-11-17', 'YYYY-MM-DD')
m.format('iDDDo').should.be.equal('21st')
})
it('should format with iDDDD', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iDDDD').should.be.equal('282')
m = moment('1981-11-17', 'YYYY-MM-DD')
m.format('iDDDD').should.be.equal('021')
})
it('should format with iwo', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iwo').should.be.equal('41st')
})
it('should format with iw', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iw').should.be.equal('41')
})
it('should format with iww', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iww').should.be.equal('41')
m = moment('1981-11-17', 'YYYY-MM-DD')
m.format('iww').should.be.equal('04')
})
it('should format with iYY', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iYY').should.be.equal('01')
})
it('should format with iYYYY', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iYYYY').should.be.equal('1401')
})
it('should format with iYYYYY', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iYYYYY').should.be.equal('01401')
})
it('should format with igg', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('igg').should.be.equal('01')
})
it('should format with igggg', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('igggg').should.be.equal('1401')
})
it('should format with iggggg', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.format('iggggg').should.be.equal('01401')
})
it('should work with long date formats too', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')// TODO: Not working
m.format('LTS').should.be.equal('12:00:00 AM')
m.format('LT').should.be.equal('12:00 AM')
m.format('L').should.be.equal('1401/10/17')
m.format('l').should.be.equal('1401/10/17')
m.format('LL').should.be.equal('17 Shawwal 1401')
m.format('ll').should.be.equal('17 Shw 1401')
m.format('LLL').should.be.equal('17 Shawwal 1401 12:00 AM')
m.format('lll').should.be.equal('17 Shw 1401 12:00 AM')
m.format('LLLL').should.be.equal('Monday, 17 Shawwal 1401 12:00 AM')
m.format('llll').should.be.equal('Mon, 17 Shw 1401 12:00 AM')
})
})
describe('#iConvert', function() {
it('should convert 1999-04-01 to 1419-12-15', function() {
var h = moment.iConvert.toHijri(1999, 3, 1);
h.hy.should.be.equal(1419);
h.hm.should.be.equal(11);
h.hd.should.be.equal(15);
});
it('should convert 1989-02-25 to 1409-07-19', function() {
var h = moment.iConvert.toHijri(1989, 1, 25);
h.hy.should.be.equal(1409);
h.hm.should.be.equal(6);
h.hd.should.be.equal(19);
})
it('should convert 1419-12-15 to 1999-04-01', function() {
var g = moment.iConvert.toGregorian(1419, 11, 15);
g.gy.should.be.equal(1999);
g.gm.should.be.equal(3);
g.gd.should.be.equal(1);
});
it('should convert 1409-07-19 to 1989-02-25', function() {
var g = moment.iConvert.toGregorian(1409, 6, 19);
g.gy.should.be.equal(1989);
g.gm.should.be.equal(1);
g.gd.should.be.equal(25);
})
})
describe('#startOf', function() {
it('should work as expected without Hijri units', function() {
var m = moment('2015-04-03 07:10:20')
m.startOf('year').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-01-01 00:00:00')
m = moment('2015-04-03 07:10:20')
m.startOf('month').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-04-01 00:00:00')
m = moment('2015-04-03 07:10:20')
m.startOf('day').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-04-03 00:00:00')
m = moment('2015-04-03 07:10:20')
m.startOf('week').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-03-28 00:00:00')
})
it('should return start of Hijri year, month and date', function() {
var m = moment('2015-04-03 07:10:20')
m.startOf('iYear').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-01-01 00:00:00')
m = moment('2015-04-03 07:10:20')
m.startOf('iMonth').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-06-01 00:00:00')
m = moment('2015-04-03 07:10:20')
m.startOf('day').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-06-14 00:00:00')
m = moment('2015-04-03 07:10:20')
m.startOf('week').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-06-08 00:00:00')
})
})
describe('#endOf', function() {
it('should work as expected without Hijri units', function() {
var m;
m = moment(new Date(2015, 1, 2, 3, 4, 5, 6))
m.endOf('year').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-12-31 23:59:59')
m = moment(new Date(2015, 1, 2, 3, 4, 5, 6))
m.endOf('month').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-02-28 23:59:59')
m = moment(new Date(2015, 1, 2, 3, 4, 5, 6))
m.endOf('day').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-02-02 23:59:59')
m = moment(new Date(2015, 1, 2, 3, 4, 5, 6))
m.endOf('week').format('YYYY-MM-DD HH:mm:ss').should.be.equal('2015-02-06 23:59:59')
})
it('should return end of Hijri year, month and date', function() {
var m = moment('2015-04-03 07:10:20')
m.endOf('iYear').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-12-30 23:59:59')
m = moment('2015-04-03 07:10:20')
m.endOf('iMonth').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-06-30 23:59:59')
m = moment('2015-04-03 07:10:20')
m.endOf('day').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-06-14 23:59:59')
m = moment('2015-04-03 07:10:20')
m.endOf('week').format('iYYYY-iMM-iDD HH:mm:ss').should.be.equal('1436-06-14 23:59:59')
})
})
describe('#iYear', function() {
it('should return Hijri year', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iYear().should.be.equal(1401)
})
it('should set Hijri year', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iYear(1435)
m.format('iYYYY/iM/iD').should.be.equal('1435/10/17')
m = moment('2013-03-20', 'YYYY-MM-DD')
m.format('iYY/iM/iD').should.be.equal('34/5/8')
m.iYear(1392)
m.format('iYY/iM/iD').should.be.equal('92/5/8')
})
it('should also has iYears alias', function() {
moment.fn.iYear.should.be.equal(moment.fn.iYears)
})
})
describe('#iMonth', function() {
it('should return Hijri month', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iMonth().should.be.equal(9)
})
it('should set Hijri month', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iMonth(7)
m.format('iYYYY/iM/iD').should.be.equal('1401/8/17')
m = moment('2012-08-21', 'YYYY-MM-DD')
m.format('iYY/iM/iD').should.be.equal('33/10/3')
m.iMonth(11)
m.format('iYY/iM/iD').should.be.equal('33/12/3')
m = moment('2013-08-22', 'YYYY-MM-DD')
m.format('iYY/iM/iD').should.be.equal('34/10/15')
m.iMonth(11)
m.format('iYY/iM/iD').should.be.equal('34/12/15')
})
it('should also has iMonths alias', function() {
moment.fn.iMonth.should.be.equal(moment.fn.iMonths)
})
it('should set month by name and short name', function() {
var m = moment(new Date(2015, 0, 1))
m.iMonth('Shawwal')
m.format('iYYYY/iM/iD').should.be.equal('1436/10/10')
m = moment(new Date(2015, 0, 1))
m.iMonth('Safar')
m.format('iYYYY/iM/iD').should.be.equal('1436/2/10')
m = moment(new Date(2015, 0, 1))
m.iMonth('Jum-I')
m.format('iYYYY/iM/iD').should.be.equal('1436/5/10')
})
})
describe('#iDate', function() {
it('should return Hijri date', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iDate().should.be.equal(17)
})
it('should set Hijri date', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iDate(29)
m.format('iYYYY/iM/iD').should.be.equal('1401/10/29')
m = moment('1981-07-17', 'YYYY-MM-DD')
m.format('iYY/iM/iD').should.be.equal('01/9/16')
m.iDate(29)
m.format('iYY/iM/iD').should.be.equal('01/9/29')
m.iDate(30)
m.format('iYY/iM/iD').should.be.equal('01/9/30')
m.iDate(30)
m.format('iYY/iM/iD').should.be.equal('01/9/30')
m.iDate(31)
m.format('iYY/iM/iD').should.be.equal('01/10/1')
m.iDate(90)
m.format('iYY/iM/iD').should.be.equal('02/1/2')
})
it('should also has iDates alias', function() {
moment.fn.iDate.should.be.equal(moment.fn.iDates)
})
})
describe('#iDayOfYear', function() {
it('should return Hijri date of year', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iDayOfYear().should.be.equal(282)
m = moment('1980-11-9', 'YYYY-MM-DD')
m.iDayOfYear().should.be.equal(1)
m = moment('2013-11-03', 'YYYY-MM-DD')//1434
m.iDayOfYear().should.be.equal(354)
m = moment('2014-10-24', 'YYYY-MM-DD')//1435
m.iDayOfYear().should.be.equal(355)
m = moment('2014-10-25', 'YYYY-MM-DD')//1436
m.iDayOfYear().should.be.equal(1)
})
it('should set Hijri date of year', function() {
var m = moment('2014-10-24', 'YYYY-MM-DD')
m.iDayOfYear(30)
m.format('iYYYY/iM/iD').should.be.equal('1435/1/30')
m.iDayOfYear(354)
m.format('iYY/iM/iD').should.be.equal('35/12/29')
m.iDayOfYear(355)
m.format('iYY/iM/iD').should.be.equal('35/12/30')
m.iDayOfYear(356)
m.format('iYY/iM/iD').should.be.equal('36/1/1')
m.iDayOfYear(1)
m.format('iYY/iM/iD').should.be.equal('36/1/1')
m.iDayOfYear(90)
m.format('iYY/iM/iD').should.be.equal('36/4/2')
m.iDayOfYear(354 + 354)
m.format('iYY/iM/iD').should.be.equal('37/12/30')
})
})
describe('#iDaysInMonth', function() {
it('should return Hijri days in Month', function() {
var m = moment('1981-08-17', 'YYYY-MM-DD')
m.iDaysInMonth().should.be.equal(29)
m = moment('1986-2-2', 'YYYY-MM-DD')
m.iDaysInMonth().should.be.equal(30)
})
})
})