1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
// Copyright 2017 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void test(int result, const char* comment, const char* parsed = "") {
printf("%s: %d\n", comment, result);
if (!result) {
printf("\nERROR: %s (\"%s\")\n", comment, parsed);
#ifdef REPORT_RESULT
abort();
#endif
}
}
int cmp(const char* s1, const char* s2) {
for (; *s1 == *s2; s1++, s2++) {
if (*s1 == '\0') break;
}
return (*s1 - *s2);
}
int main() {
struct tm tm;
char s[1000];
size_t size;
tm.tm_sec = 4;
tm.tm_min = 23;
tm.tm_hour = 20;
tm.tm_mday = 21;
tm.tm_mon = 1;
tm.tm_year = 74;
tm.tm_wday = 4;
tm.tm_yday = 51;
tm.tm_isdst = 0;
size = strftime(s, 1000, "", &tm);
test((size == 0) && (*s == '\0'), "strftime test #1", s);
size = strftime(s, 1000, "%a", &tm);
test((size == 3) && !cmp(s, "Thu"), "strftime test #2", s);
size = strftime(s, 1000, "%A", &tm);
test((size == 8) && !cmp(s, "Thursday"), "strftime test #3", s);
size = strftime(s, 1000, "%b", &tm);
test((size == 3) && !cmp(s, "Feb"), "strftime test #4", s);
size = strftime(s, 1000, "%B", &tm);
test((size == 8) && !cmp(s, "February"), "strftime test #5", s);
size = strftime(s, 1000, "%d", &tm);
test((size == 2) && !cmp(s, "21"), "strftime test #6", s);
size = strftime(s, 1000, "%Od", &tm);
test((size == 2) && !cmp(s, "21"), "strftime test #6a", s);
size = strftime(s, 1000, "%H", &tm);
test((size == 2) && !cmp(s, "20"), "strftime test #7", s);
size = strftime(s, 1000, "%OH", &tm);
test((size == 2) && !cmp(s, "20"), "strftime test #7a", s);
size = strftime(s, 1000, "%I", &tm);
test((size == 2) && !cmp(s, "08"), "strftime test #8", s);
size = strftime(s, 1000, "%OI", &tm);
test((size == 2) && !cmp(s, "08"), "strftime test #8a", s);
size = strftime(s, 1000, "%j", &tm);
test((size == 3) && !cmp(s, "052"), "strftime test #9", s);
size = strftime(s, 1000, "%m", &tm);
test((size == 2) && !cmp(s, "02"), "strftime test #10", s);
size = strftime(s, 1000, "%Om", &tm);
test((size == 2) && !cmp(s, "02"), "strftime test #10a", s);
size = strftime(s, 1000, "%M", &tm);
test((size == 2) && !cmp(s, "23"), "strftime test #11", s);
size = strftime(s, 1000, "%OM", &tm);
test((size == 2) && !cmp(s, "23"), "strftime test #11a", s);
size = strftime(s, 1000, "%p", &tm);
test((size == 2) && !cmp(s, "PM"), "strftime test #12", s);
size = strftime(s, 1000, "%S", &tm);
test((size == 2) && !cmp(s, "04"), "strftime test #13", s);
size = strftime(s, 1000, "%OS", &tm);
test((size == 2) && !cmp(s, "04"), "strftime test #13a", s);
size = strftime(s, 1000, "%U", &tm);
test((size == 2) && !cmp(s, "07"), "strftime test #14", s);
size = strftime(s, 1000, "%OU", &tm);
test((size == 2) && !cmp(s, "07"), "strftime test #14a", s);
size = strftime(s, 1000, "%w", &tm);
test((size == 1) && !cmp(s, "4"), "strftime test #15", s);
size = strftime(s, 1000, "%Ow", &tm);
test((size == 1) && !cmp(s, "4"), "strftime test #15a", s);
size = strftime(s, 1000, "%W", &tm);
test((size == 2) && !cmp(s, "07"), "strftime test #16", s);
size = strftime(s, 1000, "%OW", &tm);
test((size == 2) && !cmp(s, "07"), "strftime test #16a", s);
size = strftime(s, 1000, "%y", &tm);
test((size == 2) && !cmp(s, "74"), "strftime test #17", s);
size = strftime(s, 1000, "%Oy", &tm);
test((size == 2) && !cmp(s, "74"), "strftime test #17a", s);
size = strftime(s, 1000, "%Y", &tm);
test((size == 4) && !cmp(s, "1974"), "strftime test #18", s);
size = strftime(s, 1000, "%EY", &tm);
test((size == 4) && !cmp(s, "1974"), "strftime test #18a", s);
size = strftime(s, 1000, "%%", &tm);
test((size == 1) && !cmp(s, "%"), "strftime test #19", s);
size = strftime(s, 5, "%Y", &tm);
test((size == 4) && !cmp(s, "1974"), "strftime test #20", s);
size = strftime(s, 4, "%Y", &tm);
test((size == 0), "strftime test #21", s);
tm.tm_mon = 0;
tm.tm_mday = 1;
size = strftime(s, 10, "%U", &tm);
test((size == 2) && !cmp(s, "00"), "strftime test #22", s);
size = strftime(s, 10, "%W", &tm);
test((size == 2) && !cmp(s, "00"), "strftime test #23", s);
// 1/1/1973 was a Sunday and is in CW 1
tm.tm_year = 73;
size = strftime(s, 10, "%W", &tm);
test((size == 2) && !cmp(s, "01"), "strftime test #24", s);
// 1/1/1978 was a Monday and is in CW 1
tm.tm_year = 78;
size = strftime(s, 10, "%U", &tm);
test((size == 2) && !cmp(s, "01"), "strftime test #25", s);
// 2/1/1999
tm.tm_year = 99;
tm.tm_yday = 1;
size = strftime(s, 10, "%G (%V)", &tm);
test((size == 9) && !cmp(s, "1998 (53)"), "strftime test #26", s);
size = strftime(s, 10, "%g", &tm);
test((size == 2) && !cmp(s, "98"), "strftime test #27", s);
// 30/12/1997
tm.tm_year = 97;
tm.tm_yday = 363;
size = strftime(s, 10, "%G (%V)", &tm);
test((size == 9) && !cmp(s, "1998 (01)"), "strftime test #28", s);
size = strftime(s, 10, "%g", &tm);
test((size == 2) && !cmp(s, "98"), "strftime test #29", s);
tm.tm_wday = 0;
size = strftime(s, 2, "%w", &tm);
test((size == 1) && !cmp(s, "0"), "strftime test #30", s);
size = strftime(s, 2, "%u", &tm);
test((size == 1) && !cmp(s, "7"), "strftime test #31", s);
tm.tm_wday = 1;
size = strftime(s, 2, "%w", &tm);
test((size == 1) && !cmp(s, "1"), "strftime test #32", s);
size = strftime(s, 2, "%u", &tm);
test((size == 1) && !cmp(s, "1"), "strftime test #33", s);
tm.tm_wday = 2;
size = strftime(s, 2, "%w", &tm);
test((size == 1) && !cmp(s, "2"), "strftime test #34", s);
size = strftime(s, 2, "%u", &tm);
test((size == 1) && !cmp(s, "2"), "strftime test #35", s);
tm.tm_wday = 3;
size = strftime(s, 2, "%w", &tm);
test((size == 1) && !cmp(s, "3"), "strftime test #36", s);
size = strftime(s, 2, "%u", &tm);
test((size == 1) && !cmp(s, "3"), "strftime test #37", s);
tm.tm_wday = 4;
size = strftime(s, 2, "%w", &tm);
test((size == 1) && !cmp(s, "4"), "strftime test #38", s);
size = strftime(s, 2, "%u", &tm);
test((size == 1) && !cmp(s, "4"), "strftime test #39", s);
tm.tm_wday = 5;
size = strftime(s, 2, "%w", &tm);
test((size == 1) && !cmp(s, "5"), "strftime test #40", s);
size = strftime(s, 2, "%u", &tm);
test((size == 1) && !cmp(s, "5"), "strftime test #41", s);
tm.tm_wday = 6;
size = strftime(s, 2, "%w", &tm);
test((size == 1) && !cmp(s, "6"), "strftime test #42", s);
size = strftime(s, 2, "%u", &tm);
test((size == 1) && !cmp(s, "6"), "strftime test #43", s);
// timezones
time_t xmas2002 = 1040786563ll;
time_t summer2002 = 1025528525ll;
localtime_r(&summer2002, &tm);
int ahead = timegm(&tm) >= summer2002;
size = strftime(s, 50, "%z", &tm);
test((size == 5) && strchr(s, ahead ? '+' : '-'), "strftime zone test #1", s);
size = strftime(s, 50, "%Z", &tm);
test(strcmp(s, tzname[tm.tm_isdst]) == 0, "strftime zone test #2", s);
localtime_r(&xmas2002, &tm);
ahead = timegm(&tm) >= xmas2002;
size = strftime(s, 50, "%z", &tm);
test((size == 5) && strchr(s, ahead ? '+' : '-'), "strftime zone test #3", s);
size = strftime(s, 50, "%Z", &tm);
test(strcmp(s, tzname[tm.tm_isdst]) == 0, "strftime zone test #4", s);
// AM/PM
tm.tm_sec = 0;
tm.tm_min = 0;
tm.tm_hour = 0;
size = strftime(s, 10, "%I %p", &tm);
test(!cmp(s, "12 AM"), "strftime test #32", s);
tm.tm_hour = 12;
size = strftime(s, 10, "%I %p", &tm);
test(!cmp(s, "12 PM"), "strftime test #33", s);
tm.tm_min = 1;
tm.tm_hour = 0;
size = strftime(s, 10, "%I %M %p", &tm);
test(!cmp(s, "12 01 AM"), "strftime test #34", s);
tm.tm_hour = 12;
size = strftime(s, 10, "%I %M %p", &tm);
test(!cmp(s, "12 01 PM"), "strftime test #35", s);
#ifdef REPORT_RESULT
REPORT_RESULT(0);
#endif
}
|