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