Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / tests / util / ktime-t.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 #include <stdio.h>
14 #ifdef HAVE_STDLIB_H
15 #include <stdlib.h>
16 #endif
17 #include <time.h>
18
19 #include <afs/ktime.h>
20 #include <afs/afsutil.h>
21 #include <afs/afsutil_prototypes.h>
22 #include <tests/tap/basic.h>
23
24 static struct testTime {
25 char *time;
26 long code;
27 time_t sec;
28 } testTimes[] = {
29 { "now", 1, 0 }, /* lookup current time */
30 { "never", 0, (afs_int32) -1 },
31 { "12/3/89", 0, 628664400 },
32 { "1/1/1", 0, 978325200 },
33 { "1/0/80", -2, 0 },
34 { "13/1/80", -2, 0 },
35 { "0/1/80", -2, 0 },
36 { "1/32/80", -2, 0 },
37 { "2/30/80", 0, 320734799 },
38 /*
39 * Oh, well: note that 2/30 is bigger than any real date in February, and
40 * since this algorithm approaches the correct value from below, this is
41 * the closest it can come.
42 */
43 { "3/1/80", 0, 320734800 },
44 { "3/1/80 0:00", 0, 320734800 },
45 { "2/30/80 24:00", -2, 0 },
46 { "2/30/80 23:60", -2, 0 },
47 { "22/22/22", -2, 0 },
48 { "12/31/69 19:07", 0, 420 },
49 { "12/31/99 23:59", 0, 946702740 },
50 { "12/31/99 23:59:59", 0, 946702799 },
51 { "23:12", -1, 0 },
52 { "22/12", -1, 0 },
53 { "22/22/22 12", -1, 0 },
54 { "12/31/199 23:59:59", -2, 0 },
55 { "12/31/1888", -2, 0 },
56 { "-13/-44/22 -15:77", -2, 0 },
57 { "4/14/24", 0, 1713070800 },
58 { "4/14/2024", 0, 1713070800 },
59 { "4/14/68", 0, 0x7fffffff }, /* legal but w/sign bit on */
60 { "4/14/69", 0, 0 },
61 { NULL, 0, 0 }
62 };
63
64 int
65 main(void)
66 {
67 long code;
68 afs_int32 temp;
69 time_t t;
70 struct testTime *tt;
71
72 plan((sizeof(testTimes) / sizeof(testTimes[0]) - 1) * 2);
73
74 /* should do timezone and daylight savings time correction so this program
75 * work in other than EST */
76 putenv("TZ=EST+5");
77
78 for (tt = testTimes; tt->time; tt++) {
79 temp = 0;
80 code = ktime_DateToLong(tt->time, &temp);
81 t = temp;
82 if (tt->code == 1) {
83 is_int(0, code, "ktime_DateToLong return for %s", tt->time);
84 ok((time(0) - t <= 1), "ktime_DateToLong result for %s", tt->time);
85 } else {
86 is_int(tt->code, code, "ktime_DateToLong return for %s", tt->time);
87 is_int(tt->sec, t, "ktime_DateToLong result for %s", tt->time);
88 }
89 }
90
91 return 0;
92 }