b5fe4a564ddec9779bb3996e7db5cccf0df6ca5e
[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 <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include "ktime.h"
15
16 static struct testTime {
17     char *time;
18     long code;
19     long sec;
20 } testTimes[] = {
21     "now", 1 /* lookup current time */ , 0,
22         "never", 0, 0xffffffff, "12/3/89", 0, 628664400, "1/1/1", 0,
23         978325200, "1/0/80", -2, 0, "13/1/80", -2, 0, "0/1/80", -2, 0,
24         "1/32/80", -2, 0, "2/30/80", 0, 320734799,
25         /* Oh, well: note that 2/30 is bigger than any real date in February, and
26          * since this algorithm approaches the correct value from below, this is
27          * the closest it can come. */
28         "3/1/80", 0, 320734800, "3/1/80 0:00", 0, 320734800, "2/30/80 24:00", -2, 0, "2/30/80 23:60", -2, 0, "22/22/22", -2, 0, "12/31/69 19:07", 0, 420, "12/31/99 23:59", 0, 946702740, "12/31/99 23:59:59", 0, 946702740,    /* ignores seconds */
29         "23:12", -1, 0, "22/12", -1, 0, "22/22/22 12", -1, 0, "12/31/199 23:59:59", -1, 0, "12/31/1888", -1, 0, "-13/-44/22 -15:77", -1, 0, "4/14/24", 0, 1713067200, "4/14/2024", 0, 1713067200, "4/14/68", 0, 0x7fffffff,     /* sadly, legal but w/ sign bit on */
30 "4/14/69", 0, 0, 0, 0};
31
32 main(argc, argv)
33      int argc;
34      char *argv[];
35 {
36     long code, temp;
37     int errors;
38     time_t t;
39     struct testTime *tt;
40
41     /* should do timezone and daylight savings time correction so this program
42      * work in other than EST */
43
44     if (argc > 1) {
45         code = util_GetLong(argv[1], &temp);
46         t = temp;
47         if (code) {             /* assume its a date string */
48             code = ktime_DateToLong(argv[1], &temp);
49             t = temp;
50             printf("The string %s gives %d; ctime yields %s", argv[1], temp,
51                    ctime(&t));
52         } else {
53             printf("The value %d is %s", temp, ctime(&t));
54         }
55         exit(0);
56     }
57     errors = 0;
58     for (tt = testTimes; tt->time; tt++) {
59         code = ktime_DateToLong(tt->time, &t);
60         if (code) {
61             if (tt->code < 0)
62                 continue;       /* got error as we should have */
63             printf("ktime_DateToLong returned %d given '%s'\n", code,
64                    tt->time);
65             errors++;
66             continue;
67         } else if (tt->code < 0) {
68             printf("ktime_DateToLong didn't failed on %s, instead got %d\n",
69                    tt->time, t);
70             errors++;
71             continue;
72         }
73         if (tt->code == 1) {
74             if (time(0) - t <= 1)
75                 continue;
76         } else if (t == tt->sec)
77             continue;
78         printf("Error converting %s to long: got %d should be %d\n", tt->time,
79                t, tt->sec);
80         errors++;
81         continue;
82     }
83     exit(errors);
84 }