File: dates.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (588 lines) | stat: -rw-r--r-- 19,601 bytes parent folder | download
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright (C) 2007, 2008 Zack Weinberg <zackw@panix.com>
//                          Markus Wanner <markus@bluegap.ch>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include "unit_tests.hh"

#include "../dates.cc"

s64 const FOUR_HUNDRED_YEARS = 400 * YEAR + (100 - 4 + 1) * DAY;

UNIT_TEST(our_timegm)
{
#define OK(x) UNIT_TEST_CHECK(our_timegm(t) == MILLISEC(x))

  broken_down_time t = {0, 0, 0, 0, 1, 1, 1970};
  OK(0);

  t.year = 2000;
  OK(s64_C(946684800));

  // Make sure our_timegm works for years before 1970 as well.
  t.year = 1960;
  OK(-10 * YEAR - 3 * DAY);

  t.year = 1569;
  OK(-FOUR_HUNDRED_YEARS - YEAR);

  t.year = 1570;
  OK(-FOUR_HUNDRED_YEARS);

  t.year = 1571;
  OK(-FOUR_HUNDRED_YEARS + YEAR);

  t.year = 1572;
  OK(-FOUR_HUNDRED_YEARS + 2 * YEAR);

  t.year = 1573;
  OK(-FOUR_HUNDRED_YEARS + 3 * YEAR + DAY);

  t.year = 1574;
  OK(-FOUR_HUNDRED_YEARS + 4 * YEAR + DAY);

  t.year = 1170;
  OK(-2 * FOUR_HUNDRED_YEARS);

  t.year = 770;
  OK(-3 * FOUR_HUNDRED_YEARS);

  t.year = 370;
  OK(-4 * FOUR_HUNDRED_YEARS);

  t.year = 1;  /* year 1 anno Domini */
  OK(-1969 * YEAR - (492 - 19 + 4) * DAY);

  t.year = 0; /* no such year */
  UNIT_TEST_CHECK_THROW(our_timegm(t), std::logic_error);

#undef OK
}

UNIT_TEST(from_string)
{
#define OK(x,y) do {                                    \
    string s_ = date_t(x).as_iso_8601_extended();       \
    L(FL("date_t: %s -> %s") % (x) % s_);               \
    UNIT_TEST_CHECK(s_ == (y));                         \
  } while (0)
