-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_misc.c
More file actions
138 lines (123 loc) · 5.28 KB
/
Copy patht_misc.c
File metadata and controls
138 lines (123 loc) · 5.28 KB
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
/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
/**
* Copyright: 2013 Red Hat, Inc.
* Author: Nathaniel McCallum <npmccallum@redhat.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "iso8601.h"
#include <stdio.h>
#include <assert.h>
int main(int argc, const char **argv)
{
iso8601_time ta, tb;
time_t a, b;
setenv("TZ", "EST+5", 1);
do {
assert((a = time(NULL)) != (time_t)-1);
assert(iso8601_current(false, 0, &ta) == 0);
assert((b = time(NULL)) != (time_t)-1);
} while (a != b);
/*
* Check comparison.
*
* NOTE: iso8601_from_time_t() exercises all of the iso8601_from_*()
* functions internally.
*/
iso8601_from_time_t(b, ta.usecond, false, 0, &tb);
assert(iso8601_compare(&ta, &tb) == 0);
iso8601_from_time_t(b - 1, ta.usecond, false, 0, &tb);
assert(iso8601_compare(&ta, &tb) > 0);
iso8601_from_time_t(b + 1, ta.usecond, false, 0, &tb);
assert(iso8601_compare(&ta, &tb) < 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 1, 1 },
&(iso8601_time) { 2000, 2, 1 }) < 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 2, 1 },
&(iso8601_time) { 2000, 1, 1 }) > 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 1, 1 },
&(iso8601_time) { 2000, 1, 2 }) < 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 1, 2 },
&(iso8601_time) { 2000, 1, 1 }) > 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 1, 1, 0 },
&(iso8601_time) { 2000, 1, 1, 1 }) < 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 1, 1, 1 },
&(iso8601_time) { 2000, 1, 1, 0 }) > 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 1, 1, 0, 0 },
&(iso8601_time) { 2000, 1, 1, 0, 1 }) < 0);
assert(iso8601_compare(&(iso8601_time) { 2000, 1, 1, 0, 1 },
&(iso8601_time) { 2000, 1, 1, 0, 0 }) > 0);
/*
* Check conversion to time_t.
*
* NOTE: iso8601_from_time_t() exercises all of the iso8601_from_*()
* functions internally.
*/
iso8601_to_time_t(&ta, &a);
assert(a == b);
/* Check comparison against UTC from local time. */
iso8601_from_time_t(0, 0, true, 0, &ta);
iso8601_from_time_t(0, 0, false, 0, &tb);
assert(iso8601_compare(&ta, &tb) == 0);
/* Check comparison against UTC from offset time. */
iso8601_from_time_t(0, 0, false, 0, &ta);
iso8601_from_time_t(0, 0, false, 120, &tb);
assert(iso8601_compare(&ta, &tb) == 0);
/* Check comparison against offset time from local time. */
iso8601_from_time_t(0, 0, true, 0, &ta);
iso8601_from_time_t(0, 0, false, 120, &tb);
assert(iso8601_compare(&ta, &tb) == 0);
/* Check usecond comparison. */
iso8601_from_time_t(0, 0, true, 0, &ta);
iso8601_from_time_t(0, 500000, false, 120, &tb);
assert(iso8601_compare(&ta, &tb) < 0);
assert(iso8601_compare(&tb, &ta) > 0);
/* Check negative conversion. */
a = -157766400;
iso8601_from_time_t(a, 0, false, 0, &ta);
iso8601_to_time_t(&ta, &b);
assert(a == b);
/* Check comparison of large dates. */
assert(iso8601_parse("1970-01-01T00:00:00Z", &ta) == 0);
assert(iso8601_parse("9999-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) < 0);
assert(iso8601_compare(&tb, &ta) > 0);
assert(iso8601_parse("+99999-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) < 0);
assert(iso8601_compare(&tb, &ta) > 0);
assert(iso8601_parse("-0100-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) > 0);
assert(iso8601_compare(&tb, &ta) < 0);
assert(iso8601_parse("9999-01-01T00:00:01Z", &ta) == 0);
assert(iso8601_parse("9999-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) > 0);
assert(iso8601_compare(&tb, &ta) < 0);
/* Check comparison of large dates in different zones. */
assert(iso8601_parse("1970-01-01T00:00:00", &ta) == 0);
assert(iso8601_parse("9999-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) < 0);
assert(iso8601_compare(&tb, &ta) > 0);
assert(iso8601_parse("+99999-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) < 0);
assert(iso8601_compare(&tb, &ta) > 0);
assert(iso8601_parse("-0100-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) > 0);
assert(iso8601_compare(&tb, &ta) < 0);
#if SIZEOF_TIME_T > 4
/* Fails when time_t is too small to represent the dates. */
assert(iso8601_parse("9999-01-01T00:00:01", &ta) == 0);
assert(iso8601_parse("9999-01-01T00:00:00Z", &tb) == 0);
assert(iso8601_compare(&ta, &tb) > 0);
assert(iso8601_compare(&tb, &ta) < 0);
#endif
return 0;
}