blob: 8cbf613bd4eb186ec68102ec7010a02c1bd33a62 [file] [log] [blame]
Lev Walkin4eceeba2007-07-23 06:48:26 +00001#include <stdio.h>
2#include <assert.h>
3#include <time.h>
4
Lev Walkin44212662004-08-19 13:26:54 +00005#include <GeneralizedTime.c>
Lev Walkin3a522782005-07-04 12:21:51 +00006#include <math.h> /* for pow(3) */
Lev Walkinf15320b2004-06-03 03:38:44 +00007
8static void
Lev Walkin535612a2005-07-03 05:32:40 +00009recognize(char *time_str, time_t expect, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +000010 GeneralizedTime_t gt;
11 struct tm tm;
12 time_t tloc;
Lev Walkin3a522782005-07-04 12:21:51 +000013 int fv, fp;
Lev Walkinf15320b2004-06-03 03:38:44 +000014
Lev Walkin535612a2005-07-03 05:32:40 +000015 gt.buf = (uint8_t *)time_str;
Lev Walkinf15320b2004-06-03 03:38:44 +000016 gt.size = strlen(time_str);
17
Lev Walkin3a522782005-07-04 12:21:51 +000018 tloc = asn_GT2time_frac(&gt, &fv, &fp, &tm, as_gmt);
Lev Walkin99006362004-08-07 03:52:26 +000019 printf("%s: [%s] -> %ld == %ld\n",
20 as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect);
Lev Walkina460ba32004-10-20 15:40:04 +000021
22 if(tloc != -1) {
Lev Walkin3a522782005-07-04 12:21:51 +000023 printf("\t%04d-%02d-%02dT%02d:%02d:%02d.%f(%d/%d)%+03ld%02ld\n",
Lev Walkinf15320b2004-06-03 03:38:44 +000024 tm.tm_year + 1900,
25 tm.tm_mon + 1,
26 tm.tm_mday,
27 tm.tm_hour,
28 tm.tm_min,
29 tm.tm_sec,
Lev Walkin3a522782005-07-04 12:21:51 +000030 (double)fv * pow(0.1, fp), fv, fp,
Lev Walkina460ba32004-10-20 15:40:04 +000031 (GMTOFF(tm) / 3600),
32 labs(GMTOFF(tm) % 3600)
33 );
34 }
Lev Walkin99006362004-08-07 03:52:26 +000035 assert(tloc == expect);
36
Lev Walkina460ba32004-10-20 15:40:04 +000037#ifdef HAVE_TM_GMTOFF
38 assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0);
39#endif
Lev Walkin99006362004-08-07 03:52:26 +000040
Lev Walkin535612a2005-07-03 05:32:40 +000041 if(!as_gmt) recognize(time_str, expect, 1);
Lev Walkin99006362004-08-07 03:52:26 +000042}
43
44static void
Lev Walkin535612a2005-07-03 05:32:40 +000045encode(time_t tloc, const char *expect, int force_gmt) {
Lev Walkin99006362004-08-07 03:52:26 +000046 GeneralizedTime_t *gt;
47 struct tm tm, *tmp;
48
49 tmp = localtime_r(&tloc, &tm);
50 assert(tmp);
51
52 gt = asn_time2GT(0, &tm, force_gmt);
53 if(gt) {
54 assert(expect);
55 printf("[%s] vs [%s] (%d)\n",
56 gt->buf, expect, force_gmt);
Lev Walkin494fb702017-08-07 20:07:00 -070057 assert(gt->size == strlen((char *)gt->buf));
Lev Walkin535612a2005-07-03 05:32:40 +000058 assert(!strcmp((char *)gt->buf, expect));
Lev Walkin99006362004-08-07 03:52:26 +000059 } else {
60 assert(!expect);
61 }
Lev Walkin229ad002017-09-18 20:13:49 -070062 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
Lev Walkinf15320b2004-06-03 03:38:44 +000063}
64
Lev Walkinc8c2cb52006-07-13 13:20:19 +000065#define RECODE(foo, bar) recode(__LINE__, foo, bar)
Lev Walkin535612a2005-07-03 05:32:40 +000066
67static void
Lev Walkinc8c2cb52006-07-13 13:20:19 +000068recode(int lineno, char *time_str, const char *expect) {
Lev Walkin3a522782005-07-04 12:21:51 +000069 int frac_value, frac_digits;
Lev Walkin535612a2005-07-03 05:32:40 +000070 GeneralizedTime_t gt;
71 struct tm tm;
72 time_t tloc;
Lev Walkinc8c2cb52006-07-13 13:20:19 +000073 char *tz;
Lev Walkin535612a2005-07-03 05:32:40 +000074
75 gt.buf = (uint8_t *)time_str;
76 gt.size = strlen(time_str);
77
Lev Walkin3a522782005-07-04 12:21:51 +000078 tloc = asn_GT2time_frac(&gt, &frac_value, &frac_digits, &tm, 1);
Lev Walkin535612a2005-07-03 05:32:40 +000079 assert(tloc != -1);
80
81 gt.buf = 0;
Lev Walkin3a522782005-07-04 12:21:51 +000082 asn_time2GT_frac(&gt, &tm, frac_value, frac_digits, 1);
Lev Walkin535612a2005-07-03 05:32:40 +000083 assert(gt.buf);
84
Lev Walkinc8c2cb52006-07-13 13:20:19 +000085 tz = getenv("TZ");
86 printf("%d: [%s] (%ld) => [%s] == [%s] (%d, %d) (TZ=%s)\n",
87 lineno, time_str, (long)tloc, gt.buf,
88 expect, frac_value, frac_digits,
89 tz ? tz : "");
Lev Walkin535612a2005-07-03 05:32:40 +000090
91 assert(strcmp((char *)gt.buf, expect) == 0);
92 FREEMEM(gt.buf);
93}
94
Lev Walkin4b4c1462005-07-04 01:44:01 +000095static void
96check_fractions() {
97 GeneralizedTime_t *gt = 0;
98 struct tm tm;
Lev Walkin3a522782005-07-04 12:21:51 +000099 int fv, fd;
100 time_t tloc;
Lev Walkin4b4c1462005-07-04 01:44:01 +0000101
102 memset(&tm, 0, sizeof tm);
103 tm.tm_year = 70;
104 tm.tm_mday = 1;
105
106 gt = asn_time2GT_frac(gt, &tm, -1, -1, 1);
107 assert(gt);
108 printf("[%s]\n", gt->buf);
109 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
110
111 gt = asn_time2GT_frac(gt, &tm, 0, 0, 1);
112 assert(gt);
113 printf("[%s]\n", gt->buf);
114 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
115
116 gt = asn_time2GT_frac(gt, &tm, 0, -1, 1);
117 assert(gt);
118 printf("[%s]\n", gt->buf);
119 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
120
121 gt = asn_time2GT_frac(gt, &tm, -1, 0, 1);
122 assert(gt);
123 printf("[%s]\n", gt->buf);
124 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
125
126 gt = asn_time2GT_frac(gt, &tm, 10, 0, 1);
127 assert(gt);
128 printf("[%s]\n", gt->buf);
129 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
130
Lev Walkin3a522782005-07-04 12:21:51 +0000131 /* Normalization should happen prior to calling the _frac() */
132 gt = asn_time2GT_frac(gt, &tm, 55, 2, 1);
133 assert(gt);
134 printf("[%s]\n", gt->buf);
135 assert(strcmp((char *)gt->buf, "19700101000000.55Z") == 0);
136
137 gt = asn_time2GT_frac(gt, &tm, 5, 2, 1);
138 assert(gt);
139 printf("[%s]\n", gt->buf);
140 assert(strcmp((char *)gt->buf, "19700101000000.05Z") == 0);
141
Lev Walkin4b4c1462005-07-04 01:44:01 +0000142 /* Normalization should happen prior calling the _frac() */
Lev Walkin3a522782005-07-04 12:21:51 +0000143 gt = asn_time2GT_frac(gt, &tm, 900, 2, 1);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000144 assert(gt);
145 printf("[%s]\n", gt->buf);
146 assert(strcmp((char *)gt->buf, "19700101000000Z") == 0);
147
Lev Walkin3a522782005-07-04 12:21:51 +0000148 gt = asn_time2GT_frac(gt, &tm, 90, 2, 1);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000149 assert(gt);
150 printf("[%s]\n", gt->buf);
Lev Walkin3a522782005-07-04 12:21:51 +0000151 assert(strcmp((char *)gt->buf, "19700101000000.9Z") == 0);
Lev Walkin88e0a772005-07-04 02:29:36 +0000152
Lev Walkin3a522782005-07-04 12:21:51 +0000153 tloc = asn_GT2time_prec(gt, &fv, 0, 0, 1);
154 assert(tloc == 0);
155 assert(fv == 0);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000156
Lev Walkin3a522782005-07-04 12:21:51 +0000157 tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1);
158 assert(tloc == 0);
159 assert(fv == 9);
Lev Walkin4b4c1462005-07-04 01:44:01 +0000160
Lev Walkin3a522782005-07-04 12:21:51 +0000161 tloc = asn_GT2time_prec(gt, &fv, 2, 0, 1);
162 assert(tloc == 0);
163 assert(fv == 90);
Lev Walkin10dd97c2005-07-04 02:20:26 +0000164
Lev Walkin3a522782005-07-04 12:21:51 +0000165 tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
166 assert(tloc == 0);
167 assert(fv == 9);
168 assert(fd == 1);
Lev Walkin88e0a772005-07-04 02:29:36 +0000169
Lev Walkin3a522782005-07-04 12:21:51 +0000170 gt->buf[gt->size-1] = '0';
171 gt->buf[gt->size++] = 'Z';
172 gt->buf[gt->size] = '\0';
Lev Walkin10dd97c2005-07-04 02:20:26 +0000173
Lev Walkin3a522782005-07-04 12:21:51 +0000174 tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1);
175 assert(tloc == 0);
176 assert(fd == 2);
177 assert(fv == 90);
Lev Walkin88e0a772005-07-04 02:29:36 +0000178
Lev Walkin3a522782005-07-04 12:21:51 +0000179 tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1);
180 assert(tloc == 0);
181 assert(fv == 9);
182
183 tloc = asn_GT2time_prec(gt, &fv, 100, 0, 1);
184 assert(tloc == 0);
185 assert(fv == 0);
Lev Walkin10dd97c2005-07-04 02:20:26 +0000186
Lev Walkin4b4c1462005-07-04 01:44:01 +0000187 FREEMEM(gt->buf);
188 FREEMEM(gt);
189}
190
Lev Walkindd01a5e2017-10-14 01:45:26 -0700191static void
192compare(int lineno, int cmp_control, const char *astr, const char *bstr) {
Vasil Velichkovd346ffd2017-10-19 05:29:25 +0300193 GeneralizedTime_t a = {(uint8_t *)strdup(astr), strlen(astr), {0, 0, 0, 0, 0}};
194 GeneralizedTime_t b = {(uint8_t *)strdup(bstr), strlen(bstr), {0, 0, 0, 0, 0}};
Lev Walkindd01a5e2017-10-14 01:45:26 -0700195 int cmp_result = asn_DEF_GeneralizedTime.op->compare_struct(
196 &asn_DEF_GeneralizedTime, &a, &b);
197 if(cmp_result != cmp_control) {
198 fprintf(stderr, "%03d: [%s] == [%s] = %d, expected %d\n", lineno, astr,
199 bstr, cmp_result, cmp_control);
200 assert(cmp_result == cmp_control);
201 }
202 ASN_STRUCT_RESET(asn_DEF_GeneralizedTime, &a);
203 ASN_STRUCT_RESET(asn_DEF_GeneralizedTime, &b);
204}
205
Lev Walkinf15320b2004-06-03 03:38:44 +0000206int
207main(int ac, char **av) {
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000208 char *tz = getenv("TZ");
Lev Walkinf15320b2004-06-03 03:38:44 +0000209
Lev Walkin44212662004-08-19 13:26:54 +0000210 (void)av;
211
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000212 printf("TZ = [%s]\n", tz ? tz : "");
213
Lev Walkin4b4c1462005-07-04 01:44:01 +0000214 check_fractions();
215
Lev Walkin535612a2005-07-03 05:32:40 +0000216 recognize("200401250", -1, 0);
217 recognize("2004012509300", -1, 0);
218 recognize("20040125093000-", -1, 0);
219 recognize("20040125093007-0", -1, 0);
220 recognize("20040125093007-080", -1, 0);
221 recognize("200401250930.01Z", -1, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000222
Lev Walkin99006362004-08-07 03:52:26 +0000223 /* These six are from X.690:11.7.5 */
Lev Walkin535612a2005-07-03 05:32:40 +0000224 recognize("19920520240000Z", -1, 0); /* midnight represented incorrectly */
225 recognize("19920622123421.0Z", 709216461, 0); /* spurious trailing zeros */
226 recognize("19920722132100.30Z", 711811260, 0); /* spurious trailing zeros */
227 recognize("19920521000000Z", 706406400, 0);
228 recognize("19920622123421Z", 709216461, 0);
229 recognize("19920722132100.3Z", 711811260, 0);
Lev Walkin99006362004-08-07 03:52:26 +0000230
Lev Walkin535612a2005-07-03 05:32:40 +0000231 recognize("20040125093007Z", 1075023007, 0);
232 recognize("20040125093007+00", 1075023007, 0);
233 recognize("20040125093007.01+0000", 1075023007, 0);
234 recognize("20040125093007,1+0000", 1075023007, 0);
235 recognize("20040125093007-0800", 1075051807, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000236
Lev Walkin535612a2005-07-03 05:32:40 +0000237 recognize("19920722132100.123000123Z", 711811260, 0);
238 recognize("19920722132100.1230000123Z", 711811260, 0);
239 recognize("19920722132100.12300000123Z", 711811260, 0);
240
241 encode(1075023007, "20040125093007Z", 1);
Lev Walkin91d40d42004-08-07 04:16:42 +0000242
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 if(ac > 1) {
244 /* These will be valid only inside PST time zone */
Lev Walkin535612a2005-07-03 05:32:40 +0000245 recognize("20040125093007", 1075051807, 0);
246 recognize("200401250930", 1075051800, 0);
247 recognize("20040125093000,01", 1075051800, 0);
248 recognize("20040125093000,1234", 1075051800, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000249
Lev Walkin535612a2005-07-03 05:32:40 +0000250 encode(1075023007, "20040125013007-0800", 0);
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000251 RECODE("20050702123312", "20050702193312Z");
Lev Walkin91d40d42004-08-07 04:16:42 +0000252 }
Lev Walkin99006362004-08-07 03:52:26 +0000253
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000254#if defined(sun) || defined(__sun) || defined(_sun_) || defined(__solaris__)
255 printf("Solaris does not have a decent timegm() function.\n");
256#else /* !solaris */
257 RECODE("20050702123312Z", "20050702123312Z");
258 RECODE("20050702123312+01", "20050702113312Z");
259 RECODE("20050702123312,0+01", "20050702113312Z");
260 RECODE("20050702123312,1+01", "20050702113312.1Z");
261 RECODE("20050702123312.01+01", "20050702113312.01Z");
262 RECODE("20050702123312.00+01", "20050702113312Z");
263 RECODE("20050702123312.30+01", "20050702113312.3Z");
264 RECODE("20050702123312,30000+01", "20050702113312.3Z");
265 RECODE("20050702123312,300000000+01", "20050702113312.3Z");
266 RECODE("20050702123312.123456+01", "20050702113312.123456Z");
Russ Kubik94d87942018-04-25 17:53:59 -0400267 RECODE("20050702123312.1234567+01", "20050702113312.1234567Z");
268 RECODE("20050702123312.12345678+01", "20050702113312.12345678Z");
269 RECODE("20050702123312.123456789+01", "20050702113312.123456789Z");
270 RECODE("20050702123312.1234567891+01", "20050702113312.123456789Z");
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000271 RECODE("20050702123312.2000000000+01", "20050702113312.2Z");
272 RECODE("20050702123312.3000000000+01", "20050702113312.3Z");
273 RECODE("20050702123312.4000000000+01", "20050702113312.4Z");
274 RECODE("20050702123312.5000000000+01", "20050702113312.5Z");
Russ Kubik94d87942018-04-25 17:53:59 -0400275 RECODE("20050702123312.5000000011+01", "20050702113312.500000001Z");
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000276 RECODE("20050702123312.5000000001+01", "20050702113312.5Z");
Russ Kubik94d87942018-04-25 17:53:59 -0400277 RECODE("20050702123312.000000001+01", "20050702113312.000000001Z");
278 RECODE("20050702123312.0000000001Z", "20050702123312Z");
Russ Kubik3ce07b92018-04-25 18:06:51 -0400279 RECODE("20050702123312.0000000100Z", "20050702123312.00000001Z");
Russ Kubik94d87942018-04-25 17:53:59 -0400280 RECODE("20050702123312.0080000010+1056", "20050702013712.008000001Z");
Lev Walkinc8c2cb52006-07-13 13:20:19 +0000281#endif
Lev Walkin535612a2005-07-03 05:32:40 +0000282
Lev Walkindd01a5e2017-10-14 01:45:26 -0700283 compare(__LINE__, 0, "20040125093007", "20040125093007");
284 compare(__LINE__, 0, "20040125093007-0000", "20040125093007Z");
285 compare(__LINE__, 1, "20040125093008", "20040125093007");
286 compare(__LINE__, 1, "20040125093008-0000", "20040125093007-0000");
287 compare(__LINE__, 0, "20040125093008-0000", "20040125093008-0000");
288 compare(__LINE__, 1, "20040125093008-0000", "20040125093007Z");
289 compare(__LINE__, 0, "20040125093007-0000", "20040125093007+0000");
290 compare(__LINE__, 1, "20040125093007-0030", "20040125093007Z");
291 compare(__LINE__, -1, "20040125093007+0030", "20040125093007Z");
292
Lev Walkinf15320b2004-06-03 03:38:44 +0000293 return 0;
294}
Lev Walkin99006362004-08-07 03:52:26 +0000295