#define NO(x) UNIT_TEST_CHECK_THROW(date_t(x), recoverable_failure)

  // canonical format
  OK("2007-03-01T18:41:13", "2007-03-01T18:41:13");
  OK("2007-03-01T00:41:13", "2007-03-01T00:41:13");
  OK("2007-03-01T01:41:13", "2007-03-01T01:41:13");
  OK("2007-03-01T23:41:13", "2007-03-01T23:41:13");

  // test dates around leap years
  OK("1999-12-31T12:00:00", "1999-12-31T12:00:00");
  OK("1999-12-31T23:59:59", "1999-12-31T23:59:59");
  OK("2000-01-01T00:00:00", "2000-01-01T00:00:00");
  OK("2000-01-01T12:00:00", "2000-01-01T12:00:00");
  OK("2003-12-31T12:00:00", "2003-12-31T12:00:00");
  OK("2003-12-31T23:59:59", "2003-12-31T23:59:59");
  OK("2004-01-01T00:00:00", "2004-01-01T00:00:00");
  OK("2004-01-01T12:00:00", "2004-01-01T12:00:00");

  // test dates around the leap day in february
  OK("2003-02-28T23:59:59", "2003-02-28T23:59:59");
  NO("2003-02-29T00:00:00");
  OK("2004-02-28T23:59:59", "2004-02-28T23:59:59");
  OK("2004-02-29T00:00:00", "2004-02-29T00:00:00");

  // squashed format
  OK("20070301T184113", "2007-03-01T18:41:13");
  // space between date and time
  OK("2007-03-01 18:41:13", "2007-03-01T18:41:13");
  // squashed, space
  OK("20070301 184113", "2007-03-01T18:41:13");

  // more than four digits in the year
  OK("120070301T184113", "12007-03-01T18:41:13");

  // before the epoch
  OK("1969-03-01T18:41:13", "1969-03-01T18:41:13");

  // inappropriate character at every possible position
  NO("x007-03-01T18:41:13");
  NO("2x07-03-01T18:41:13");
  NO("20x7-03-01T18:41:13");
  NO("200x-03-01T18:41:13");
  NO("2007x03-01T18:41:13");
  NO("2007-x3-01T18:41:13");
  NO("2007-0x-01T18:41:13");
  NO("2007-03x01T18:41:13");
  NO("2007-03-x1T18:41:13");
  NO("2007-03-0xT18:41:13");
  NO("2007-03-01x18:41:13");
  NO("2007-03-01Tx8:41:13");
  NO("2007-03-01T1x:41:13");
  NO("2007-03-01T18x41:13");
  NO("2007-03-01T18:x1:13");
  NO("2007-03-01T18:4x:13");
  NO("2007-03-01T18:41x13");
  NO("2007-03-01T18:41:x3");
  NO("2007-03-01T18:41:1x");

  NO("x0070301T184113");
  NO("2x070301T184113");
  NO("20x70301T184113");
  NO("200x0301T184113");
  NO("2007x301T184113");
  NO("20070x01T184113");
  NO("200703x1T184113");
  NO("2007030xT184113");
  NO("20070301x184113");
  NO("20070301Tx84113");
  NO("20070301T1x4113");
  NO("20070301T18x113");
  NO("20070301T184x13");
  NO("20070301T1841x3");
  NO("20070301T18411x");

  // two digit years are not accepted
  NO("07-03-01T18:41:13");

  // components (other than year) out of range
  NO("2007-00-01T18:41:13");
  NO("2007-13-01T18:41:13");

  NO("2007-01-00T18:41:13");
  NO("2007-01-32T18:41:13");
  NO("2007-02-29T18:41:13");
  NO("2007-03-32T18:41:13");
  NO("2007-04-31T18:41:13");
  NO("2007-05-32T18:41:13");
  NO("2007-06-31T18:41:13");
  NO("2007-07-32T18:41:13");
  NO("2007-08-32T18:41:13");
  NO("2007-09-31T18:41:13");
  NO("2007-10-32T18:41:13");
  NO("2007-11-31T18:41:13");
  NO("2007-03-32T18:41:13");

  NO("2007-03-01T24:41:13");
  NO("2007-03-01T18:60:13");
  NO("2007-03-01T18:41:60");

  // leap year February
  OK("2008-02-29T18:41:13", "2008-02-29T18:41:13");
  NO("2008-02-30T18:41:13");

  // maybe we should support these, but we don't
  NO("2007-03-01");
  NO("18:41");
  NO("18:41:13");
  NO("Thu Mar 1 18:41:13 PST 2007");
  NO("Thu, 01 Mar 2007 18:47:22");
  NO("Thu, 01 Mar 2007 18:47:22 -0800");
  NO("torsdag, mars 01, 2007, 18.50.10");
  // et cetera
#undef OK
#undef NO
}

#ifndef WIN32
// parse_date (used by from_formatted_localtime) not implemented on Win32
UNIT_TEST(roundtrip_localtimes)
{
#define OK(x) do {                                                       \
    string iso8601 = x.as_iso_8601_extended();                           \
    string formatted = x.as_formatted_localtime("%c");                   \
    L(FL("iso 8601 date '%s' local date '%s'\n") % iso8601 % formatted); \
    date_t parsed = date_t::from_formatted_localtime(formatted, "%c");   \
    UNIT_TEST_CHECK(parsed == x);                                        \
  } while (0)

  // this is the valid range of dates supported by 32 bit time_t
  date_t start("1901-12-13T20:45:52");
  date_t end("2038-01-19T03:14:07");

  OK(start);
  OK(end);

  // stagger the millisecond values to hit different times of day
  for (date_t date = start; date <= end; date += MILLISEC(DAY+HOUR+MIN+SEC))
    OK(date);

  start -= 1000;
  end += 1000;

  // these tests run with LANG=C and TZ=UTC so the %c format seems to work
  // however strptime does not like the timezone name when %c is used in
  // other locales. with LANG=en_CA.UTF-8 this test fails.

  if (sizeof(time_t) <= 4)
    {
      UNIT_TEST_CHECK_THROW(start.as_formatted_localtime("%c"),
                            recoverable_failure);
      UNIT_TEST_CHECK_THROW(date_t::from_formatted_localtime("Fri Dec 13 20:45:51 1901", "%c"),
                            recoverable_failure);

      UNIT_TEST_CHECK_THROW(end.as_formatted_localtime("%c"),
                            recoverable_failure);
      UNIT_TEST_CHECK_THROW(date_t::from_formatted_localtime("Tue Jan 19 03:14:08 2038", "%c"),
                            recoverable_failure);
    }
  else
    {
      OK(start);
      OK(end);
    }

  // this date represents 1 second before the unix epoch which has a time_t
  // value of -1. conveniently, mktime returns -1 to indicate that it was
  // unable to convert a struct tm into a valid time_t value even though
  // dates before/after this are valid. our date parsing code ignores this
  // "error" from mktime, does a conversion back to localtime and compares
  // this with the localtime value it called mktime with in the first place.
  // allowing conversion of this date to succeed while still detecting dates
  // that are out of range.

  OK(date_t("1969-12-31T23:59:59"));

