| 1 | /* -*- Mode: C; c-file-style: "stroustrup" -*- */ |
| 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" |
| 10 | |
| 11 | #define BUILD_DLL |
| 12 | #include "utct.h" |
| 13 | |
| 14 | #if defined( WIN32 ) |
| 15 | #define snprintf _snprintf |
| 16 | #endif |
| 17 | |
| 18 | static int char_pair_atoi( const char* pair ) |
| 19 | { |
| 20 | char str[3] = {0}; |
| 21 | str[0] = pair[0]; str[1] = pair[1]; str[2] = '\0'; |
| 22 | if ( !isdigit( str[0] ) || !isdigit( str[1] ) ) { return -1; } |
| 23 | return atoi( str ); |
| 24 | } |
| 25 | |
| 26 | /* deal with 2 char year issue */ |
| 27 | static int century_offset_to_year( int century_offset ) |
| 28 | { |
| 29 | time_t time_now = time( 0 ); /* local time */ |
| 30 | struct tm* now = gmtime( &time_now ); /* gmt broken down time */ |
| 31 | int current_year = now->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_DATE 50 /* Sun Mar 10 19:25:06 2002 (EST) */ |
| 44 | |
| 45 | /* more logical time_t to string conversion */ |
| 46 | |
| 47 | const char* strtime( time_t* timep, int utc ) |
| 48 | { |
| 49 | static char str[MAX_DATE+1] = {0}; |
| 50 | struct tm* isdst = NULL; |
| 51 | char date[MAX_DATE+1] = {0}; |
| 52 | char* timestr = NULL ; |
| 53 | char* zone = NULL ; |
| 54 | if ( utc ) { |
| 55 | timestr = asctime( gmtime( timep ) ); |
| 56 | zone = "UTC"; |
| 57 | } else { |
| 58 | isdst = localtime( timep ); |
| 59 | timestr = asctime( isdst ); |
| 60 | zone = tzname[isdst->tm_isdst]; |
| 61 | } |
| 62 | sstrncpy( date, timestr, MAX_DATE ); |
| 63 | date[strlen(date)-1]='\0'; /* remove trailing \n */ |
| 64 | snprintf( str, MAX_DATE, "%s (%s)", date, zone ); |
| 65 | return str; |
| 66 | } |
| 67 | |
| 68 | /* alternate form of mktime, this tm struct is in UTC time */ |
| 69 | |
| 70 | time_t mk_utctime( struct tm* tms ) { |
| 71 | char* tz = getenv( "TZ" ); |
| 72 | time_t res = 0 ; |
| 73 | char *set_tz = NULL ; |
| 74 | |
| 75 | putenv( "TZ=UTC+0" ); |
| 76 | res = mktime( tms ); |
| 77 | if ( tz ) { |
| 78 | set_tz = malloc( strlen( tz ) + 3 + 1 ); |
| 79 | sprintf( set_tz, "TZ=%s", tz ); |
| 80 | putenv( set_tz ); |
| 81 | free( set_tz ); |
| 82 | } else { putenv( "TZ" ); } |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | time_t hashcash_from_utctimestr( const char utct[MAX_UTC+1], int utc ) |
| 87 | { |
| 88 | time_t failed = -1; |
| 89 | time_t res = 0 ; |
| 90 | struct tm tms; |
| 91 | int tms_hour = 0; |
| 92 | struct tm* dst = NULL; |
| 93 | int utct_len = strlen( utct ); |
| 94 | int century_offset = 0; |
| 95 | |
| 96 | if ( utct_len > MAX_UTC || utct_len < 2 || ( utct_len % 2 == 1 ) ) { |
| 97 | return failed; |
| 98 | } |
| 99 | |
| 100 | /* defaults */ |
| 101 | tms.tm_mon = 0; tms.tm_mday = 1; |
| 102 | tms.tm_hour = 0; tms.tm_min = 0; tms.tm_sec = 0; |
| 103 | tms.tm_isdst = 0; /* daylight saving on */ |
| 104 | tms_hour = tms.tm_hour; |
| 105 | |
| 106 | /* year */ |
| 107 | century_offset = char_pair_atoi( utct ); |
| 108 | if ( century_offset < 0 ) { return failed; } |
| 109 | tms.tm_year = century_offset_to_year( century_offset ) - 1900; |
| 110 | /* month -- optional */ |
| 111 | if ( utct_len <= 2 ) { goto convert; } |
| 112 | tms.tm_mon = char_pair_atoi( utct+2 ) - 1; |
| 113 | if ( tms.tm_mon < 0 ) { return failed; } |
| 114 | /* day */ |
| 115 | if ( utct_len <= 4 ) { goto convert; } |
| 116 | tms.tm_mday = char_pair_atoi( utct+4 ); |
| 117 | if ( tms.tm_mday < 0 ) { return failed; } |
| 118 | /* hour -- optional */ |
| 119 | if ( utct_len <= 6 ) { goto convert; } |
| 120 | tms.tm_hour = char_pair_atoi( utct+6 ); |
| 121 | if ( tms.tm_hour < 0 ) { return failed; } |
| 122 | /* minute -- optional */ |
| 123 | if ( utct_len <= 8 ) { goto convert; } |
| 124 | tms.tm_min = char_pair_atoi( utct+8 ); |
| 125 | if ( tms.tm_min < 0 ) { return failed; } |
| 126 | if ( utct_len <= 10 ) { goto convert; } |
| 127 | /* second -- optional */ |
| 128 | tms.tm_sec = char_pair_atoi( utct+10 ); |
| 129 | if ( tms.tm_sec < 0 ) { return failed; } |
| 130 | |
| 131 | convert: |
| 132 | if ( utc ) { |
| 133 | return mk_utctime( &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_hour; |
| 146 | res = mktime( &tms ); /* get time without DST adjust */ |
| 147 | dst = localtime( &res ); /* convert back to get DST adjusted */ |
| 148 | dst->tm_hour = tms_hour; /* put back in hour to convert */ |
| 149 | res = mktime( dst ); /* redo conversion with DST adjustment */ |
| 150 | return res; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | int hashcash_to_utctimestr( char utct[MAX_UTC+1], int len, time_t t ) |
| 155 | { |
| 156 | struct tm* tms = gmtime( &t ); |
| 157 | |
| 158 | if ( tms == NULL || len > MAX_UTC || len < 2 ) { return 0; } |
| 159 | sprintf( utct, "%02d", tms->tm_year % 100 ); |
| 160 | if ( len == 2 ) { goto leave; } |
| 161 | sprintf( utct+2, "%02d", tms->tm_mon+1 ); |
| 162 | if ( len == 4 ) { goto leave; } |
| 163 | sprintf( utct+4, "%02d", tms->tm_mday ); |
| 164 | if ( len == 6 ) { goto leave; } |
| 165 | sprintf( utct+6, "%02d", tms->tm_hour ); |
| 166 | if ( len == 8 ) { goto leave; } |
| 167 | sprintf( utct+8, "%02d", tms->tm_min ); |
| 168 | if ( len == 10 ) { goto leave; } |
| 169 | sprintf( utct+10, "%02d", tms->tm_sec ); |
| 170 | |
| 171 | leave: |
| 172 | return 1; |
| 173 | } |