| [821] | 1 | /* $Id: 625main.c 1334 2004-04-05 19:45:58Z bird $ | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * TZ (tzset++) testcase. | 
|---|
|  | 4 | * | 
|---|
|  | 5 | * InnoTek Systemberatung GmbH confidential | 
|---|
|  | 6 | * | 
|---|
|  | 7 | * Copyright (c) 2003 InnoTek Systemberatung GmbH | 
|---|
|  | 8 | * Author: knut st. osmundsen <bird-srcspam@anduin.net> | 
|---|
|  | 9 | * | 
|---|
|  | 10 | * All Rights Reserved | 
|---|
|  | 11 | * | 
|---|
|  | 12 | */ | 
|---|
|  | 13 | #include <time.h> | 
|---|
|  | 14 | #include <stdio.h> | 
|---|
|  | 15 | #include <stdlib.h> | 
|---|
| [901] | 16 | #include <string.h> | 
|---|
| [821] | 17 |  | 
|---|
| [901] | 18 | static struct times | 
|---|
|  | 19 | { | 
|---|
|  | 20 | time_t   time; | 
|---|
|  | 21 | }   aTimes[7] = | 
|---|
|  | 22 | { | 
|---|
|  | 23 | 0,                                       /* 1970-01-01 00:00:00 */ | 
|---|
|  | 24 | 13046400,                                /* 1970-06-01 00:00:00 */ | 
|---|
|  | 25 | -(10*365+3-31-29)*24*3600 - 12*3600,     /* 1960-03-01 12:00:00 */ | 
|---|
|  | 26 | -0x7fffffff + (365*24*60*60),            /* 1901-12-14 20:45:52 */ | 
|---|
|  | 27 | 0x7fffffff - (24*60*60),                 /* 2038-01-18 03:14:07 */ | 
|---|
|  | 28 | 31*365*24*3600 + 132,                    /* 2000-12-23 16:01:12 */ | 
|---|
|  | 29 | (33*365 + 172)*24*3600 + 132,            /* 2000-12-23 16:01:12 */ | 
|---|
|  | 30 | }; | 
|---|
|  | 31 |  | 
|---|
|  | 32 | static struct testcase | 
|---|
|  | 33 | { | 
|---|
|  | 34 | const char *pszTZ; | 
|---|
|  | 35 | const char *pszTZRes; | 
|---|
|  | 36 | const char *apszRes[9]; | 
|---|
|  | 37 | }   aTests[] = | 
|---|
|  | 38 | { | 
|---|
|  | 39 | { | 
|---|
|  | 40 | "TZ=PST8", | 
|---|
|  | 41 | "timezone=28800  daylight=0  tzname='PST','", | 
|---|
|  | 42 | { | 
|---|
|  | 43 | "1969-12-31 16:00:00  3  364  not-dst(0)", | 
|---|
|  | 44 | "1970-05-31 16:00:00  0  150  not-dst(0)", | 
|---|
|  | 45 | "1960-02-29 04:00:00  1   59  not-dst(0)", | 
|---|
|  | 46 | "1902-12-13 12:45:53  6  346  not-dst(0)", | 
|---|
|  | 47 | "2038-01-17 19:14:07  0   16  not-dst(0)", | 
|---|
|  | 48 | "2000-12-23 16:02:12  6  357  not-dst(0)", | 
|---|
|  | 49 | "2003-06-13 16:02:12  5  163  not-dst(0)", | 
|---|
|  | 50 | } | 
|---|
|  | 51 | }, | 
|---|
|  | 52 | { | 
|---|
|  | 53 | "TZ=UCT0", | 
|---|
|  | 54 | "timezone=0      daylight=0  tzname='UCT','", | 
|---|
|  | 55 | { | 
|---|
|  | 56 | "1970-01-01 00:00:00  4    0  not-dst(0)", | 
|---|
|  | 57 | "1970-06-01 00:00:00  1  151  not-dst(0)", | 
|---|
|  | 58 | "1960-02-29 12:00:00  1   59  not-dst(0)", | 
|---|
|  | 59 | "1902-12-13 20:45:53  6  346  not-dst(0)", | 
|---|
|  | 60 | "2038-01-18 03:14:07  1   17  not-dst(0)", | 
|---|
|  | 61 | "2000-12-24 00:02:12  0  358  not-dst(0)", | 
|---|
|  | 62 | "2003-06-14 00:02:12  6  164  not-dst(0)", | 
|---|
|  | 63 | } | 
|---|
|  | 64 | }, | 
|---|
|  | 65 | { | 
|---|
|  | 66 | "TZ=PST8PDT", | 
|---|
|  | 67 | "timezone=28800  daylight=1  tzname='PST','PDT'", | 
|---|
|  | 68 | { | 
|---|
|  | 69 | "1969-12-31 16:00:00  3  364  not-dst(0)", | 
|---|
|  | 70 | "1970-05-31 17:00:00  0  150  dst(1)", | 
|---|
|  | 71 | "1960-02-29 04:00:00  1   59  not-dst(0)", | 
|---|
|  | 72 | "1902-12-13 12:45:53  6  346  not-dst(0)", | 
|---|
|  | 73 | "2038-01-17 19:14:07  0   16  not-dst(0)", | 
|---|
|  | 74 | "2000-12-23 16:02:12  6  357  not-dst(0)", | 
|---|
|  | 75 | "2003-06-13 17:02:12  5  163  dst(1)", | 
|---|
|  | 76 | } | 
|---|
|  | 77 | }, | 
|---|
|  | 78 | { | 
|---|
|  | 79 | "TZ=CET-1CDT", | 
|---|
|  | 80 | "timezone=-3600  daylight=1  tzname='CET','CDT'", | 
|---|
|  | 81 | { | 
|---|
|  | 82 | "1970-01-01 01:00:00  4    0  not-dst(0)", | 
|---|
|  | 83 | "1970-06-01 02:00:00  1  151  dst(1)", | 
|---|
|  | 84 | "1960-02-29 13:00:00  1   59  not-dst(0)", | 
|---|
|  | 85 | "1902-12-13 21:45:53  6  346  not-dst(0)", | 
|---|
|  | 86 | "2038-01-18 04:14:07  1   17  not-dst(0)", | 
|---|
|  | 87 | "2000-12-24 01:02:12  0  358  not-dst(0)", | 
|---|
|  | 88 | "2003-06-14 02:02:12  6  164  dst(1)", | 
|---|
|  | 89 | } | 
|---|
|  | 90 | }, | 
|---|
|  | 91 | { | 
|---|
|  | 92 | #ifndef UNIX_TZ | 
|---|
|  | 93 | "TZ=CET-1CEDT,3,-1,0,7200,10,-1,0,10800,3600", | 
|---|
|  | 94 | #else | 
|---|
| [904] | 95 | /*"TZ=CET-1CEDT,M3.-1.0/2,M10.-1.0/3", */ | 
|---|
|  | 96 | "TZ=CET-1CEDT,M3.4.0/2,M10.4.0/3", | 
|---|
| [901] | 97 | #endif | 
|---|
|  | 98 | "timezone=-3600  daylight=1  tzname='CET','CEDT'", | 
|---|
|  | 99 | { | 
|---|
|  | 100 | "1970-01-01 01:00:00  4    0  not-dst(0)", | 
|---|
|  | 101 | "1970-06-01 02:00:00  1  151  dst(1)", | 
|---|
|  | 102 | "1960-02-29 13:00:00  1   59  not-dst(0)", | 
|---|
|  | 103 | "1902-12-13 21:45:53  6  346  not-dst(0)", | 
|---|
|  | 104 | "2038-01-18 04:14:07  1   17  not-dst(0)", | 
|---|
|  | 105 | "2000-12-24 01:02:12  0  358  not-dst(0)", | 
|---|
|  | 106 | "2003-06-14 02:02:12  6  164  dst(1)", | 
|---|
|  | 107 | } | 
|---|
|  | 108 | }, | 
|---|
|  | 109 | { | 
|---|
|  | 110 | #ifndef UNIX_TZ | 
|---|
|  | 111 | "TZ=AEST-10AEDT,10,-1,0,7200,3,-1,0,7200,3600", | 
|---|
|  | 112 | #else | 
|---|
| [904] | 113 | /*"TZ=AEST-10AEDT,M10.-1.0/2,M3.-1.0/2", */ | 
|---|
|  | 114 | "TZ=AEST-10AEDT,M10.4.0/2,M3.4.0/2", | 
|---|
| [901] | 115 | #endif | 
|---|
|  | 116 | "timezone=-36000  daylight=1  tzname='AEST','AEDT'", | 
|---|
|  | 117 | { | 
|---|
|  | 118 | "1970-01-01 11:00:00  4    0  dst(1)", | 
|---|
|  | 119 | "1970-06-01 10:00:00  1  151  not-dst(0)", | 
|---|
|  | 120 | "1960-02-29 23:00:00  1   59  dst(1)", | 
|---|
|  | 121 | "1902-12-14 07:45:53  0  347  dst(1)", | 
|---|
|  | 122 | "2038-01-18 14:14:07  1   17  dst(1)", | 
|---|
|  | 123 | "2000-12-24 11:02:12  0  358  dst(1)", | 
|---|
|  | 124 | "2003-06-14 10:02:12  6  164  not-dst(0)", | 
|---|
|  | 125 | } | 
|---|
| [1334] | 126 | }, | 
|---|
|  | 127 | { | 
|---|
|  | 128 | #ifndef UNIX_TZ | 
|---|
|  | 129 | "TZ=EST5EDT,4,1,0,7200,10,-1,0,7200,3600", | 
|---|
|  | 130 | #else | 
|---|
|  | 131 | "TZ=EST5EDT,M4.1.0/2,M10.4.0/3", | 
|---|
|  | 132 | #endif | 
|---|
|  | 133 | "timezone=18000  daylight=1  tzname='EST','EDT'", | 
|---|
|  | 134 | { | 
|---|
|  | 135 | "1969-12-31 19:00:00  3  364  not-dst(0)", | 
|---|
|  | 136 | "1970-05-31 20:00:00  0  150  dst(1)", | 
|---|
|  | 137 | "1960-02-29 07:00:00  1   59  not-dst(0)", | 
|---|
|  | 138 | "1902-12-13 15:45:53  6  346  not-dst(0)", | 
|---|
|  | 139 | "2038-01-17 22:14:07  0   16  not-dst(0)", | 
|---|
|  | 140 | "2000-12-23 19:02:12  6  357  not-dst(0)", | 
|---|
|  | 141 | "2003-06-13 20:02:12  5  163  dst(1)", | 
|---|
|  | 142 | } | 
|---|
| [901] | 143 | } | 
|---|
| [1334] | 144 |  | 
|---|
| [901] | 145 | }; | 
|---|
|  | 146 |  | 
|---|
|  | 147 | int doTests(void) | 
|---|
|  | 148 | { | 
|---|
|  | 149 | int i; | 
|---|
|  | 150 | int rc; | 
|---|
|  | 151 | char sz[512]; | 
|---|
|  | 152 |  | 
|---|
|  | 153 | for (rc = i = 0; i < sizeof(aTests) / sizeof(aTests[0]); i++) | 
|---|
|  | 154 | { | 
|---|
|  | 155 | int j; | 
|---|
|  | 156 | putenv(aTests[i].pszTZ); | 
|---|
|  | 157 | tzset(); | 
|---|
|  | 158 | sprintf(sz, "timezone=%-5d  daylight=%d  tzname='%s','%s'", | 
|---|
| [904] | 159 | #ifndef FREEBSD | 
|---|
| [901] | 160 | #ifdef __IBMC__ | 
|---|
|  | 161 | _timezone, | 
|---|
|  | 162 | #else | 
|---|
|  | 163 | timezone, | 
|---|
|  | 164 | #endif | 
|---|
|  | 165 | daylight, | 
|---|
| [904] | 166 | #else | 
|---|
|  | 167 | -1, -1, | 
|---|
|  | 168 | #endif | 
|---|
| [901] | 169 | tzname[0], | 
|---|
|  | 170 | tzname[1]); | 
|---|
| [904] | 171 | #ifndef FREEBSD | 
|---|
| [901] | 172 | if (!strncmp(sz, aTests[i].pszTZRes, strlen(aTests[i].pszTZRes))) | 
|---|
| [904] | 173 | #endif | 
|---|
| [901] | 174 | printf("%s\n  %s - ok\n", aTests[i].pszTZ, sz); | 
|---|
| [904] | 175 | #ifndef FREEBSD | 
|---|
| [901] | 176 | else | 
|---|
|  | 177 | { | 
|---|
|  | 178 | printf("%s\n  %s - mismatch!!! %s\n", aTests[i].pszTZ, sz, aTests[i].pszTZRes); | 
|---|
|  | 179 | rc++; | 
|---|
|  | 180 | } | 
|---|
| [904] | 181 | #endif | 
|---|
| [901] | 182 |  | 
|---|
|  | 183 | for (j = 0; j < sizeof(aTimes) / sizeof(aTimes[0]); j++) | 
|---|
|  | 184 | { | 
|---|
|  | 185 | struct tm  *pTm = localtime(&aTimes[j].time); | 
|---|
|  | 186 | sprintf(sz, "%04d-%02d-%02d %02d:%02d:%02d  %d  %3d  %s(%d)", | 
|---|
|  | 187 | pTm->tm_year + 1900, pTm->tm_mon + 1, pTm->tm_mday, | 
|---|
|  | 188 | pTm->tm_hour, pTm->tm_min, pTm->tm_sec, pTm->tm_wday, pTm->tm_yday, | 
|---|
|  | 189 | pTm->tm_isdst ? "dst" : "not-dst", pTm->tm_isdst); | 
|---|
|  | 190 | if (!strcmp(sz, aTests[i].apszRes[j])) | 
|---|
|  | 191 | printf("%12d: %s - ok\n", aTimes[j].time, sz); | 
|---|
|  | 192 | else | 
|---|
|  | 193 | { | 
|---|
|  | 194 | printf("%12d: %s - mismatch!! %s\n", aTimes[j].time, sz, aTests[i].apszRes[j]); | 
|---|
|  | 195 | rc++; | 
|---|
|  | 196 | } | 
|---|
|  | 197 | } | 
|---|
|  | 198 | } | 
|---|
|  | 199 | return rc; | 
|---|
|  | 200 | } | 
|---|
|  | 201 |  | 
|---|
| [821] | 202 | void printtz(void) | 
|---|
|  | 203 | { | 
|---|
|  | 204 | const char *pszTZ = getenv("TZ"); | 
|---|
| [901] | 205 | int         i; | 
|---|
|  | 206 |  | 
|---|
| [821] | 207 | tzset(); | 
|---|
| [901] | 208 | printf("TZ=%s\n    timezone=%-5d\t daylight=%d\ttzname='%s','%s'\n", | 
|---|
| [821] | 209 | pszTZ, | 
|---|
| [904] | 210 | #ifndef FREEBSD | 
|---|
| [821] | 211 | #ifdef __IBMC__ | 
|---|
|  | 212 | _timezone, | 
|---|
|  | 213 | #else | 
|---|
|  | 214 | timezone, | 
|---|
|  | 215 | #endif | 
|---|
|  | 216 | daylight, | 
|---|
| [904] | 217 | #else | 
|---|
|  | 218 | -1, -1, | 
|---|
|  | 219 | #endif | 
|---|
| [821] | 220 | tzname[0], | 
|---|
|  | 221 | tzname[1]); | 
|---|
| [901] | 222 | for (i = 0; i < sizeof(aTimes) / sizeof(aTimes[0]); i++) | 
|---|
|  | 223 | { | 
|---|
|  | 224 | struct tm  *pTm = localtime(&aTimes[i].time); | 
|---|
|  | 225 | printf("%12d: %04d-%02d-%02d %02d:%02d:%02d  %d  %3d  %s(%d)\n", | 
|---|
|  | 226 | (int)aTimes[i].time, pTm->tm_year + 1900, pTm->tm_mon + 1, pTm->tm_mday, | 
|---|
|  | 227 | pTm->tm_hour, pTm->tm_min, pTm->tm_sec, pTm->tm_wday, pTm->tm_yday, | 
|---|
|  | 228 | pTm->tm_isdst ? "dst" : "not-dst", pTm->tm_isdst); | 
|---|
|  | 229 | } | 
|---|
| [821] | 230 | } | 
|---|
|  | 231 |  | 
|---|
|  | 232 | int main() | 
|---|
|  | 233 | { | 
|---|
| [901] | 234 | int rcRet; | 
|---|
|  | 235 |  | 
|---|
| [821] | 236 | printtz(); | 
|---|
|  | 237 | putenv("TZ="); | 
|---|
|  | 238 | printtz(); | 
|---|
| [901] | 239 |  | 
|---|
|  | 240 | /* execute testcases. */ | 
|---|
|  | 241 | rcRet = doTests(); | 
|---|
|  | 242 |  | 
|---|
|  | 243 | /* results */ | 
|---|
|  | 244 | if (!rcRet) | 
|---|
|  | 245 | printf("Successfully executed return struct testcase (#625).\n"); | 
|---|
|  | 246 | else | 
|---|
|  | 247 | printf("625main: %d failures.\n", rcRet); | 
|---|
|  | 248 | return rcRet; | 
|---|
| [821] | 249 | } | 
|---|
|  | 250 |  | 
|---|