#undef OK
}
#endif

#ifndef WIN32
// parse_date (used by from_formatted_localtime) not implemented on Win32
UNIT_TEST(localtime_formats)
{
#define OK(d, f) do {                                                    \
    string formatted = d.as_formatted_localtime(f);                      \
    date_t parsed = date_t::from_formatted_localtime(formatted, f);      \
    UNIT_TEST_CHECK(parsed == d);                                        \
    L(FL("date %s formatted %s parsed %s\n") % d % formatted % parsed);  \
  } while (0)

  // can we fiddle with LANG or TZ here?

  // note that %c doesn't work for en_CA.UTF-8 because it includes a timezone label
  // that strptime doesn't parse. this leaves some of the input string unprocessed
  // which date_t::from_formatted_localtime doesn't allow.

  // this is the valid range of dates supported by 32 bit time_t
  date_t start("1901-12-13T20:45:52");
  date_t end("2038-01-19T03:14:07");

  // The 'y' (year in century) specification is taken to specify a year in
  // the 20th century by libc4 and libc5.  It is taken to be a year in the
  // range 1950-2049 by glibc 2.0.  It is taken to be a year in 1969-2068
  // since glibc 2.1.

  // When a century is not otherwise specified, values in the range 69-99
  // refer to years in the twentieth century (1969-1999); values in the
  // range 00-68 refer to years in the twenty-first century (2000-2068).

  // With glibc 2.1 or newer the 2 digit years in the %x format will fail
  // for years before 1969 because strptime will assume they are in the 21st
  // century.
  date_t yy_ok("1969-01-01T00:00:00");

  // test roughly 2 days per month in this entire range
  for (date_t date = start; date <= end; date += MILLISEC(15*(DAY+HOUR+MIN+SEC)))
    {
      L(FL("iso 8601 date '%s' end '%s'\n") % date % end);

      // these all seem to work with the test setup of LANG=C and TZ=UTC

      OK(date, "%Y-%m-%d %X"); // YYYY-MM-DD hh:mm:ss
      OK(date, "%X %Y-%m-%d"); // hh:mm:ss YYYY-MM-DD
      OK(date, "%d %b %Y, %I:%M:%S %p");
      OK(date, "%a %b %d %H:%M:%S %Y");
      OK(date, "%a %d %b %Y %I:%M:%S %p");
      OK(date, "%a, %d %b %Y %H:%M:%S");
      OK(date, "%Y-%m-%d %H:%M:%S");
      OK(date, "%Y-%m-%dT%H:%M:%S");

      if (date >= yy_ok)
        {
          OK(date, "%x %X"); // YY-MM-DD hh:mm:ss
          OK(date, "%X %x"); // hh:mm:ss YY-MM-DD
        }

      // both, %F and %z, are GNU extensions which we cannot test here
      // universally, because e.g. openBSD doesn't know them
      //OK(date, "%F %X"); // YYYY-MM-DD hh:mm:ss
      //OK(date, "%a %d %b %Y %I:%M:%S %p %z");

      // possibly anything with a timezone label (%Z) will fail
      //OK(date, "%a %d %b %Y %I:%M:%S %p %Z"); // the timezone label breaks this
    }

  // check that trailing characters not matched by the date format are caught
  UNIT_TEST_CHECK_THROW(date_t::from_formatted_localtime("1988-01-01 12:12:12 gobbledygook",
                                                         "%Y-%m-%d %X"),
                        recoverable_failure);

