Lev Walkin | 4eceeba | 2007-07-23 06:48:26 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <assert.h> |
| 3 | #include <time.h> |
| 4 | |
Lev Walkin | 4421266 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 5 | #include <GeneralizedTime.c> |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 6 | #include <math.h> /* for pow(3) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | |
| 8 | static void |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 9 | recognize(char *time_str, time_t expect, int as_gmt) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | GeneralizedTime_t gt; |
| 11 | struct tm tm; |
| 12 | time_t tloc; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 13 | int fv, fp; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 15 | gt.buf = (uint8_t *)time_str; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | gt.size = strlen(time_str); |
| 17 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 18 | tloc = asn_GT2time_frac(>, &fv, &fp, &tm, as_gmt); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 19 | printf("%s: [%s] -> %ld == %ld\n", |
| 20 | as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect); |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 21 | |
| 22 | if(tloc != -1) { |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 23 | printf("\t%04d-%02d-%02dT%02d:%02d:%02d.%f(%d/%d)%+03ld%02ld\n", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | 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 Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 30 | (double)fv * pow(0.1, fp), fv, fp, |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 31 | (GMTOFF(tm) / 3600), |
| 32 | labs(GMTOFF(tm) % 3600) |
| 33 | ); |
| 34 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 35 | assert(tloc == expect); |
| 36 | |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 37 | #ifdef HAVE_TM_GMTOFF |
| 38 | assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0); |
| 39 | #endif |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 40 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 41 | if(!as_gmt) recognize(time_str, expect, 1); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 45 | encode(time_t tloc, const char *expect, int force_gmt) { |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 46 | 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 Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 57 | assert(gt->size == strlen((char *)gt->buf)); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 58 | assert(!strcmp((char *)gt->buf, expect)); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 59 | } else { |
| 60 | assert(!expect); |
| 61 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 64 | #define RECODE(foo, bar) recode(__LINE__, foo, bar) |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 65 | |
| 66 | static void |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 67 | recode(int lineno, char *time_str, const char *expect) { |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 68 | int frac_value, frac_digits; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 69 | GeneralizedTime_t gt; |
| 70 | struct tm tm; |
| 71 | time_t tloc; |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 72 | char *tz; |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 73 | |
| 74 | gt.buf = (uint8_t *)time_str; |
| 75 | gt.size = strlen(time_str); |
| 76 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 77 | tloc = asn_GT2time_frac(>, &frac_value, &frac_digits, &tm, 1); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 78 | assert(tloc != -1); |
| 79 | |
| 80 | gt.buf = 0; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 81 | asn_time2GT_frac(>, &tm, frac_value, frac_digits, 1); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 82 | assert(gt.buf); |
| 83 | |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 84 | tz = getenv("TZ"); |
| 85 | printf("%d: [%s] (%ld) => [%s] == [%s] (%d, %d) (TZ=%s)\n", |
| 86 | lineno, time_str, (long)tloc, gt.buf, |
| 87 | expect, frac_value, frac_digits, |
| 88 | tz ? tz : ""); |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 89 | |
| 90 | assert(strcmp((char *)gt.buf, expect) == 0); |
| 91 | FREEMEM(gt.buf); |
| 92 | } |
| 93 | |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 94 | static void |
| 95 | check_fractions() { |
| 96 | GeneralizedTime_t *gt = 0; |
| 97 | struct tm tm; |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 98 | int fv, fd; |
| 99 | time_t tloc; |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 100 | |
| 101 | memset(&tm, 0, sizeof tm); |
| 102 | tm.tm_year = 70; |
| 103 | tm.tm_mday = 1; |
| 104 | |
| 105 | gt = asn_time2GT_frac(gt, &tm, -1, -1, 1); |
| 106 | assert(gt); |
| 107 | printf("[%s]\n", gt->buf); |
| 108 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 109 | |
| 110 | gt = asn_time2GT_frac(gt, &tm, 0, 0, 1); |
| 111 | assert(gt); |
| 112 | printf("[%s]\n", gt->buf); |
| 113 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 114 | |
| 115 | gt = asn_time2GT_frac(gt, &tm, 0, -1, 1); |
| 116 | assert(gt); |
| 117 | printf("[%s]\n", gt->buf); |
| 118 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 119 | |
| 120 | gt = asn_time2GT_frac(gt, &tm, -1, 0, 1); |
| 121 | assert(gt); |
| 122 | printf("[%s]\n", gt->buf); |
| 123 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 124 | |
| 125 | gt = asn_time2GT_frac(gt, &tm, 10, 0, 1); |
| 126 | assert(gt); |
| 127 | printf("[%s]\n", gt->buf); |
| 128 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 129 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 130 | /* Normalization should happen prior to calling the _frac() */ |
| 131 | gt = asn_time2GT_frac(gt, &tm, 55, 2, 1); |
| 132 | assert(gt); |
| 133 | printf("[%s]\n", gt->buf); |
| 134 | assert(strcmp((char *)gt->buf, "19700101000000.55Z") == 0); |
| 135 | |
| 136 | gt = asn_time2GT_frac(gt, &tm, 5, 2, 1); |
| 137 | assert(gt); |
| 138 | printf("[%s]\n", gt->buf); |
| 139 | assert(strcmp((char *)gt->buf, "19700101000000.05Z") == 0); |
| 140 | |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 141 | /* Normalization should happen prior calling the _frac() */ |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 142 | gt = asn_time2GT_frac(gt, &tm, 900, 2, 1); |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 143 | assert(gt); |
| 144 | printf("[%s]\n", gt->buf); |
| 145 | assert(strcmp((char *)gt->buf, "19700101000000Z") == 0); |
| 146 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 147 | gt = asn_time2GT_frac(gt, &tm, 90, 2, 1); |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 148 | assert(gt); |
| 149 | printf("[%s]\n", gt->buf); |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 150 | assert(strcmp((char *)gt->buf, "19700101000000.9Z") == 0); |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 151 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 152 | tloc = asn_GT2time_prec(gt, &fv, 0, 0, 1); |
| 153 | assert(tloc == 0); |
| 154 | assert(fv == 0); |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 155 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 156 | tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1); |
| 157 | assert(tloc == 0); |
| 158 | assert(fv == 9); |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 159 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 160 | tloc = asn_GT2time_prec(gt, &fv, 2, 0, 1); |
| 161 | assert(tloc == 0); |
| 162 | assert(fv == 90); |
Lev Walkin | 10dd97c | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 163 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 164 | tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1); |
| 165 | assert(tloc == 0); |
| 166 | assert(fv == 9); |
| 167 | assert(fd == 1); |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 168 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 169 | gt->buf[gt->size-1] = '0'; |
| 170 | gt->buf[gt->size++] = 'Z'; |
| 171 | gt->buf[gt->size] = '\0'; |
Lev Walkin | 10dd97c | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 172 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 173 | tloc = asn_GT2time_frac(gt, &fv, &fd, 0, 1); |
| 174 | assert(tloc == 0); |
| 175 | assert(fd == 2); |
| 176 | assert(fv == 90); |
Lev Walkin | 88e0a77 | 2005-07-04 02:29:36 +0000 | [diff] [blame] | 177 | |
Lev Walkin | 3a52278 | 2005-07-04 12:21:51 +0000 | [diff] [blame] | 178 | tloc = asn_GT2time_prec(gt, &fv, 1, 0, 1); |
| 179 | assert(tloc == 0); |
| 180 | assert(fv == 9); |
| 181 | |
| 182 | tloc = asn_GT2time_prec(gt, &fv, 100, 0, 1); |
| 183 | assert(tloc == 0); |
| 184 | assert(fv == 0); |
Lev Walkin | 10dd97c | 2005-07-04 02:20:26 +0000 | [diff] [blame] | 185 | |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 186 | FREEMEM(gt->buf); |
| 187 | FREEMEM(gt); |
| 188 | } |
| 189 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 190 | int |
| 191 | main(int ac, char **av) { |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 192 | char *tz = getenv("TZ"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | |
Lev Walkin | 4421266 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 194 | (void)av; |
| 195 | |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 196 | printf("TZ = [%s]\n", tz ? tz : ""); |
| 197 | |
Lev Walkin | 4b4c146 | 2005-07-04 01:44:01 +0000 | [diff] [blame] | 198 | check_fractions(); |
| 199 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 200 | recognize("200401250", -1, 0); |
| 201 | recognize("2004012509300", -1, 0); |
| 202 | recognize("20040125093000-", -1, 0); |
| 203 | recognize("20040125093007-0", -1, 0); |
| 204 | recognize("20040125093007-080", -1, 0); |
| 205 | recognize("200401250930.01Z", -1, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 207 | /* These six are from X.690:11.7.5 */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 208 | recognize("19920520240000Z", -1, 0); /* midnight represented incorrectly */ |
| 209 | recognize("19920622123421.0Z", 709216461, 0); /* spurious trailing zeros */ |
| 210 | recognize("19920722132100.30Z", 711811260, 0); /* spurious trailing zeros */ |
| 211 | recognize("19920521000000Z", 706406400, 0); |
| 212 | recognize("19920622123421Z", 709216461, 0); |
| 213 | recognize("19920722132100.3Z", 711811260, 0); |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 214 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 215 | recognize("20040125093007Z", 1075023007, 0); |
| 216 | recognize("20040125093007+00", 1075023007, 0); |
| 217 | recognize("20040125093007.01+0000", 1075023007, 0); |
| 218 | recognize("20040125093007,1+0000", 1075023007, 0); |
| 219 | recognize("20040125093007-0800", 1075051807, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 220 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 221 | recognize("19920722132100.123000123Z", 711811260, 0); |
| 222 | recognize("19920722132100.1230000123Z", 711811260, 0); |
| 223 | recognize("19920722132100.12300000123Z", 711811260, 0); |
| 224 | |
| 225 | encode(1075023007, "20040125093007Z", 1); |
Lev Walkin | 91d40d4 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 226 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 227 | if(ac > 1) { |
| 228 | /* These will be valid only inside PST time zone */ |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 229 | recognize("20040125093007", 1075051807, 0); |
| 230 | recognize("200401250930", 1075051800, 0); |
| 231 | recognize("20040125093000,01", 1075051800, 0); |
| 232 | recognize("20040125093000,1234", 1075051800, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 233 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 234 | encode(1075023007, "20040125013007-0800", 0); |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 235 | RECODE("20050702123312", "20050702193312Z"); |
Lev Walkin | 91d40d4 | 2004-08-07 04:16:42 +0000 | [diff] [blame] | 236 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 237 | |
Lev Walkin | c8c2cb5 | 2006-07-13 13:20:19 +0000 | [diff] [blame] | 238 | #if defined(sun) || defined(__sun) || defined(_sun_) || defined(__solaris__) |
| 239 | printf("Solaris does not have a decent timegm() function.\n"); |
| 240 | #else /* !solaris */ |
| 241 | RECODE("20050702123312Z", "20050702123312Z"); |
| 242 | RECODE("20050702123312+01", "20050702113312Z"); |
| 243 | RECODE("20050702123312,0+01", "20050702113312Z"); |
| 244 | RECODE("20050702123312,1+01", "20050702113312.1Z"); |
| 245 | RECODE("20050702123312.01+01", "20050702113312.01Z"); |
| 246 | RECODE("20050702123312.00+01", "20050702113312Z"); |
| 247 | RECODE("20050702123312.30+01", "20050702113312.3Z"); |
| 248 | RECODE("20050702123312,30000+01", "20050702113312.3Z"); |
| 249 | RECODE("20050702123312,300000000+01", "20050702113312.3Z"); |
| 250 | RECODE("20050702123312.123456+01", "20050702113312.123456Z"); |
| 251 | RECODE("20050702123312.1234567+01", "20050702113312.123456Z"); |
| 252 | RECODE("20050702123312.12345678+01", "20050702113312.123456Z"); |
| 253 | RECODE("20050702123312.123456789+01", "20050702113312.123456Z"); |
| 254 | RECODE("20050702123312.2000000000+01", "20050702113312.2Z"); |
| 255 | RECODE("20050702123312.3000000000+01", "20050702113312.3Z"); |
| 256 | RECODE("20050702123312.4000000000+01", "20050702113312.4Z"); |
| 257 | RECODE("20050702123312.5000000000+01", "20050702113312.5Z"); |
| 258 | RECODE("20050702123312.5000000001+01", "20050702113312.5Z"); |
| 259 | RECODE("20050702123312.5000010001+01", "20050702113312.500001Z"); |
| 260 | RECODE("20050702123312.5000001001+01", "20050702113312.5Z"); |
| 261 | RECODE("20050702123312.000001+01", "20050702113312.000001Z"); |
| 262 | RECODE("20050702123312.0000001Z", "20050702123312Z"); |
| 263 | RECODE("20050702123312.0080010+1056", "20050702013712.008001Z"); |
| 264 | #endif |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 265 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | } |
Lev Walkin | 9900636 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 268 | |