4231462128511f9dc7a0c261a24c29d8473f6bb8
[openafs.git] / src / util / test / test_ktime.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include "ktime.h"
11
12 static struct testTime {
13     char *time;
14     long code;
15     long sec;
16 } testTimes[] = {
17     "now", 1/* lookup current time */, 0,
18     "never", 0, 0xffffffff,
19     "12/3/89", 0, 628664400,
20     "1/1/1", 0, 978325200,
21     "1/0/80", -2, 0,
22     "13/1/80", -2, 0,
23     "0/1/80", -2, 0,
24     "1/32/80", -2, 0,
25     "2/30/80", 0, 320734799,
26     /* Oh, well: note that 2/30 is bigger than any real date in February, and
27      * since this algorithm approaches the correct value from below, this is
28      * the closest it can come. */
29     "3/1/80", 0, 320734800,
30     "3/1/80 0:00", 0, 320734800,
31     "2/30/80 24:00", -2, 0,
32     "2/30/80 23:60", -2, 0,
33     "22/22/22", -2, 0,
34     "12/31/69 19:07", 0, 420,
35     "12/31/99 23:59", 0, 946702740,
36     "12/31/99 23:59:59", 0, 946702740,  /* ignores seconds */
37     "23:12", -1, 0,
38     "22/12", -1, 0,
39     "22/22/22 12", -1, 0,
40     "12/31/199 23:59:59", -1, 0,
41     "12/31/1888", -1, 0,
42     "-13/-44/22 -15:77", -1, 0,
43     "4/14/24", 0, 1713067200,
44     "4/14/2024", 0, 1713067200,
45     "4/14/68", 0, 0x7fffffff,           /* sadly, legal but w/ sign bit on */
46     "4/14/69", 0, 0,
47     0,0
48 };
49
50 main(argc, argv)
51   int argc;
52   char *argv[];
53 {
54     long code;
55     int  errors;
56     long t;
57     struct testTime *tt;
58
59     /* should do timezone and daylight savings time correction so this program
60      * work in other than EST */
61
62     if (argc > 1) {
63         code = util_GetLong (argv[1], &t);
64         if (code) { /* assume its a date string */
65             code = ktime_DateToLong (argv[1], &t);
66             printf ("The string %s gives %d; ctime yields %s",
67                     argv[1], t, ctime (&t));
68         } else {
69             printf ("The value %d is %s", t, ctime(&t));
70         }
71         exit (0);
72     }
73     errors = 0;
74     for (tt = testTimes; tt->time; tt++) {
75         code = ktime_DateToLong (tt->time, &t);
76         if (code) {
77             if (tt->code < 0) continue; /* got error as we should have */
78             printf ("ktime_DateToLong returned %d given '%s'\n",
79                     code, tt->time);
80             errors++;
81             continue;
82         } else if (tt->code < 0) {
83             printf ("ktime_DateToLong didn't failed on %s, instead got %d\n",
84                     tt->time, t);
85             errors++;
86             continue;
87         }
88         if (tt->code == 1) {
89             if (time(0) - t <= 1) continue;
90         } else if (t == tt->sec) continue;
91         printf ("Error converting %s to long: got %d should be %d\n",
92                 tt->time, t, tt->sec);
93         errors++;
94         continue;
95     }
96     exit (errors);
97 }