#undef OK
}
#endif

UNIT_TEST(from_unix_epoch)
{
#define OK_(x,y) do {                              \
    string s_ = date_t(x).as_iso_8601_extended();  \
    L(FL("date_t: %lu -> %s") % (x) % s_);         \
    UNIT_TEST_CHECK(s_ == (y));                    \
  } while (0)
#define OK(x,y) OK_(s64_C(x),y)

  // every month boundary in 1970
  OK(0,          "1970-01-01T00:00:00");
  OK(2678399000, "1970-01-31T23:59:59");
  OK(2678400000, "1970-02-01T00:00:00");
  OK(5097599000, "1970-02-28T23:59:59");
  OK(5097600000, "1970-03-01T00:00:00");
  OK(7775999000, "1970-03-31T23:59:59");
  OK(7776000000, "1970-04-01T00:00:00");
  OK(10367999000, "1970-04-30T23:59:59");
  OK(10368000000, "1970-05-01T00:00:00");
  OK(13046399000, "1970-05-31T23:59:59");
  OK(13046400000, "1970-06-01T00:00:00");
  OK(15638399000, "1970-06-30T23:59:59");
  OK(15638400000, "1970-07-01T00:00:00");
  OK(18316799000, "1970-07-31T23:59:59");
  OK(18316800000, "1970-08-01T00:00:00");
  OK(20995199000, "1970-08-31T23:59:59");
  OK(20995200000, "1970-09-01T00:00:00");
  OK(23587199000, "1970-09-30T23:59:59");
  OK(23587200000, "1970-10-01T00:00:00");
  OK(26265599000, "1970-10-31T23:59:59");
  OK(26265600000, "1970-11-01T00:00:00");
  OK(28857599000, "1970-11-30T23:59:59");
  OK(28857600000, "1970-12-01T00:00:00");
  OK(31535999000, "1970-12-31T23:59:59");
  OK(31536000000, "1971-01-01T00:00:00");

  // every month boundary in 1972 (an ordinary leap year)
  OK(63071999000, "1971-12-31T23:59:59");
  OK(63072000000, "1972-01-01T00:00:00");
  OK(65750399000, "1972-01-31T23:59:59");
  OK(65750400000, "1972-02-01T00:00:00");
  OK(68255999000, "1972-02-29T23:59:59");
  OK(68256000000, "1972-03-01T00:00:00");
  OK(70934399000, "1972-03-31T23:59:59");
  OK(70934400000, "1972-04-01T00:00:00");
  OK(73526399000, "1972-04-30T23:59:59");
  OK(73526400000, "1972-05-01T00:00:00");
  OK(76204799000, "1972-05-31T23:59:59");
  OK(76204800000, "1972-06-01T00:00:00");
  OK(78796799000, "1972-06-30T23:59:59");
  OK(78796800000, "1972-07-01T00:00:00");
  OK(81475199000, "1972-07-31T23:59:59");
  OK(81475200000, "1972-08-01T00:00:00");
  OK(84153599000, "1972-08-31T23:59:59");
  OK(84153600000, "1972-09-01T00:00:00");
  OK(86745599000, "1972-09-30T23:59:59");
  OK(86745600000, "1972-10-01T00:00:00");
  OK(89423999000, "1972-10-31T23:59:59");
  OK(89424000000, "1972-11-01T00:00:00");
  OK(92015999000, "1972-11-30T23:59:59");
  OK(92016000000, "1972-12-01T00:00:00");
  OK(94694399000, "1972-12-31T23:59:59");
  OK(94694400000, "1973-01-01T00:00:00");

  // every month boundary in 2000 (a leap year per rule 5)
  OK(946684799000, "1999-12-31T23:59:59");
  OK(946684800000, "2000-01-01T00:00:00");
  OK(949363199000, "2000-01-31T23:59:59");
  OK(949363200000, "2000-02-01T00:00:00");
  OK(951868799000, "2000-02-29T23:59:59");
  OK(951868800000, "2000-03-01T00:00:00");
  OK(954547199000, "2000-03-31T23:59:59");
  OK(954547200000, "2000-04-01T00:00:00");
  OK(957139199000, "2000-04-30T23:59:59");
  OK(957139200000, "2000-05-01T00:00:00");
  OK(959817599000, "2000-05-31T23:59:59");
  OK(959817600000, "2000-06-01T00:00:00");
  OK(962409599000, "2000-06-30T23:59:59");
  OK(962409600000, "2000-07-01T00:00:00");
  OK(965087999000, "2000-07-31T23:59:59");
  OK(965088000000, "2000-08-01T00:00:00");
  OK(967766399000, "2000-08-31T23:59:59");
  OK(967766400000, "2000-09-01T00:00:00");
  OK(970358399000, "2000-09-30T23:59:59");
  OK(970358400000, "2000-10-01T00:00:00");
  OK(973036799000, "2000-10-31T23:59:59");
  OK(973036800000, "2000-11-01T00:00:00");
  OK(975628799000, "2000-11-30T23:59:59");
  OK(975628800000, "2000-12-01T00:00:00");
  OK(978307199000, "2000-12-31T23:59:59");
  OK(978307200000, "2001-01-01T00:00:00");

  // every month boundary in 2100 (a normal year per rule 4)
  OK(4102444800000, "2100-01-01T00:00:00");
  OK(4105123199000, "2100-01-31T23:59:59");
  OK(4105123200000, "2100-02-01T00:00:00");
  OK(4107542399000, "2100-02-28T23:59:59");
  OK(4107542400000, "2100-03-01T00:00:00");
  OK(4110220799000, "2100-03-31T23:59:59");
  OK(4110220800000, "2100-04-01T00:00:00");
  OK(4112812799000, "2100-04-30T23:59:59");
  OK(4112812800000, "2100-05-01T00:00:00");
  OK(4115491199000, "2100-05-31T23:59:59");
  OK(4115491200000, "2100-06-01T00:00:00");
  OK(4118083199000, "2100-06-30T23:59:59");
  OK(4118083200000, "2100-07-01T00:00:00");
  OK(4120761599000, "2100-07-31T23:59:59");
  OK(4120761600000, "2100-08-01T00:00:00");
  OK(4123439999000, "2100-08-31T23:59:59");
  OK(4123440000000, "2100-09-01T00:00:00");
  OK(4126031999000, "2100-09-30T23:59:59");
  OK(4126032000000, "2100-10-01T00:00:00");
  OK(4128710399000, "2100-10-31T23:59:59");
  OK(4128710400000, "2100-11-01T00:00:00");
  OK(4131302399000, "2100-11-30T23:59:59");
  OK(4131302400000, "2100-12-01T00:00:00");
  OK(4133980799000, "2100-12-31T23:59:59");

  // limit of valid dates
  OK_(LATEST_SUPPORTED_DATE, "292278994-01-01T00:00:00");
  UNIT_TEST_CHECK_THROW(date_t(LATEST_SUPPORTED_DATE+1),
                        std::logic_error);
  OK_(EARLIEST_SUPPORTED_DATE, "0001-01-01T00:00:00");
  UNIT_TEST_CHECK_THROW(date_t(EARLIEST_SUPPORTED_DATE-1),
                        std::logic_error);

