| 1 | /* -*- Mode: C; c-file-style: "stroustrup" -*- */cxx . . $ `<file>/utct.c`/ |
| 2 | |
| 3 | #include <time.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include <ctype.h> |
| 8 | #include <assert.h> |
| 9 | #include "sstring.h"cxx . . $ `<file>/sstring.h`/ |
| 10 | |
| 11 | #define BUILD_DLLcxx . . $ `utct.c:11:9`! |
| 12 | #include "utct.h"cxx . . $ `<file>/utct.h`/ |
| 13 | |
| 14 | #if defined( WIN32 ) |
| 15 | #define snprintf _snprintf |
| 16 | #endif |
| 17 | |
| 18 | static int char_pair_atoicxx . . $ char_pair_atoi(5c4306d8553b3696).( const char* pair ) |
| 19 | { |
| 20 | char str[3] = {0}; |
| 21 | str[0] = pair[0]; str[1] = pair[1]; str[2] = '\0'; |
| 22 | if ( !isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!( str[0] ) || !isdigitcxx . . $ `<external>/17904084882347303253/ctype.h:192:10`!( str[1] ) ) { return -1; } |
| 23 | return atoicxx . . $ atoi(5c4306d8553b3696).( str ); |
| 24 | } |
| 25 | |
| 26 | /* deal with 2 char year issue */ |
| 27 | static int century_offset_to_yearcxx . . $ century_offset_to_year(7864480464b09eea).( int century_offset ) |
| 28 | { |
| 29 | time_tcxx . . $ time_t# time_now = timecxx . . $ time(7f38134db6521d23).( 0 ); /* local time */ |
| 30 | struct tmcxx . . $ tm#* now = gmtimecxx . . $ gmtime(9477a1348abbbc5).( &time_now ); /* gmt broken down time */ |
| 31 | int current_year = now->tm_yearcxx . . $ tm#tm_year. + 1900; |
| 32 | int current_century_offset = current_year % 100; |
| 33 | int current_century = (current_year - current_century_offset) / 100; |
| 34 | int year = current_century * 100 + century_offset; |
| 35 | |
| 36 | /* assume year is in current century */ |
| 37 | /* unless that leads to very old or very new, then adjust */ |
| 38 | if ( year - current_year > 50 ) { year -= 100; } |
| 39 | else if ( year - current_year < -50 ) { year += 100; } |
| 40 | return year; |
| 41 | } |
| 42 | |
| 43 | #define MAX_DATEcxx . . $ `utct.c:43:9`! 50 /* Sun Mar 10 19:25:06 2002 (EST) */ |
| 44 | |
| 45 | /* more logical time_t to string conversion */ |
| 46 | |
| 47 | const char* strtimecxx . . $ strtime(f4b0cc662bb878e7).( time_tcxx . . $ time_t#* timep, int utc ) |
| 48 | { |
| 49 | static char str[MAX_DATEcxx . . $ `utct.c:43:9`!+1] = {0}; |
| 50 | struct tmcxx . . $ tm#* isdst = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 51 | char date[MAX_DATEcxx . . $ `utct.c:43:9`!+1] = {0}; |
| 52 | char* timestr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ; |
| 53 | char* zone = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ; |
| 54 | if ( utc ) { |
| 55 | timestr = asctimecxx . . $ asctime(888906f487632ba6).( gmtimecxx . . $ gmtime(9477a1348abbbc5).( timep ) ); |
| 56 | zone = "UTC"; |
| 57 | } else { |
| 58 | isdst = localtimecxx . . $ localtime(9477a1348abbbc5).( timep ); |
| 59 | timestr = asctimecxx . . $ asctime(888906f487632ba6).( isdst ); |
| 60 | zone = tznamecxx . . $ tzname.[isdst->tm_isdstcxx . . $ tm#tm_isdst.]; |
| 61 | } |
| 62 | sstrncpycxx . . $ `sstring.h:3:9`!( date, timestr, MAX_DATEcxx . . $ `utct.c:43:9`! ); |
| 63 | date[strlencxx . . $ strlen(751346f8406fd082).(date)-1]='\0'; /* remove trailing \n */ |
| 64 | snprintfcxx . . $ snprintf(ea502d73e74c754c).( str, MAX_DATEcxx . . $ `utct.c:43:9`!, "%s (%s)", date, zone ); |
| 65 | return str; |
| 66 | } |
| 67 | |
| 68 | /* alternate form of mktime, this tm struct is in UTC time */ |
| 69 | |
| 70 | time_tcxx . . $ time_t# mk_utctimecxx . . $ mk_utctime(237c3516b2cfac2c).( struct tmcxx . . $ tm#* tms ) { |
| 71 | char* tz = getenvcxx . . $ getenv(a8b8e32347182212).( "TZ" ); |
| 72 | time_tcxx . . $ time_t# res = 0 ; |
| 73 | char *set_tz = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ; |
| 74 | |
| 75 | putenvcxx . . $ putenv(1ef8e6ec083aabb9).( "TZ=UTC+0" ); |
| 76 | res = mktimecxx . . $ mktime(237c3516b2cfac2c).( tms ); |
| 77 | if ( tz ) { |
| 78 | set_tz = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).( strlencxx . . $ strlen(751346f8406fd082).( tz ) + 3 + 1 ); |
| 79 | sprintfcxx . . $ sprintf(26c8642d26409d6d).( set_tz, "TZ=%s", tz ); |
| 80 | putenvcxx . . $ putenv(1ef8e6ec083aabb9).( set_tz ); |
| 81 | freecxx . . $ free(5d22bdacc48458e8).( set_tz ); |
| 82 | } else { putenvcxx . . $ putenv(1ef8e6ec083aabb9).( "TZ" ); } |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | time_tcxx . . $ time_t# hashcash_from_utctimestrcxx . . $ hashcash_from_utctimestr(71083d4ee4d9cb41).( const char utct[MAX_UTCcxx . . $ `utct.h:39:9`!+1], int utc ) |
| 87 | { |
| 88 | time_tcxx . . $ time_t# failed = -1; |
| 89 | time_tcxx . . $ time_t# res = 0 ; |
| 90 | struct tmcxx . . $ tm# tms; |
| 91 | int tms_hour = 0; |
| 92 | struct tmcxx . . $ tm#* dst = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 93 | int utct_len = strlencxx . . $ strlen(751346f8406fd082).( utct ); |
| 94 | int century_offset = 0; |
| 95 | |
| 96 | if ( utct_len > MAX_UTCcxx . . $ `utct.h:39:9`! || utct_len < 2 || ( utct_len % 2 == 1 ) ) { |
| 97 | return failed; |
| 98 | } |
| 99 | |
| 100 | /* defaults */ |
| 101 | tms.tm_moncxx . . $ tm#tm_mon. = 0; tms.tm_mdaycxx . . $ tm#tm_mday. = 1; |
| 102 | tms.tm_hourcxx . . $ tm#tm_hour. = 0; tms.tm_mincxx . . $ tm#tm_min. = 0; tms.tm_seccxx . . $ tm#tm_sec. = 0; |
| 103 | tms.tm_isdstcxx . . $ tm#tm_isdst. = 0; /* daylight saving on */ |
| 104 | tms_hour = tms.tm_hourcxx . . $ tm#tm_hour.; |
| 105 | |
| 106 | /* year */ |
| 107 | century_offset = char_pair_atoicxx . . $ char_pair_atoi(5c4306d8553b3696).( utct ); |
| 108 | if ( century_offset < 0 ) { return failed; } |
| 109 | tms.tm_yearcxx . . $ tm#tm_year. = century_offset_to_yearcxx . . $ century_offset_to_year(7864480464b09eea).( century_offset ) - 1900; |
| 110 | /* month -- optional */ |
| 111 | if ( utct_len <= 2 ) { goto convert; } |
| 112 | tms.tm_moncxx . . $ tm#tm_mon. = char_pair_atoicxx . . $ char_pair_atoi(5c4306d8553b3696).( utct+2 ) - 1; |
| 113 | if ( tms.tm_moncxx . . $ tm#tm_mon. < 0 ) { return failed; } |
| 114 | /* day */ |
| 115 | if ( utct_len <= 4 ) { goto convert; } |
| 116 | tms.tm_mdaycxx . . $ tm#tm_mday. = char_pair_atoicxx . . $ char_pair_atoi(5c4306d8553b3696).( utct+4 ); |
| 117 | if ( tms.tm_mdaycxx . . $ tm#tm_mday. < 0 ) { return failed; } |
| 118 | /* hour -- optional */ |
| 119 | if ( utct_len <= 6 ) { goto convert; } |
| 120 | tms.tm_hourcxx . . $ tm#tm_hour. = char_pair_atoicxx . . $ char_pair_atoi(5c4306d8553b3696).( utct+6 ); |
| 121 | if ( tms.tm_hourcxx . . $ tm#tm_hour. < 0 ) { return failed; } |
| 122 | /* minute -- optional */ |
| 123 | if ( utct_len <= 8 ) { goto convert; } |
| 124 | tms.tm_mincxx . . $ tm#tm_min. = char_pair_atoicxx . . $ char_pair_atoi(5c4306d8553b3696).( utct+8 ); |
| 125 | if ( tms.tm_mincxx . . $ tm#tm_min. < 0 ) { return failed; } |
| 126 | if ( utct_len <= 10 ) { goto convert; } |
| 127 | /* second -- optional */ |
| 128 | tms.tm_seccxx . . $ tm#tm_sec. = char_pair_atoicxx . . $ char_pair_atoi(5c4306d8553b3696).( utct+10 ); |
| 129 | if ( tms.tm_seccxx . . $ tm#tm_sec. < 0 ) { return failed; } |
| 130 | |
| 131 | convert: |
| 132 | if ( utc ) { |
| 133 | return mk_utctimecxx . . $ mk_utctime(237c3516b2cfac2c).( &tms ); |
| 134 | } else { |
| 135 | /* note when switching from daylight to standard the last daylight |
| 136 | hour(s) are ambiguous with the first hour(s) of standard time. The |
| 137 | system calls give you the first hour(s) of standard time which is |
| 138 | as good a choice as any. */ |
| 139 | |
| 140 | /* note also the undefined hour(s) between the last hour of |
| 141 | standard time and the first hour of daylight time (when |
| 142 | expressed in localtime) are illegal values and have undefined |
| 143 | conversions, this code will do whatever the system calls do */ |
| 144 | |
| 145 | tms_hour = tms.tm_hourcxx . . $ tm#tm_hour.; |
| 146 | res = mktimecxx . . $ mktime(237c3516b2cfac2c).( &tms ); /* get time without DST adjust */ |
| 147 | dst = localtimecxx . . $ localtime(9477a1348abbbc5).( &res ); /* convert back to get DST adjusted */ |
| 148 | dst->tm_hourcxx . . $ tm#tm_hour. = tms_hour; /* put back in hour to convert */ |
| 149 | res = mktimecxx . . $ mktime(237c3516b2cfac2c).( dst ); /* redo conversion with DST adjustment */ |
| 150 | return res; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | int hashcash_to_utctimestrcxx . . $ hashcash_to_utctimestr(9276ee04ba38ce81).( char utct[MAX_UTCcxx . . $ `utct.h:39:9`!+1], int len, time_tcxx . . $ time_t# t ) |
| 155 | { |
| 156 | struct tmcxx . . $ tm#* tms = gmtimecxx . . $ gmtime(9477a1348abbbc5).( &t ); |
| 157 | |
| 158 | if ( tms == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || len > MAX_UTCcxx . . $ `utct.h:39:9`! || len < 2 ) { return 0; } |
| 159 | sprintfcxx . . $ sprintf(26c8642d26409d6d).( utct, "%02d", tms->tm_yearcxx . . $ tm#tm_year. % 100 ); |
| 160 | if ( len == 2 ) { goto leave; } |
| 161 | sprintfcxx . . $ sprintf(26c8642d26409d6d).( utct+2, "%02d", tms->tm_moncxx . . $ tm#tm_mon.+1 ); |
| 162 | if ( len == 4 ) { goto leave; } |
| 163 | sprintfcxx . . $ sprintf(26c8642d26409d6d).( utct+4, "%02d", tms->tm_mdaycxx . . $ tm#tm_mday. ); |
| 164 | if ( len == 6 ) { goto leave; } |
| 165 | sprintfcxx . . $ sprintf(26c8642d26409d6d).( utct+6, "%02d", tms->tm_hourcxx . . $ tm#tm_hour. ); |
| 166 | if ( len == 8 ) { goto leave; } |
| 167 | sprintfcxx . . $ sprintf(26c8642d26409d6d).( utct+8, "%02d", tms->tm_mincxx . . $ tm#tm_min. ); |
| 168 | if ( len == 10 ) { goto leave; } |
| 169 | sprintfcxx . . $ sprintf(26c8642d26409d6d).( utct+10, "%02d", tms->tm_seccxx . . $ tm#tm_sec. ); |
| 170 | |
| 171 | leave: |
| 172 | return 1; |
| 173 | } |