1 /* $NetBSD: t_strptime.c,v 1.15 2018/06/03 08:48:37 maya Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by David Laight. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __COPYRIGHT("@(#) Copyright (c) 2008\ 34 The NetBSD Foundation, inc. All rights reserved."); 35 __RCSID("$NetBSD: t_strptime.c,v 1.15 2018/06/03 08:48:37 maya Exp $"); 36 37 #include <time.h> 38 #include <stdlib.h> 39 #include <stdio.h> 40 41 #include <atf-c.h> 42 43 static void 44 h_pass(const char *buf, const char *fmt, int len, 45 int tm_sec, int tm_min, int tm_hour, int tm_mday, 46 int tm_mon, int tm_year, int tm_wday, int tm_yday) 47 { 48 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL }; 49 const char *ret, *exp; 50 51 exp = buf + len; 52 ret = strptime(buf, fmt, &tm); 53 54 ATF_CHECK_MSG(ret == exp, 55 "strptime(\"%s\", \"%s\", tm): incorrect return code: " 56 "expected: %p, got: %p", buf, fmt, exp, ret); 57 58 #define H_REQUIRE_FIELD(field) \ 59 ATF_CHECK_MSG(tm.field == field, \ 60 "strptime(\"%s\", \"%s\", tm): incorrect %s: " \ 61 "expected: %d, but got: %d", buf, fmt, \ 62 ___STRING(field), field, tm.field) 63 64 H_REQUIRE_FIELD(tm_sec); 65 H_REQUIRE_FIELD(tm_min); 66 H_REQUIRE_FIELD(tm_hour); 67 H_REQUIRE_FIELD(tm_mday); 68 H_REQUIRE_FIELD(tm_mon); 69 H_REQUIRE_FIELD(tm_year); 70 H_REQUIRE_FIELD(tm_wday); 71 H_REQUIRE_FIELD(tm_yday); 72 73 #undef H_REQUIRE_FIELD 74 } 75 76 static void 77 h_fail(const char *buf, const char *fmt) 78 { 79 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL }; 80 81 ATF_CHECK_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", " 82 "\"%s\", &tm) should fail, but it didn't", buf, fmt); 83 } 84 85 static struct { 86 const char *name; 87 long offs; 88 } zt[] = { 89 { "Z", 0 }, 90 { "UT", 0 }, 91 { "UTC", 0 }, 92 { "GMT", 0 }, 93 { "EST", -18000 }, 94 { "EDT", -14400 }, 95 { "CST", -21600 }, 96 { "CDT", -18000 }, 97 { "MST", -25200 }, 98 { "MDT", -21600 }, 99 { "PST", -28800 }, 100 { "PDT", -25200 }, 101 102 { "VST", -1 }, 103 { "VDT", -1 }, 104 105 { "+03", 10800 }, 106 { "-03", -10800 }, 107 { "+0403", 14580 }, 108 { "-0403", -14580 }, 109 { "+04:03", 14580 }, 110 { "-04:03", -14580 }, 111 { "+14:00", 50400 }, 112 { "-14:00", -50400 }, 113 { "+23:59", 86340 }, 114 { "-23:59", -86340 }, 115 116 { "1", -1 }, 117 { "03", -1 }, 118 { "0304", -1 }, 119 { "+1", -1 }, 120 { "-203", -1 }, 121 { "+12345", -1 }, 122 { "+12:345", -1 }, 123 { "+123:45", -1 }, 124 { "+2400", -1 }, 125 { "-2400", -1 }, 126 { "+1060", -1 }, 127 { "-1060", -1 }, 128 129 { "A", 3600 }, 130 { "B", 7200 }, 131 { "C", 10800 }, 132 { "D", 14400 }, 133 { "E", 18000 }, 134 { "F", 21600 }, 135 { "G", 25200 }, 136 { "H", 28800 }, 137 { "I", 32400 }, 138 { "L", 39600 }, 139 { "M", 43200 }, 140 { "N", -3600 }, 141 { "O", -7200 }, 142 { "P", -10800 }, 143 { "Q", -14400 }, 144 { "R", -18000 }, 145 { "T", -25200 }, 146 { "U", -28800 }, 147 { "V", -32400 }, 148 { "W", -36000 }, 149 { "X", -39600 }, 150 { "Y", -43200 }, 151 152 { "J", -2 }, 153 154 { "America/Los_Angeles", -28800 }, 155 { "America/New_York", -18000 }, 156 { "EST4EDT", -14400 }, 157 158 { "Bogus", -1 }, 159 }; 160 161 static void 162 ztest1(const char *name, const char *fmt, long value) 163 { 164 struct tm tm; 165 char *rv; 166 167 memset(&tm, 0, sizeof(tm)); 168 if ((rv = strptime(name, fmt, &tm)) == NULL) 169 tm.tm_gmtoff = -1; 170 else if (rv == name && fmt[1] == 'Z') 171 value = 0; 172 173 switch (value) { 174 case -2: 175 value = -timezone; 176 break; 177 case -1: 178 if (fmt[1] == 'Z') 179 value = 0; 180 break; 181 default: 182 break; 183 } 184 185 ATF_CHECK_MSG(tm.tm_gmtoff == value, 186 "strptime(\"%s\", \"%s\", &tm): " 187 "expected: tm.tm_gmtoff=%ld, got: tm.tm_gmtoff=%ld", 188 name, fmt, value, tm.tm_gmtoff); 189 printf("%s %s %ld\n", name, fmt, tm.tm_gmtoff); 190 } 191 192 static void 193 ztest(const char *fmt) 194 { 195 setenv("TZ", "US/Eastern", 1); 196 ztest1("GMT", fmt, 0); 197 ztest1("UTC", fmt, 0); 198 ztest1("US/Eastern", fmt, -18000); 199 for (size_t i = 0; i < __arraycount(zt); i++) 200 ztest1(zt[i].name, fmt, zt[i].offs); 201 } 202 203 ATF_TC(common); 204 205 ATF_TC_HEAD(common, tc) 206 { 207 208 atf_tc_set_md_var(tc, "descr", "Checks strptime(3): various checks"); 209 } 210 211 ATF_TC_BODY(common, tc) 212 { 213 214 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %T %Y", 215 24, 46, 27, 23, 20, 0, 98, 2, 19); 216 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %H:%M:%S %Y", 217 24, 46, 27, 23, 20, 0, 98, 2, 19); 218 h_pass("Tue Jan 20 23:27:46 1998", "%c", 219 24, 46, 27, 23, 20, 0, 98, 2, 19); 220 h_pass("Fri Mar 4 20:05:34 2005", "%a %b %e %H:%M:%S %Y", 221 24, 34, 5, 20, 4, 2, 105, 5, 62); 222 h_pass("5\t3 4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y", 223 21, 34, 5, 20, 4, 2, 105, 5, 62); 224 h_pass("Fri Mar 4 20:05:34 2005", "%c", 225 24, 34, 5, 20, 4, 2, 105, 5, 62); 226 } 227 228 ATF_TC(day); 229 230 ATF_TC_HEAD(day, tc) 231 { 232 233 atf_tc_set_md_var(tc, "descr", 234 "Checks strptime(3) day name conversions [aA]"); 235 } 236 237 ATF_TC_BODY(day, tc) 238 { 239 240 h_pass("Sun", "%a", 3, -1, -1, -1, -1, -1, -1, 0, -1); 241 h_pass("Sunday", "%a", 6, -1, -1, -1, -1, -1, -1, 0, -1); 242 h_pass("Mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1); 243 h_pass("Monday", "%a", 6, -1, -1, -1, -1, -1, -1, 1, -1); 244 h_pass("Tue", "%a", 3, -1, -1, -1, -1, -1, -1, 2, -1); 245 h_pass("Tuesday", "%a", 7, -1, -1, -1, -1, -1, -1, 2, -1); 246 h_pass("Wed", "%a", 3, -1, -1, -1, -1, -1, -1, 3, -1); 247 h_pass("Wednesday", "%a", 9, -1, -1, -1, -1, -1, -1, 3, -1); 248 h_pass("Thu", "%a", 3, -1, -1, -1, -1, -1, -1, 4, -1); 249 h_pass("Thursday", "%a", 8, -1, -1, -1, -1, -1, -1, 4, -1); 250 h_pass("Fri", "%a", 3, -1, -1, -1, -1, -1, -1, 5, -1); 251 h_pass("Friday", "%a", 6, -1, -1, -1, -1, -1, -1, 5, -1); 252 h_pass("Sat", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1); 253 h_pass("Saturday", "%a", 8, -1, -1, -1, -1, -1, -1, 6, -1); 254 h_pass("Saturn", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1); 255 h_fail("Moon", "%a"); 256 h_pass("Sun", "%A", 3, -1, -1, -1, -1, -1, -1, 0, -1); 257 h_pass("Sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1); 258 h_pass("Mon", "%A", 3, -1, -1, -1, -1, -1, -1, 1, -1); 259 h_pass("Monday", "%A", 6, -1, -1, -1, -1, -1, -1, 1, -1); 260 h_pass("Tue", "%A", 3, -1, -1, -1, -1, -1, -1, 2, -1); 261 h_pass("Tuesday", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1); 262 h_pass("Wed", "%A", 3, -1, -1, -1, -1, -1, -1, 3, -1); 263 h_pass("Wednesday", "%A", 9, -1, -1, -1, -1, -1, -1, 3, -1); 264 h_pass("Thu", "%A", 3, -1, -1, -1, -1, -1, -1, 4, -1); 265 h_pass("Thursday", "%A", 8, -1, -1, -1, -1, -1, -1, 4, -1); 266 h_pass("Fri", "%A", 3, -1, -1, -1, -1, -1, -1, 5, -1); 267 h_pass("Friday", "%A", 6, -1, -1, -1, -1, -1, -1, 5, -1); 268 h_pass("Sat", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1); 269 h_pass("Saturday", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1); 270 h_pass("Saturn", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1); 271 h_fail("Moon", "%A"); 272 273 h_pass("mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1); 274 h_pass("tueSDay", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1); 275 h_pass("sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1); 276 h_fail("sunday", "%EA"); 277 h_pass("SaturDay", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1); 278 h_fail("SaturDay", "%OA"); 279 } 280 281 ATF_TC(hour); 282 283 ATF_TC_HEAD(hour, tc) 284 { 285 286 atf_tc_set_md_var(tc, "descr", 287 "Checks strptime(3) hour conversions [IH]"); 288 } 289 290 ATF_TC_BODY(hour, tc) 291 { 292 293 h_fail("00", "%I"); 294 h_fail("13", "%I"); 295 296 h_pass("00", "%H", 2, -1, -1, 0, -1, -1, -1, -1, -1); 297 h_pass("12", "%H", 2, -1, -1, 12, -1, -1, -1, -1, -1); 298 h_pass("23", "%H", 2, -1, -1, 23, -1, -1, -1, -1, -1); 299 h_fail("24", "%H"); 300 } 301 302 303 ATF_TC(month); 304 305 ATF_TC_HEAD(month, tc) 306 { 307 308 atf_tc_set_md_var(tc, "descr", 309 "Checks strptime(3) month name conversions [bB]"); 310 } 311 312 ATF_TC_BODY(month, tc) 313 { 314 315 h_pass("Jan", "%b", 3, -1, -1, -1, -1, 0, -1, -1, -1); 316 h_pass("January", "%b", 7, -1, -1, -1, -1, 0, -1, -1, -1); 317 h_pass("Feb", "%b", 3, -1, -1, -1, -1, 1, -1, -1, -1); 318 h_pass("February", "%b", 8, -1, -1, -1, -1, 1, -1, -1, -1); 319 h_pass("Mar", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1); 320 h_pass("March", "%b", 5, -1, -1, -1, -1, 2, -1, -1, -1); 321 h_pass("Apr", "%b", 3, -1, -1, -1, -1, 3, -1, -1, -1); 322 h_pass("April", "%b", 5, -1, -1, -1, -1, 3, -1, -1, -1); 323 h_pass("May", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1); 324 h_pass("Jun", "%b", 3, -1, -1, -1, -1, 5, -1, -1, -1); 325 h_pass("June", "%b", 4, -1, -1, -1, -1, 5, -1, -1, -1); 326 h_pass("Jul", "%b", 3, -1, -1, -1, -1, 6, -1, -1, -1); 327 h_pass("July", "%b", 4, -1, -1, -1, -1, 6, -1, -1, -1); 328 h_pass("Aug", "%b", 3, -1, -1, -1, -1, 7, -1, -1, -1); 329 h_pass("August", "%b", 6, -1, -1, -1, -1, 7, -1, -1, -1); 330 h_pass("Sep", "%b", 3, -1, -1, -1, -1, 8, -1, -1, -1); 331 h_pass("September", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1); 332 h_pass("Oct", "%b", 3, -1, -1, -1, -1, 9, -1, -1, -1); 333 h_pass("October", "%b", 7, -1, -1, -1, -1, 9, -1, -1, -1); 334 h_pass("Nov", "%b", 3, -1, -1, -1, -1, 10, -1, -1, -1); 335 h_pass("November", "%b", 8, -1, -1, -1, -1, 10, -1, -1, -1); 336 h_pass("Dec", "%b", 3, -1, -1, -1, -1, 11, -1, -1, -1); 337 h_pass("December", "%b", 8, -1, -1, -1, -1, 11, -1, -1, -1); 338 h_pass("Mayor", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1); 339 h_pass("Mars", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1); 340 h_fail("Rover", "%b"); 341 h_pass("Jan", "%B", 3, -1, -1, -1, -1, 0, -1, -1, -1); 342 h_pass("January", "%B", 7, -1, -1, -1, -1, 0, -1, -1, -1); 343 h_pass("Feb", "%B", 3, -1, -1, -1, -1, 1, -1, -1, -1); 344 h_pass("February", "%B", 8, -1, -1, -1, -1, 1, -1, -1, -1); 345 h_pass("Mar", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1); 346 h_pass("March", "%B", 5, -1, -1, -1, -1, 2, -1, -1, -1); 347 h_pass("Apr", "%B", 3, -1, -1, -1, -1, 3, -1, -1, -1); 348 h_pass("April", "%B", 5, -1, -1, -1, -1, 3, -1, -1, -1); 349 h_pass("May", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1); 350 h_pass("Jun", "%B", 3, -1, -1, -1, -1, 5, -1, -1, -1); 351 h_pass("June", "%B", 4, -1, -1, -1, -1, 5, -1, -1, -1); 352 h_pass("Jul", "%B", 3, -1, -1, -1, -1, 6, -1, -1, -1); 353 h_pass("July", "%B", 4, -1, -1, -1, -1, 6, -1, -1, -1); 354 h_pass("Aug", "%B", 3, -1, -1, -1, -1, 7, -1, -1, -1); 355 h_pass("August", "%B", 6, -1, -1, -1, -1, 7, -1, -1, -1); 356 h_pass("Sep", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1); 357 h_pass("September", "%B", 9, -1, -1, -1, -1, 8, -1, -1, -1); 358 h_pass("Oct", "%B", 3, -1, -1, -1, -1, 9, -1, -1, -1); 359 h_pass("October", "%B", 7, -1, -1, -1, -1, 9, -1, -1, -1); 360 h_pass("Nov", "%B", 3, -1, -1, -1, -1, 10, -1, -1, -1); 361 h_pass("November", "%B", 8, -1, -1, -1, -1, 10, -1, -1, -1); 362 h_pass("Dec", "%B", 3, -1, -1, -1, -1, 11, -1, -1, -1); 363 h_pass("December", "%B", 8, -1, -1, -1, -1, 11, -1, -1, -1); 364 h_pass("Mayor", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1); 365 h_pass("Mars", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1); 366 h_fail("Rover", "%B"); 367 368 h_pass("september", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1); 369 h_pass("septembe", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1); 370 } 371 372 ATF_TC(seconds); 373 374 ATF_TC_HEAD(seconds, tc) 375 { 376 377 atf_tc_set_md_var(tc, "descr", 378 "Checks strptime(3) seconds conversions [S]"); 379 } 380 381 ATF_TC_BODY(seconds, tc) 382 { 383 384 h_pass("0", "%S", 1, 0, -1, -1, -1, -1, -1, -1, -1); 385 h_pass("59", "%S", 2, 59, -1, -1, -1, -1, -1, -1, -1); 386 h_pass("60", "%S", 2, 60, -1, -1, -1, -1, -1, -1, -1); 387 h_pass("61", "%S", 2, 61, -1, -1, -1, -1, -1, -1, -1); 388 h_fail("62", "%S"); 389 } 390 391 ATF_TC(year); 392 393 ATF_TC_HEAD(year, tc) 394 { 395 396 atf_tc_set_md_var(tc, "descr", 397 "Checks strptime(3) century/year conversions [CyY]"); 398 } 399 400 ATF_TC_BODY(year, tc) 401 { 402 403 h_pass("x20y", "x%Cy", 4, -1, -1, -1, -1, -1, 100, -1, -1); 404 h_pass("x84y", "x%yy", 4, -1, -1, -1, -1, -1, 84, -1, -1); 405 h_pass("x2084y", "x%C%yy", 6, -1, -1, -1, -1, -1, 184, -1, -1); 406 h_pass("x8420y", "x%y%Cy", 6, -1, -1, -1, -1, -1, 184, -1, -1); 407 h_pass("%20845", "%%%C%y5", 6, -1, -1, -1, -1, -1, 184, -1, -1); 408 h_fail("%", "%E%"); 409 410 h_pass("1980", "%Y", 4, -1, -1, -1, -1, -1, 80, -1, -1); 411 h_pass("1980", "%EY", 4, -1, -1, -1, -1, -1, 80, -1, -1); 412 } 413 414 ATF_TC(zone); 415 416 ATF_TC_HEAD(zone, tc) 417 { 418 419 atf_tc_set_md_var(tc, "descr", 420 "Checks strptime(3) timezone conversion [z]"); 421 } 422 423 424 ATF_TC_BODY(zone, tc) 425 { 426 ztest("%z"); 427 } 428 429 ATF_TC(Zone); 430 431 ATF_TC_HEAD(Zone, tc) 432 { 433 434 atf_tc_set_md_var(tc, "descr", 435 "Checks strptime(3) timezone conversion [Z]"); 436 } 437 438 439 ATF_TC_BODY(Zone, tc) 440 { 441 ztest("%Z"); 442 } 443 444 ATF_TP_ADD_TCS(tp) 445 { 446 447 ATF_TP_ADD_TC(tp, common); 448 ATF_TP_ADD_TC(tp, day); 449 ATF_TP_ADD_TC(tp, hour); 450 ATF_TP_ADD_TC(tp, month); 451 ATF_TP_ADD_TC(tp, seconds); 452 ATF_TP_ADD_TC(tp, year); 453 ATF_TP_ADD_TC(tp, zone); 454 ATF_TP_ADD_TC(tp, Zone); 455 456 return atf_no_error(); 457 } 458