#undef OK
}

UNIT_TEST(comparisons)
{
  date_t may = date_t("2000-05-01T00:00:00"),
         jun = date_t("2000-06-01T00:00:00"),
         jul = date_t("2000-07-01T00:00:00"),
         v;

  // testing all comparisons operators
  UNIT_TEST_CHECK(may < jun);
  UNIT_TEST_CHECK(jun < jul);
  UNIT_TEST_CHECK(may < jul);

  UNIT_TEST_CHECK(jul > may);

  UNIT_TEST_CHECK(may == date_t("2000-05-01T00:00:00"));
  UNIT_TEST_CHECK(may != date_t("2000-05-01T00:00:01"));
  UNIT_TEST_CHECK(may != date_t("2000-09-01T00:00:00"));
  UNIT_TEST_CHECK(may != date_t("1999-05-01T00:00:00"));

  v = may;
  v += MILLISEC(DAY * 31);
  UNIT_TEST_CHECK(v == jun);

  v = jul;
  v -= MILLISEC(DAY * 30);
  UNIT_TEST_CHECK(v == jun);

  // check limits for subtractions
  v = date_t("0001-01-01T00:00:01");
  v -= 1000;
  UNIT_TEST_CHECK(v == date_t("0001-01-01T00:00:00"));
  UNIT_TEST_CHECK_THROW(v -= 1, std::logic_error);

  // check limits for additions
  v = date_t("292278993-12-31T23:59:59");
  v += 1000;
  UNIT_TEST_CHECK(v == date_t("292278994-01-01T00:00:00"));
  L(FL("v off by %ld")
    % (v.as_millisecs_since_unix_epoch() - LATEST_SUPPORTED_DATE));
  UNIT_TEST_CHECK_THROW(v += 1, std::logic_error);

  // check date differences
  UNIT_TEST_CHECK(date_t("2000-05-05T00:00:01") -
                  date_t("2000-05-05T00:00:00")
                  == 1000);
  UNIT_TEST_CHECK(date_t("2000-05-05T00:00:01") -
                  date_t("2000-05-05T00:00:02")
                  == -1000);
  UNIT_TEST_CHECK(date_t("2000-05-05T01:00:00") -
                  date_t("2000-05-05T00:00:00")
                  == 3600000);
}

