blob: 65882d45a76b019bc233536a6c3c7d32d4bf199f [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#define __NO_ASN_TABLE__
2#include "../GeneralizedTime.c"
3#include "../constraints.c"
4
5static void
Lev Walkin99006362004-08-07 03:52:26 +00006check(char *time_str, time_t expect, int as_gmt) {
Lev Walkinf15320b2004-06-03 03:38:44 +00007 GeneralizedTime_t gt;
8 struct tm tm;
9 time_t tloc;
10
11 gt.buf = time_str;
12 gt.size = strlen(time_str);
13
Lev Walkin99006362004-08-07 03:52:26 +000014 tloc = asn_GT2time(&gt, &tm, as_gmt);
15 printf("%s: [%s] -> %ld == %ld\n",
16 as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect);
Lev Walkinf15320b2004-06-03 03:38:44 +000017 if(tloc != -1)
Lev Walkin99006362004-08-07 03:52:26 +000018 printf("\t%04d-%02d-%02dT%02d:%02d:%02d%+03ld%02ld\n",
Lev Walkinf15320b2004-06-03 03:38:44 +000019 tm.tm_year + 1900,
20 tm.tm_mon + 1,
21 tm.tm_mday,
22 tm.tm_hour,
23 tm.tm_min,
24 tm.tm_sec,
Lev Walkin99006362004-08-07 03:52:26 +000025 (tm.tm_gmtoff / 3600),
26 labs(tm.tm_gmtoff % 3600)
Lev Walkinf15320b2004-06-03 03:38:44 +000027 );
Lev Walkin99006362004-08-07 03:52:26 +000028 assert(tloc == expect);
29
30 assert(tloc == -1 || as_gmt == 0 || tm.tm_gmtoff == 0);
31
32 if(!as_gmt) check(time_str, expect, 1);
33}
34
35static void
36rcheck(time_t tloc, const char *expect, int force_gmt) {
37 GeneralizedTime_t *gt;
38 struct tm tm, *tmp;
39
40 tmp = localtime_r(&tloc, &tm);
41 assert(tmp);
42
43 gt = asn_time2GT(0, &tm, force_gmt);
44 if(gt) {
45 assert(expect);
46 printf("[%s] vs [%s] (%d)\n",
47 gt->buf, expect, force_gmt);
48 assert(gt->size == strlen(gt->buf));
49 assert(!strcmp(gt->buf, expect));
50 } else {
51 assert(!expect);
52 }
Lev Walkinf15320b2004-06-03 03:38:44 +000053}
54
55int
56main(int ac, char **av) {
57
Lev Walkin99006362004-08-07 03:52:26 +000058 check("200401250", -1, 0);
59 check("2004012509300", -1, 0);
60 check("20040125093000-", -1, 0);
61 check("20040125093007-0", -1, 0);
62 check("20040125093007-080", -1, 0);
63 check("200401250930.01Z", -1, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000064
Lev Walkin99006362004-08-07 03:52:26 +000065 /* These six are from X.690:11.7.5 */
66 check("19920520240000Z", -1, 0); /* midnight represented incorrectly */
67 //check("19920622123421.0Z", -1, 0); /* spurious trailing zeros */
68 //check("19920722132100.30Z", -1, 0); /* spurious trailing zeros */
69 check("19920521000000Z", 706406400, 0);
70 check("19920622123421Z", 709216461, 0);
71 check("19920722132100.3Z", 711811260, 0);
72
73 check("20040125093007Z", 1075023007, 0);
74 check("20040125093007+00", 1075023007, 0);
75 check("20040125093007.01+0000", 1075023007, 0);
76 check("20040125093007,1+0000", 1075023007, 0);
77 check("20040125093007-0800", 1075051807, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000078
Lev Walkin91d40d42004-08-07 04:16:42 +000079 rcheck(1075023007, "20040125093007Z", 1);
80
Lev Walkinf15320b2004-06-03 03:38:44 +000081 if(ac > 1) {
82 /* These will be valid only inside PST time zone */
Lev Walkin99006362004-08-07 03:52:26 +000083 check("20040125093007", 1075051807, 0);
84 check("200401250930", 1075051800, 0);
85 check("20040125093000,01", 1075051800, 0);
86 check("20040125093000,1234", 1075051800, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000087
Lev Walkin91d40d42004-08-07 04:16:42 +000088 rcheck(1075023007, "20040125013007-0800", 0);
89 }
Lev Walkin99006362004-08-07 03:52:26 +000090
Lev Walkinf15320b2004-06-03 03:38:44 +000091 return 0;
92}
Lev Walkin99006362004-08-07 03:52:26 +000093
94/*
95 * Dummy function.
96 */
97
98der_enc_rval_t
99OCTET_STRING_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
100 der_enc_rval_t erval;
101 return erval;
102}