Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_datetime_date.d

raw
1 @safe pure unittest
2 {
3 import std.datetime.date;
4
5 assert(Date(2018, 10, 1).month == Month.oct);
6 assert(DateTime(1, 1, 1).month == Month.jan);
7 }
8
9 @safe pure unittest
10 {
11 import std.datetime.date;
12
13 assert(Date(2018, 10, 1).dayOfWeek == DayOfWeek.mon);
14 assert(DateTime(5, 5, 5).dayOfWeek == DayOfWeek.thu);
15 }
16
17 @safe unittest
18 {
19 import std.datetime.date;
20
21 assert(DateTime(Date(1999, 7, 6), TimeOfDay(9, 7, 5)).year == 1999);
22 assert(DateTime(Date(2010, 10, 4), TimeOfDay(0, 0, 30)).year == 2010);
23 assert(DateTime(Date(-7, 4, 5), TimeOfDay(7, 45, 2)).year == -7);
24
25 }
26
27 @safe unittest
28 {
29 import std.datetime.date;
30
31 assert(DateTime(Date(0, 1, 1), TimeOfDay(12, 30, 33)).yearBC == 1);
32 assert(DateTime(Date(-1, 1, 1), TimeOfDay(10, 7, 2)).yearBC == 2);
33 assert(DateTime(Date(-100, 1, 1), TimeOfDay(4, 59, 0)).yearBC == 101);
34
35 }
36
37 @safe unittest
38 {
39 import std.datetime.date;
40
41 auto dt = DateTime(Date(2010, 1, 1), TimeOfDay(7, 30, 0));
42 dt.yearBC = 1;
43 assert(dt == DateTime(Date(0, 1, 1), TimeOfDay(7, 30, 0)));
44
45 dt.yearBC = 10;
46 assert(dt == DateTime(Date(-9, 1, 1), TimeOfDay(7, 30, 0)));
47
48 }
49
50 @safe unittest
51 {
52 import std.datetime.date;
53
54 assert(DateTime(Date(1999, 7, 6), TimeOfDay(9, 7, 5)).month == 7);
55 assert(DateTime(Date(2010, 10, 4), TimeOfDay(0, 0, 30)).month == 10);
56 assert(DateTime(Date(-7, 4, 5), TimeOfDay(7, 45, 2)).month == 4);
57
58 }
59
60 @safe unittest
61 {
62 import std.datetime.date;
63
64 assert(DateTime(Date(1999, 7, 6), TimeOfDay(9, 7, 5)).day == 6);
65 assert(DateTime(Date(2010, 10, 4), TimeOfDay(0, 0, 30)).day == 4);
66 assert(DateTime(Date(-7, 4, 5), TimeOfDay(7, 45, 2)).day == 5);
67
68 }
69
70 @safe unittest
71 {
72 import std.datetime.date;
73
74 auto dt1 = DateTime(2010, 1, 1, 12, 30, 33);
75 dt1.add!"months"(11);
76 assert(dt1 == DateTime(2010, 12, 1, 12, 30, 33));
77
78 auto dt2 = DateTime(2010, 1, 1, 12, 30, 33);
79 dt2.add!"months"(-11);
80 assert(dt2 == DateTime(2009, 2, 1, 12, 30, 33));
81
82 auto dt3 = DateTime(2000, 2, 29, 12, 30, 33);
83 dt3.add!"years"(1);
84 assert(dt3 == DateTime(2001, 3, 1, 12, 30, 33));
85
86 auto dt4 = DateTime(2000, 2, 29, 12, 30, 33);
87 dt4.add!"years"(1, AllowDayOverflow.no);
88 assert(dt4 == DateTime(2001, 2, 28, 12, 30, 33));
89
90 }
91
92 @safe unittest
93 {
94 import std.datetime.date;
95
96 auto dt1 = DateTime(2010, 1, 1, 12, 33, 33);
97 dt1.roll!"months"(1);
98 assert(dt1 == DateTime(2010, 2, 1, 12, 33, 33));
99
100 auto dt2 = DateTime(2010, 1, 1, 12, 33, 33);
101 dt2.roll!"months"(-1);
102 assert(dt2 == DateTime(2010, 12, 1, 12, 33, 33));
103
104 auto dt3 = DateTime(1999, 1, 29, 12, 33, 33);
105 dt3.roll!"months"(1);
106 assert(dt3 == DateTime(1999, 3, 1, 12, 33, 33));
107
108 auto dt4 = DateTime(1999, 1, 29, 12, 33, 33);
109 dt4.roll!"months"(1, AllowDayOverflow.no);
110 assert(dt4 == DateTime(1999, 2, 28, 12, 33, 33));
111
112 auto dt5 = DateTime(2000, 2, 29, 12, 30, 33);
113 dt5.roll!"years"(1);
114 assert(dt5 == DateTime(2001, 3, 1, 12, 30, 33));
115
116 auto dt6 = DateTime(2000, 2, 29, 12, 30, 33);
117 dt6.roll!"years"(1, AllowDayOverflow.no);
118 assert(dt6 == DateTime(2001, 2, 28, 12, 30, 33));
119
120 }
121
122 @safe unittest
123 {
124 import std.datetime.date;
125
126 auto dt1 = DateTime(2010, 1, 1, 11, 23, 12);
127 dt1.roll!"days"(1);
128 assert(dt1 == DateTime(2010, 1, 2, 11, 23, 12));
129 dt1.roll!"days"(365);
130 assert(dt1 == DateTime(2010, 1, 26, 11, 23, 12));
131 dt1.roll!"days"(-32);
132 assert(dt1 == DateTime(2010, 1, 25, 11, 23, 12));
133
134 auto dt2 = DateTime(2010, 7, 4, 12, 0, 0);
135 dt2.roll!"hours"(1);
136 assert(dt2 == DateTime(2010, 7, 4, 13, 0, 0));
137
138 auto dt3 = DateTime(2010, 1, 1, 0, 0, 0);
139 dt3.roll!"seconds"(-1);
140 assert(dt3 == DateTime(2010, 1, 1, 0, 0, 59));
141
142 }
143
144 @safe unittest
145 {
146 import std.datetime.date;
147
148 import core.time : hours, seconds;
149
150 assert(DateTime(2015, 12, 31, 23, 59, 59) + seconds(1) ==
151 DateTime(2016, 1, 1, 0, 0, 0));
152
153 assert(DateTime(2015, 12, 31, 23, 59, 59) + hours(1) ==
154 DateTime(2016, 1, 1, 0, 59, 59));
155
156 assert(DateTime(2016, 1, 1, 0, 0, 0) - seconds(1) ==
157 DateTime(2015, 12, 31, 23, 59, 59));
158
159 assert(DateTime(2016, 1, 1, 0, 59, 59) - hours(1) ==
160 DateTime(2015, 12, 31, 23, 59, 59));
161
162 }
163
164 @safe unittest
165 {
166 import std.datetime.date;
167
168 assert(DateTime(1999, 2, 1, 12, 2, 3).diffMonths(
169 DateTime(1999, 1, 31, 23, 59, 59)) == 1);
170
171 assert(DateTime(1999, 1, 31, 0, 0, 0).diffMonths(
172 DateTime(1999, 2, 1, 12, 3, 42)) == -1);
173
174 assert(DateTime(1999, 3, 1, 5, 30, 0).diffMonths(
175 DateTime(1999, 1, 1, 2, 4, 7)) == 2);
176
177 assert(DateTime(1999, 1, 1, 7, 2, 4).diffMonths(
178 DateTime(1999, 3, 31, 0, 30, 58)) == -2);
179
180 }
181
182 @safe unittest
183 {
184 import std.datetime.date;
185
186 assert(DateTime(Date(1999, 1, 1), TimeOfDay(12, 22, 7)).dayOfYear == 1);
187 assert(DateTime(Date(1999, 12, 31), TimeOfDay(7, 2, 59)).dayOfYear == 365);
188 assert(DateTime(Date(2000, 12, 31), TimeOfDay(21, 20, 0)).dayOfYear == 366);
189
190 }
191
192 @safe unittest
193 {
194 import std.datetime.date;
195
196 assert(DateTime(Date(1, 1, 1), TimeOfDay(0, 0, 0)).dayOfGregorianCal == 1);
197 assert(DateTime(Date(1, 12, 31), TimeOfDay(23, 59, 59)).dayOfGregorianCal == 365);
198 assert(DateTime(Date(2, 1, 1), TimeOfDay(2, 2, 2)).dayOfGregorianCal == 366);
199
200 assert(DateTime(Date(0, 12, 31), TimeOfDay(7, 7, 7)).dayOfGregorianCal == 0);
201 assert(DateTime(Date(0, 1, 1), TimeOfDay(19, 30, 0)).dayOfGregorianCal == -365);
202 assert(DateTime(Date(-1, 12, 31), TimeOfDay(4, 7, 0)).dayOfGregorianCal == -366);
203
204 assert(DateTime(Date(2000, 1, 1), TimeOfDay(9, 30, 20)).dayOfGregorianCal == 730_120);
205 assert(DateTime(Date(2010, 12, 31), TimeOfDay(15, 45, 50)).dayOfGregorianCal == 734_137);
206
207 }
208
209 @safe unittest
210 {
211 import std.datetime.date;
212
213 auto dt = DateTime(Date.init, TimeOfDay(12, 0, 0));
214 dt.dayOfGregorianCal = 1;
215 assert(dt == DateTime(Date(1, 1, 1), TimeOfDay(12, 0, 0)));
216
217 dt.dayOfGregorianCal = 365;
218 assert(dt == DateTime(Date(1, 12, 31), TimeOfDay(12, 0, 0)));
219
220 dt.dayOfGregorianCal = 366;
221 assert(dt == DateTime(Date(2, 1, 1), TimeOfDay(12, 0, 0)));
222
223 dt.dayOfGregorianCal = 0;
224 assert(dt == DateTime(Date(0, 12, 31), TimeOfDay(12, 0, 0)));
225
226 dt.dayOfGregorianCal = -365;
227 assert(dt == DateTime(Date(-0, 1, 1), TimeOfDay(12, 0, 0)));
228
229 dt.dayOfGregorianCal = -366;
230 assert(dt == DateTime(Date(-1, 12, 31), TimeOfDay(12, 0, 0)));
231
232 dt.dayOfGregorianCal = 730_120;
233 assert(dt == DateTime(Date(2000, 1, 1), TimeOfDay(12, 0, 0)));
234
235 dt.dayOfGregorianCal = 734_137;
236 assert(dt == DateTime(Date(2010, 12, 31), TimeOfDay(12, 0, 0)));
237
238 }
239
240 @safe unittest
241 {
242 import std.datetime.date;
243
244 assert(DateTime(Date(1999, 1, 6), TimeOfDay(0, 0, 0)).endOfMonth ==
245 DateTime(Date(1999, 1, 31), TimeOfDay(23, 59, 59)));
246
247 assert(DateTime(Date(1999, 2, 7), TimeOfDay(19, 30, 0)).endOfMonth ==
248 DateTime(Date(1999, 2, 28), TimeOfDay(23, 59, 59)));
249
250 assert(DateTime(Date(2000, 2, 7), TimeOfDay(5, 12, 27)).endOfMonth ==
251 DateTime(Date(2000, 2, 29), TimeOfDay(23, 59, 59)));
252
253 assert(DateTime(Date(2000, 6, 4), TimeOfDay(12, 22, 9)).endOfMonth ==
254 DateTime(Date(2000, 6, 30), TimeOfDay(23, 59, 59)));
255
256 }
257
258 @safe unittest
259 {
260 import std.datetime.date;
261
262 assert(DateTime(Date(1999, 1, 6), TimeOfDay(0, 0, 0)).daysInMonth == 31);
263 assert(DateTime(Date(1999, 2, 7), TimeOfDay(19, 30, 0)).daysInMonth == 28);
264 assert(DateTime(Date(2000, 2, 7), TimeOfDay(5, 12, 27)).daysInMonth == 29);
265 assert(DateTime(Date(2000, 6, 4), TimeOfDay(12, 22, 9)).daysInMonth == 30);
266
267 }
268
269 @safe unittest
270 {
271 import std.datetime.date;
272
273 assert(DateTime(Date(1, 1, 1), TimeOfDay(12, 7, 0)).isAD);
274 assert(DateTime(Date(2010, 12, 31), TimeOfDay(0, 0, 0)).isAD);
275 assert(!DateTime(Date(0, 12, 31), TimeOfDay(23, 59, 59)).isAD);
276 assert(!DateTime(Date(-2010, 1, 1), TimeOfDay(2, 2, 2)).isAD);
277
278 }
279
280 @safe unittest
281 {
282 import std.datetime.date;
283
284 assert(DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)).toISOString() ==
285 "20100704T070612");
286
287 assert(DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)).toISOString() ==
288 "19981225T021500");
289
290 assert(DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)).toISOString() ==
291 "00000105T230959");
292
293 assert(DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)).toISOString() ==
294 "-00040105T000002");
295
296 }
297
298 @safe unittest
299 {
300 import std.datetime.date;
301
302 assert(DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)).toISOExtString() ==
303 "2010-07-04T07:06:12");
304
305 assert(DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)).toISOExtString() ==
306 "1998-12-25T02:15:00");
307
308 assert(DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)).toISOExtString() ==
309 "0000-01-05T23:09:59");
310
311 assert(DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)).toISOExtString() ==
312 "-0004-01-05T00:00:02");
313
314 }
315
316 @safe unittest
317 {
318 import std.datetime.date;
319
320 assert(DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)).toSimpleString() ==
321 "2010-Jul-04 07:06:12");
322
323 assert(DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)).toSimpleString() ==
324 "1998-Dec-25 02:15:00");
325
326 assert(DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)).toSimpleString() ==
327 "0000-Jan-05 23:09:59");
328
329 assert(DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)).toSimpleString() ==
330 "-0004-Jan-05 00:00:02");
331
332 }
333
334 @safe unittest
335 {
336 import std.datetime.date;
337
338 assert(DateTime.fromISOString("20100704T070612") ==
339 DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)));
340
341 assert(DateTime.fromISOString("19981225T021500") ==
342 DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)));
343
344 assert(DateTime.fromISOString("00000105T230959") ==
345 DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)));
346
347 assert(DateTime.fromISOString("-00040105T000002") ==
348 DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)));
349
350 assert(DateTime.fromISOString(" 20100704T070612 ") ==
351 DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)));
352
353 }
354
355 @safe unittest
356 {
357 import std.datetime.date;
358
359 assert(DateTime.fromISOExtString("2010-07-04T07:06:12") ==
360 DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)));
361
362 assert(DateTime.fromISOExtString("1998-12-25T02:15:00") ==
363 DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)));
364
365 assert(DateTime.fromISOExtString("0000-01-05T23:09:59") ==
366 DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)));
367
368 assert(DateTime.fromISOExtString("-0004-01-05T00:00:02") ==
369 DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)));
370
371 assert(DateTime.fromISOExtString(" 2010-07-04T07:06:12 ") ==
372 DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)));
373
374 }
375
376 @safe unittest
377 {
378 import std.datetime.date;
379
380 assert(DateTime.fromSimpleString("2010-Jul-04 07:06:12") ==
381 DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)));
382 assert(DateTime.fromSimpleString("1998-Dec-25 02:15:00") ==
383 DateTime(Date(1998, 12, 25), TimeOfDay(2, 15, 0)));
384 assert(DateTime.fromSimpleString("0000-Jan-05 23:09:59") ==
385 DateTime(Date(0, 1, 5), TimeOfDay(23, 9, 59)));
386 assert(DateTime.fromSimpleString("-0004-Jan-05 00:00:02") ==
387 DateTime(Date(-4, 1, 5), TimeOfDay(0, 0, 2)));
388 assert(DateTime.fromSimpleString(" 2010-Jul-04 07:06:12 ") ==
389 DateTime(Date(2010, 7, 4), TimeOfDay(7, 6, 12)));
390
391 }
392
393 @safe pure unittest
394 {
395 import std.datetime.date;
396
397 import core.time : days, seconds;
398
399 auto dt = DateTime(2000, 6, 1, 10, 30, 0);
400
401 assert(dt.date == Date(2000, 6, 1));
402 assert(dt.timeOfDay == TimeOfDay(10, 30, 0));
403 assert(dt.dayOfYear == 153);
404 assert(dt.dayOfWeek == DayOfWeek.thu);
405
406 dt += 10.days + 100.seconds;
407 assert(dt == DateTime(2000, 6, 11, 10, 31, 40));
408
409 assert(dt.toISOExtString() == "2000-06-11T10:31:40");
410 assert(dt.toISOString() == "20000611T103140");
411 assert(dt.toSimpleString() == "2000-Jun-11 10:31:40");
412
413 assert(DateTime.fromISOExtString("2018-01-01T12:00:00") == DateTime(2018, 1, 1, 12, 0, 0));
414 assert(DateTime.fromISOString("20180101T120000") == DateTime(2018, 1, 1, 12, 0, 0));
415 assert(DateTime.fromSimpleString("2018-Jan-01 12:00:00") == DateTime(2018, 1, 1, 12, 0, 0));
416 }
417
418 @safe unittest
419 {
420 import std.datetime.date;
421
422 assert(Date(1999, 7, 6).year == 1999);
423 assert(Date(2010, 10, 4).year == 2010);
424 assert(Date(-7, 4, 5).year == -7);
425
426 }
427
428 @safe unittest
429 {
430 import std.datetime.date;
431
432 assert(Date(1999, 7, 6).year == 1999);
433 assert(Date(2010, 10, 4).year == 2010);
434 assert(Date(-7, 4, 5).year == -7);
435
436 }
437
438 @safe unittest
439 {
440 import std.datetime.date;
441
442 assert(Date(0, 1, 1).yearBC == 1);
443 assert(Date(-1, 1, 1).yearBC == 2);
444 assert(Date(-100, 1, 1).yearBC == 101);
445
446 }
447
448 @safe unittest
449 {
450 import std.datetime.date;
451
452 auto date = Date(2010, 1, 1);
453 date.yearBC = 1;
454 assert(date == Date(0, 1, 1));
455
456 date.yearBC = 10;
457 assert(date == Date(-9, 1, 1));
458
459 }
460
461 @safe unittest
462 {
463 import std.datetime.date;
464
465 assert(Date(1999, 7, 6).month == 7);
466 assert(Date(2010, 10, 4).month == 10);
467 assert(Date(-7, 4, 5).month == 4);
468
469 }
470
471 @safe unittest
472 {
473 import std.datetime.date;
474
475 assert(Date(1999, 7, 6).day == 6);
476 assert(Date(2010, 10, 4).day == 4);
477 assert(Date(-7, 4, 5).day == 5);
478
479 }
480
481 @safe unittest
482 {
483 import std.datetime.date;
484
485 auto d1 = Date(2010, 1, 1);
486 d1.add!"months"(11);
487 assert(d1 == Date(2010, 12, 1));
488
489 auto d2 = Date(2010, 1, 1);
490 d2.add!"months"(-11);
491 assert(d2 == Date(2009, 2, 1));
492
493 auto d3 = Date(2000, 2, 29);
494 d3.add!"years"(1);
495 assert(d3 == Date(2001, 3, 1));
496
497 auto d4 = Date(2000, 2, 29);
498 d4.add!"years"(1, AllowDayOverflow.no);
499 assert(d4 == Date(2001, 2, 28));
500
501 }
502
503 @safe unittest
504 {
505 import std.datetime.date;
506
507 auto d1 = Date(2010, 1, 1);
508 d1.roll!"months"(1);
509 assert(d1 == Date(2010, 2, 1));
510
511 auto d2 = Date(2010, 1, 1);
512 d2.roll!"months"(-1);
513 assert(d2 == Date(2010, 12, 1));
514
515 auto d3 = Date(1999, 1, 29);
516 d3.roll!"months"(1);
517 assert(d3 == Date(1999, 3, 1));
518
519 auto d4 = Date(1999, 1, 29);
520 d4.roll!"months"(1, AllowDayOverflow.no);
521 assert(d4 == Date(1999, 2, 28));
522
523 auto d5 = Date(2000, 2, 29);
524 d5.roll!"years"(1);
525 assert(d5 == Date(2001, 3, 1));
526
527 auto d6 = Date(2000, 2, 29);
528 d6.roll!"years"(1, AllowDayOverflow.no);
529 assert(d6 == Date(2001, 2, 28));
530
531 }
532
533 @safe unittest
534 {
535 import std.datetime.date;
536
537 auto d = Date(2010, 1, 1);
538 d.roll!"days"(1);
539 assert(d == Date(2010, 1, 2));
540 d.roll!"days"(365);
541 assert(d == Date(2010, 1, 26));
542 d.roll!"days"(-32);
543 assert(d == Date(2010, 1, 25));
544
545 }
546
547 @safe unittest
548 {
549 import std.datetime.date;
550
551 import core.time : days;
552
553 assert(Date(2015, 12, 31) + days(1) == Date(2016, 1, 1));
554 assert(Date(2004, 2, 26) + days(4) == Date(2004, 3, 1));
555
556 assert(Date(2016, 1, 1) - days(1) == Date(2015, 12, 31));
557 assert(Date(2004, 3, 1) - days(4) == Date(2004, 2, 26));
558
559 }
560
561 @safe unittest
562 {
563 import std.datetime.date;
564
565 assert(Date(1999, 2, 1).diffMonths(Date(1999, 1, 31)) == 1);
566 assert(Date(1999, 1, 31).diffMonths(Date(1999, 2, 1)) == -1);
567 assert(Date(1999, 3, 1).diffMonths(Date(1999, 1, 1)) == 2);
568 assert(Date(1999, 1, 1).diffMonths(Date(1999, 3, 31)) == -2);
569
570 }
571
572 @safe unittest
573 {
574 import std.datetime.date;
575
576 assert(Date(1999, 1, 1).dayOfYear == 1);
577 assert(Date(1999, 12, 31).dayOfYear == 365);
578 assert(Date(2000, 12, 31).dayOfYear == 366);
579
580 }
581
582 @safe unittest
583 {
584 import std.datetime.date;
585
586 assert(Date(1, 1, 1).dayOfGregorianCal == 1);
587 assert(Date(1, 12, 31).dayOfGregorianCal == 365);
588 assert(Date(2, 1, 1).dayOfGregorianCal == 366);
589
590 assert(Date(0, 12, 31).dayOfGregorianCal == 0);
591 assert(Date(0, 1, 1).dayOfGregorianCal == -365);
592 assert(Date(-1, 12, 31).dayOfGregorianCal == -366);
593
594 assert(Date(2000, 1, 1).dayOfGregorianCal == 730_120);
595 assert(Date(2010, 12, 31).dayOfGregorianCal == 734_137);
596
597 }
598
599 @safe unittest
600 {
601 import std.datetime.date;
602
603 auto date = Date.init;
604 date.dayOfGregorianCal = 1;
605 assert(date == Date(1, 1, 1));
606
607 date.dayOfGregorianCal = 365;
608 assert(date == Date(1, 12, 31));
609
610 date.dayOfGregorianCal = 366;
611 assert(date == Date(2, 1, 1));
612
613 date.dayOfGregorianCal = 0;
614 assert(date == Date(0, 12, 31));
615
616 date.dayOfGregorianCal = -365;
617 assert(date == Date(-0, 1, 1));
618
619 date.dayOfGregorianCal = -366;
620 assert(date == Date(-1, 12, 31));
621
622 date.dayOfGregorianCal = 730_120;
623 assert(date == Date(2000, 1, 1));
624
625 date.dayOfGregorianCal = 734_137;
626 assert(date == Date(2010, 12, 31));
627
628 }
629
630 @safe unittest
631 {
632 import std.datetime.date;
633
634 assert(Date(1999, 1, 6).endOfMonth == Date(1999, 1, 31));
635 assert(Date(1999, 2, 7).endOfMonth == Date(1999, 2, 28));
636 assert(Date(2000, 2, 7).endOfMonth == Date(2000, 2, 29));
637 assert(Date(2000, 6, 4).endOfMonth == Date(2000, 6, 30));
638
639 }
640
641 @safe unittest
642 {
643 import std.datetime.date;
644
645 assert(Date(1999, 1, 6).daysInMonth == 31);
646 assert(Date(1999, 2, 7).daysInMonth == 28);
647 assert(Date(2000, 2, 7).daysInMonth == 29);
648 assert(Date(2000, 6, 4).daysInMonth == 30);
649
650 }
651
652 @safe unittest
653 {
654 import std.datetime.date;
655
656 assert(Date(1, 1, 1).isAD);
657 assert(Date(2010, 12, 31).isAD);
658 assert(!Date(0, 12, 31).isAD);
659 assert(!Date(-2010, 1, 1).isAD);
660
661 }
662
663 @safe unittest
664 {
665 import std.datetime.date;
666
667 assert(Date(2010, 7, 4).toISOString() == "20100704");
668 assert(Date(1998, 12, 25).toISOString() == "19981225");
669 assert(Date(0, 1, 5).toISOString() == "00000105");
670 assert(Date(-4, 1, 5).toISOString() == "-00040105");
671
672 }
673
674 @safe unittest
675 {
676 import std.datetime.date;
677
678 assert(Date(2010, 7, 4).toISOExtString() == "2010-07-04");
679 assert(Date(1998, 12, 25).toISOExtString() == "1998-12-25");
680 assert(Date(0, 1, 5).toISOExtString() == "0000-01-05");
681 assert(Date(-4, 1, 5).toISOExtString() == "-0004-01-05");
682
683 }
684
685 @safe unittest
686 {
687 import std.datetime.date;
688
689 assert(Date(2010, 7, 4).toSimpleString() == "2010-Jul-04");
690 assert(Date(1998, 12, 25).toSimpleString() == "1998-Dec-25");
691 assert(Date(0, 1, 5).toSimpleString() == "0000-Jan-05");
692 assert(Date(-4, 1, 5).toSimpleString() == "-0004-Jan-05");
693
694 }
695
696 @safe unittest
697 {
698 import std.datetime.date;
699
700 assert(Date.fromISOString("20100704") == Date(2010, 7, 4));
701 assert(Date.fromISOString("19981225") == Date(1998, 12, 25));
702 assert(Date.fromISOString("00000105") == Date(0, 1, 5));
703 assert(Date.fromISOString("-00040105") == Date(-4, 1, 5));
704 assert(Date.fromISOString(" 20100704 ") == Date(2010, 7, 4));
705
706 }
707
708 @safe unittest
709 {
710 import std.datetime.date;
711
712 assert(Date.fromISOExtString("2010-07-04") == Date(2010, 7, 4));
713 assert(Date.fromISOExtString("1998-12-25") == Date(1998, 12, 25));
714 assert(Date.fromISOExtString("0000-01-05") == Date(0, 1, 5));
715 assert(Date.fromISOExtString("-0004-01-05") == Date(-4, 1, 5));
716 assert(Date.fromISOExtString(" 2010-07-04 ") == Date(2010, 7, 4));
717
718 }
719
720 @safe unittest
721 {
722 import std.datetime.date;
723
724 assert(Date.fromSimpleString("2010-Jul-04") == Date(2010, 7, 4));
725 assert(Date.fromSimpleString("1998-Dec-25") == Date(1998, 12, 25));
726 assert(Date.fromSimpleString("0000-Jan-05") == Date(0, 1, 5));
727 assert(Date.fromSimpleString("-0004-Jan-05") == Date(-4, 1, 5));
728 assert(Date.fromSimpleString(" 2010-Jul-04 ") == Date(2010, 7, 4));
729
730 }
731
732 @safe pure unittest
733 {
734 import std.datetime.date;
735
736 import core.time : days;
737
738 auto d = Date(2000, 6, 1);
739
740 assert(d.dayOfYear == 153);
741 assert(d.dayOfWeek == DayOfWeek.thu);
742
743 d += 10.days;
744 assert(d == Date(2000, 6, 11));
745
746 assert(d.toISOExtString() == "2000-06-11");
747 assert(d.toISOString() == "20000611");
748 assert(d.toSimpleString() == "2000-Jun-11");
749
750 assert(Date.fromISOExtString("2018-01-01") == Date(2018, 1, 1));
751 assert(Date.fromISOString("20180101") == Date(2018, 1, 1));
752 assert(Date.fromSimpleString("2018-Jan-01") == Date(2018, 1, 1));
753 }
754
755 @safe unittest
756 {
757 import std.datetime.date;
758
759 auto tod1 = TimeOfDay(7, 12, 0);
760 tod1.roll!"hours"(1);
761 assert(tod1 == TimeOfDay(8, 12, 0));
762
763 auto tod2 = TimeOfDay(7, 12, 0);
764 tod2.roll!"hours"(-1);
765 assert(tod2 == TimeOfDay(6, 12, 0));
766
767 auto tod3 = TimeOfDay(23, 59, 0);
768 tod3.roll!"minutes"(1);
769 assert(tod3 == TimeOfDay(23, 0, 0));
770
771 auto tod4 = TimeOfDay(0, 0, 0);
772 tod4.roll!"minutes"(-1);
773 assert(tod4 == TimeOfDay(0, 59, 0));
774
775 auto tod5 = TimeOfDay(23, 59, 59);
776 tod5.roll!"seconds"(1);
777 assert(tod5 == TimeOfDay(23, 59, 0));
778
779 auto tod6 = TimeOfDay(0, 0, 0);
780 tod6.roll!"seconds"(-1);
781 assert(tod6 == TimeOfDay(0, 0, 59));
782
783 }
784
785 @safe unittest
786 {
787 import std.datetime.date;
788
789 import core.time : hours, minutes, seconds;
790
791 assert(TimeOfDay(12, 12, 12) + seconds(1) == TimeOfDay(12, 12, 13));
792 assert(TimeOfDay(12, 12, 12) + minutes(1) == TimeOfDay(12, 13, 12));
793 assert(TimeOfDay(12, 12, 12) + hours(1) == TimeOfDay(13, 12, 12));
794 assert(TimeOfDay(23, 59, 59) + seconds(1) == TimeOfDay(0, 0, 0));
795
796 assert(TimeOfDay(12, 12, 12) - seconds(1) == TimeOfDay(12, 12, 11));
797 assert(TimeOfDay(12, 12, 12) - minutes(1) == TimeOfDay(12, 11, 12));
798 assert(TimeOfDay(12, 12, 12) - hours(1) == TimeOfDay(11, 12, 12));
799 assert(TimeOfDay(0, 0, 0) - seconds(1) == TimeOfDay(23, 59, 59));
800
801 }
802
803 @safe unittest
804 {
805 import std.datetime.date;
806
807 assert(TimeOfDay(0, 0, 0).toISOString() == "000000");
808 assert(TimeOfDay(12, 30, 33).toISOString() == "123033");
809
810 }
811
812 @safe unittest
813 {
814 import std.datetime.date;
815
816 assert(TimeOfDay(0, 0, 0).toISOExtString() == "00:00:00");
817 assert(TimeOfDay(12, 30, 33).toISOExtString() == "12:30:33");
818
819 }
820
821 @safe unittest
822 {
823 import std.datetime.date;
824
825 assert(TimeOfDay.fromISOString("000000") == TimeOfDay(0, 0, 0));
826 assert(TimeOfDay.fromISOString("123033") == TimeOfDay(12, 30, 33));
827 assert(TimeOfDay.fromISOString(" 123033 ") == TimeOfDay(12, 30, 33));
828
829 }
830
831 @safe unittest
832 {
833 import std.datetime.date;
834
835 assert(TimeOfDay.fromISOExtString("00:00:00") == TimeOfDay(0, 0, 0));
836 assert(TimeOfDay.fromISOExtString("12:30:33") == TimeOfDay(12, 30, 33));
837 assert(TimeOfDay.fromISOExtString(" 12:30:33 ") == TimeOfDay(12, 30, 33));
838
839 }
840
841 @safe pure unittest
842 {
843 import std.datetime.date;
844
845 import core.time : minutes, seconds;
846
847 auto t = TimeOfDay(12, 30, 0);
848
849 t += 10.minutes + 100.seconds;
850 assert(t == TimeOfDay(12, 41, 40));
851
852 assert(t.toISOExtString() == "12:41:40");
853 assert(t.toISOString() == "124140");
854
855 assert(TimeOfDay.fromISOExtString("15:00:00") == TimeOfDay(15, 0, 0));
856 assert(TimeOfDay.fromISOString("015000") == TimeOfDay(1, 50, 0));
857 }
858
859 @safe unittest
860 {
861 import std.datetime.date;
862
863 assert(valid!"hours"(12));
864 assert(!valid!"hours"(32));
865 assert(valid!"months"(12));
866 assert(!valid!"months"(13));
867 }
868
869 @safe pure nothrow @nogc unittest
870 {
871 import std.datetime.date;
872
873 assert(valid!"days"(2016, 2, 29));
874 assert(!valid!"days"(2016, 2, 30));
875 assert(valid!"days"(2017, 2, 20));
876 assert(!valid!"days"(2017, 2, 29));
877 }
878
879 @safe pure unittest
880 {
881 import std.datetime.date;
882
883 import std.exception : assertThrown, assertNotThrown;
884
885 assertNotThrown(enforceValid!"months"(10));
886 assertNotThrown(enforceValid!"seconds"(40));
887
888 assertThrown!DateTimeException(enforceValid!"months"(0));
889 assertThrown!DateTimeException(enforceValid!"hours"(24));
890 assertThrown!DateTimeException(enforceValid!"minutes"(60));
891 assertThrown!DateTimeException(enforceValid!"seconds"(60));
892 }
893
894 @safe pure unittest
895 {
896 import std.datetime.date;
897
898 import std.exception : assertThrown, assertNotThrown;
899
900 assertNotThrown(enforceValid!"days"(2000, Month.jan, 1));
901 // leap year
902 assertNotThrown(enforceValid!"days"(2000, Month.feb, 29));
903
904 assertThrown!DateTimeException(enforceValid!"days"(2001, Month.feb, 29));
905 assertThrown!DateTimeException(enforceValid!"days"(2000, Month.jan, 32));
906 assertThrown!DateTimeException(enforceValid!"days"(2000, Month.apr, 31));
907 }
908
909 @safe pure nothrow @nogc unittest
910 {
911 import std.datetime.date;
912
913 assert(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.mon) == 0);
914 assert(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.sun) == 6);
915 assert(daysToDayOfWeek(DayOfWeek.mon, DayOfWeek.wed) == 2);
916 }
917
918 @safe pure unittest
919 {
920 import std.datetime.date;
921
922 assert(monthsToMonth(Month.jan, Month.jan) == 0);
923 assert(monthsToMonth(Month.jan, Month.dec) == 11);
924 assert(monthsToMonth(Month.jul, Month.oct) == 3);
925 }
926
927 @safe unittest
928 {
929 import std.datetime.date;
930
931 foreach (year; [1, 2, 100, 2001, 2002, 2003, 2005, 2006, 2007, 2009, 2010])
932 {
933 assert(!yearIsLeapYear(year));
934 assert(!yearIsLeapYear(-year));
935 }
936
937 foreach (year; [0, 4, 8, 400, 800, 1600, 1996, 2000, 2004, 2008, 2012])
938 {
939 assert(yearIsLeapYear(year));
940 assert(yearIsLeapYear(-year));
941 }
942 }
943
944 @safe unittest
945 {
946 import std.datetime.date;
947
948 import core.time : Duration;
949 import std.datetime.interval : Interval;
950 import std.datetime.systime : SysTime;
951
952 static assert(isTimePoint!Date);
953 static assert(isTimePoint!DateTime);
954 static assert(isTimePoint!SysTime);
955 static assert(isTimePoint!TimeOfDay);
956
957 static assert(!isTimePoint!int);
958 static assert(!isTimePoint!Duration);
959 static assert(!isTimePoint!(Interval!SysTime));
960 }
961
962 @safe @nogc nothrow unittest
963 {
964 import std.datetime.date;
965
966 assert(validTimeUnits("msecs", "seconds", "minutes"));
967 assert(validTimeUnits("days", "weeks", "months"));
968 assert(!validTimeUnits("ms", "seconds", "minutes"));
969 }
970
971 @safe pure unittest
972 {
973 import std.datetime.date;
974
975 import std.exception : assertThrown;
976
977 assert(cmpTimeUnits("hours", "hours") == 0);
978 assert(cmpTimeUnits("hours", "weeks") < 0);
979 assert(cmpTimeUnits("months", "seconds") > 0);
980
981 assertThrown!DateTimeException(cmpTimeUnits("month", "second"));
982 }
983
984 @safe pure unittest
985 {
986 import std.datetime.date;
987
988 static assert(CmpTimeUnits!("years", "weeks") > 0);
989 static assert(CmpTimeUnits!("days", "days") == 0);
990 static assert(CmpTimeUnits!("seconds", "hours") < 0);
991 }
992