// This test takes a long time to run and can create an enormous logfile
// (if there are a lot of failures) so it is disabled by default.  If you
// make substantial changes to our_gmtime or our_timegm you should run it.
#if 0
static void
roundtrip_1(s64 t)
{
  if (!valid_ms_count(t))
    return;

  broken_down_time tm;
  our_gmtime(t, tm);
  s64 t1 = our_timegm(tm);
  if (t != t1)
    {
      L(FL("%d -> %04u-%02u-%02uT%02u:%02u:%02u.%03u -> %d error %+d")
        % t
        % tm.year % tm.month % tm.day % tm.hour % tm.min % tm.sec % tm.millisec
        % t1
        % (t - t1));
      UNIT_TEST_CHECK(t == t1);
    }

  // if possible check against the system gmtime() as well
  if (std::numeric_limits<time_t>::max() >= std::numeric_limits<s64>::max())
    {
      time_t tsys = ((t - tm.millisec) / 1000) - get_epoch_offset();
      std::tm tmsys = *std::gmtime(&tsys);
      broken_down_time tmo;
      tmo.millisec = 0;
      tmo.sec = tmsys.tm_sec;
      tmo.min = tmsys.tm_min;
      tmo.hour = tmsys.tm_hour;
      tmo.day = tmsys.tm_mday;
      tmo.month = tmsys.tm_mon + 1;
      tmo.year = tmsys.tm_year + 1900;

      bool sys_match = (tm.year == tmo.year
                        && tm.month == tmo.month
                        && tm.day == tmo.day
                        && tm.hour == tmo.hour
                        && tm.min == tmo.min
                        && tm.sec == tmo.sec);
      if (!sys_match)
        {
          L(FL("ours: %04u-%02u-%02uT%02u:%02u:%02u.%03u")
            % tm.year % tm.month % tm.day % tm.hour % tm.min
            % tm.sec % tm.millisec);
          L(FL("system: %04u-%02u-%02uT%02u:%02u:%02u")
            % tmo.year % tmo.month % tmo.day % tmo.hour % tmo.min % tmo.sec);
          UNIT_TEST_CHECK(sys_match);
        }
    }
}

UNIT_TEST(roundtrip_all_year_boundaries)
{
  s64 t = EARLIEST_SUPPORTED_DATE;
  u32 year = 1;

  while (t < LATEST_SUPPORTED_DATE)
    {
      roundtrip_1(t-1);
      roundtrip_1(t);

      t += MILLISEC(DAY * days_in_year(year));
      year ++;
    }
}
#endif

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: