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 | 4eceeba | 2007-07-23 06:48:26 +0000 | [diff] [blame] | 6 | #include <UTCTime.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | |
| 8 | static void |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 9 | check(char *time_str, time_t sample, int as_gmt) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | UTCTime_t gt; |
| 11 | struct tm tm; |
| 12 | time_t tloc; |
| 13 | |
Lev Walkin | 535612a | 2005-07-03 05:32:40 +0000 | [diff] [blame] | 14 | gt.buf = (uint8_t *)time_str; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | gt.size = strlen(time_str); |
| 16 | |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 17 | tloc = asn_UT2time(>, &tm, as_gmt); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | printf("[%s] -> %ld == %ld\n", time_str, (long)tloc, (long)sample); |
| 19 | if(tloc != -1) |
| 20 | printf("\t%d-%d-%dT%02d:%02d:%02d %ld\n", |
| 21 | tm.tm_year + 1900, |
| 22 | tm.tm_mon + 1, |
| 23 | tm.tm_mday, |
| 24 | tm.tm_hour, |
| 25 | tm.tm_min, |
| 26 | tm.tm_sec, |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 27 | GMTOFF(tm) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | ); |
| 29 | assert(tloc == sample); |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 30 | |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 31 | assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0); |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 32 | |
| 33 | if(as_gmt) check(time_str, sample, as_gmt); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Lev Walkin | dd01a5e | 2017-10-14 01:45:26 -0700 | [diff] [blame] | 36 | static void |
| 37 | compare(int lineno, int cmp_control, const char *astr, const char *bstr) { |
| 38 | UTCTime_t a = {(uint8_t *)strdup(astr), strlen(astr)}; |
| 39 | UTCTime_t b = {(uint8_t *)strdup(bstr), strlen(bstr)}; |
| 40 | int cmp_result = |
| 41 | asn_DEF_UTCTime.op->compare_struct(&asn_DEF_UTCTime, &a, &b); |
| 42 | if(cmp_result != cmp_control) { |
| 43 | fprintf(stderr, "%03d: [%s] == [%s] = %d, expected %d\n", lineno, astr, |
| 44 | bstr, cmp_result, cmp_control); |
| 45 | assert(cmp_result == cmp_control); |
| 46 | } |
| 47 | ASN_STRUCT_RESET(asn_DEF_UTCTime, &a); |
| 48 | ASN_STRUCT_RESET(asn_DEF_UTCTime, &b); |
| 49 | } |
| 50 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | int |
| 52 | main(int ac, char **av) { |
| 53 | |
Lev Walkin | 4421266 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 54 | (void)av; |
| 55 | |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 56 | check("0401250", -1, 0); |
| 57 | check("0401250930", -1, 0); /* "Z" or "(+|-)hhmm" required */ |
| 58 | check("04012509300", -1, 0); |
| 59 | check("040125093000-", -1, 0); |
| 60 | check("040125093007-0", -1, 0); |
| 61 | check("040125093007-080", -1, 0); |
| 62 | check("0401250930.01Z", -1, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 63 | |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 64 | check("040125093007Z", 1075023007, 0); |
| 65 | check("040125093007+00", 1075023007, 0); |
| 66 | check("040125093007-0800", 1075051807, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | |
| 68 | if(ac > 1) { |
| 69 | /* These will be valid only inside PST time zone */ |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 70 | check("040125093007", 1075051807, 0); |
| 71 | check("040125093000,01", 1075051800, 0); |
| 72 | check("040125093000,1234", 1075051800, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Lev Walkin | dd01a5e | 2017-10-14 01:45:26 -0700 | [diff] [blame] | 75 | compare(__LINE__, 0, "040125093007", "040125093007"); |
| 76 | compare(__LINE__, 0, "040125093007-0000", "040125093007Z"); |
| 77 | compare(__LINE__, 1, "040125093008", "040125093007"); |
| 78 | compare(__LINE__, 1, "040125093008-0000", "040125093007-0000"); |
| 79 | compare(__LINE__, 0, "040125093008-0000", "040125093008-0000"); |
| 80 | compare(__LINE__, 1, "040125093008-0000", "040125093007Z"); |
| 81 | compare(__LINE__, 0, "040125093007-0000", "040125093007+0000"); |
| 82 | compare(__LINE__, 1, "040125093007-0030", "040125093007Z"); |
| 83 | compare(__LINE__, -1, "040125093007+0030", "040125093007Z"); |
| 84 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | return 0; |
| 86 | } |
Lev Walkin | 0aca385 | 2004-08-07 03:51:43 +0000 | [diff] [blame] | 87 | |