1/** 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include <stdlib.h> 17#include <time.h> 18#include "asctime_data.h" 19#include "functionalext.h" 20#include "strftime_data.h" 21 22static time_t gTime = 1659177614; 23static int16_t gBufferSize = 256; 24 25/** 26 * @tc.name : strftime_0100 27 * @tc.desc : according to different time zones, format date 28 * @tc.level : Level 0 29 */ 30void strftime_0100(void) 31{ 32 for (int32_t i = 0; i < (int32_t)(sizeof(test_asctime_data) / sizeof(test_asctime_data[0])); i++) { 33 const char *handlerChar = test_handle_path(test_asctime_data[i].tz); 34 if (!handlerChar) { 35 t_error("strftime_0100 failed: handlerChar is NULL\n"); 36 continue; 37 } 38 39 setenv("TZ", handlerChar, 1); 40 tzset(); 41 char buffer[gBufferSize]; 42 struct tm *timeptr = localtime(&gTime); 43 if (!timeptr) { 44 EXPECT_PTRNE("strftime_0100", timeptr, NULL); 45 return; 46 } 47 size_t count = strftime(buffer, sizeof(buffer) - 1, "%c", timeptr); 48 EXPECT_TRUE("strftime_0100", count > 0); 49 EXPECT_STREQ("strftime_0100", buffer, test_asctime_data[i].result); 50 } 51} 52 53/** 54 * @tc.name : strftime_0200 55 * @tc.desc : according to different time zones, format date 56 * @tc.level : Level 0 57 */ 58void strftime_0200(void) 59{ 60 for (int32_t i = 0; i < (int32_t)(sizeof(test_strftime_data) / sizeof(test_strftime_data[0])); i++) { 61 const char *handlerChar = test_handle_path(test_strftime_data[i].tz); 62 if (!handlerChar) { 63 t_error("strftime_0200 failed: handlerChar is NULL\n"); 64 continue; 65 } 66 67 setenv("TZ", handlerChar, 1); 68 tzset(); 69 struct tm *timeptr = localtime(&gTime); 70 if (!timeptr) { 71 EXPECT_PTRNE("strftime_0200", timeptr, NULL); 72 return; 73 } 74 char buffer[gBufferSize]; 75 size_t count = strftime(buffer, sizeof(buffer) - 1, "%c %Z%z", timeptr); 76 EXPECT_TRUE("strftime_0200", count > 0); 77 EXPECT_STREQ("strftime_0200", buffer, test_strftime_data[i].result); 78 } 79} 80 81/** 82 * @tc.name : strftime_0300 83 * @tc.desc : according to different time zones, format date 84 * @tc.level : Level 0 85 */ 86void strftime_0300(void) 87{ 88 const char *handlerChar = test_handle_path("Pacific/Pitcairn"); 89 if (!handlerChar) { 90 t_error("strftime_0300 failed: handlerChar is NULL\n"); 91 return; 92 } 93 94 setenv("TZ", handlerChar, 1); 95 tzset(); 96 struct tm *timeptr = localtime(&gTime); 97 if (!timeptr) { 98 EXPECT_PTRNE("strftime_0300", timeptr, NULL); 99 return; 100 } 101 char buffer[gBufferSize]; 102 size_t count = strftime(buffer, sizeof(buffer) - 1, "%k", timeptr); 103 EXPECT_TRUE("strftime_0300", count > 0); 104 EXPECT_STREQ("strftime_0300", buffer, " 2"); 105} 106 107/** 108 * @tc.name : strftime_0400 109 * @tc.desc : according to different time zones, format date 110 * @tc.level : Level 0 111 */ 112void strftime_0400(void) 113{ 114 const char *handlerChar = test_handle_path("Asia/Shanghai"); 115 if (!handlerChar) { 116 t_error("strftime_0400 failed: handlerChar is NULL\n"); 117 return; 118 } 119 120 setenv("TZ", handlerChar, 1); 121 tzset(); 122 struct tm *timeptr = localtime(&gTime); 123 if (!timeptr) { 124 EXPECT_PTRNE("strftime_0400", timeptr, NULL); 125 return; 126 } 127 char buffer[gBufferSize]; 128 size_t count = strftime(buffer, sizeof(buffer) - 1, "%k", timeptr); 129 EXPECT_TRUE("strftime_0400", count > 0); 130 EXPECT_STREQ("strftime_0400", buffer, "18"); 131} 132 133/** 134 * @tc.name : strftime_0500 135 * @tc.desc : according to different time zones, format date 136 * @tc.level : Level 0 137 */ 138void strftime_0500(void) 139{ 140 const char *handlerChar = test_handle_path("Asia/Shanghai"); 141 if (!handlerChar) { 142 t_error("strftime_0500 failed: handlerChar is NULL\n"); 143 return; 144 } 145 146 setenv("TZ", handlerChar, 1); 147 tzset(); 148 struct tm *timeptr = localtime(&gTime); 149 if (!timeptr) { 150 EXPECT_PTRNE("strftime_0500", timeptr, NULL); 151 return; 152 } 153 char buffer[gBufferSize]; 154 size_t count = strftime(buffer, sizeof(buffer) - 1, "%I", timeptr); 155 EXPECT_TRUE("strftime_0500", count > 0); 156 EXPECT_STREQ("strftime_0500", buffer, "06"); 157} 158 159/** 160 * @tc.name : strftime_0600 161 * @tc.desc : according to different time zones, format date 162 * @tc.level : Level 0 163 */ 164void strftime_0600(void) 165{ 166 const char *handlerChar = test_handle_path("Asia/Shanghai"); 167 if (!handlerChar) { 168 t_error("strftime_0600 failed: handlerChar is NULL\n"); 169 return; 170 } 171 172 setenv("TZ", handlerChar, 1); 173 tzset(); 174 struct tm *timeptr = localtime(&gTime); 175 if (!timeptr) { 176 EXPECT_PTRNE("strftime_0600", timeptr, NULL); 177 return; 178 } 179 char buffer[gBufferSize]; 180 size_t count = strftime(buffer, sizeof(buffer) - 1, "%P", timeptr); 181 EXPECT_TRUE("strftime_0600", count > 0); 182 EXPECT_STREQ("strftime_0600", buffer, "pm"); 183} 184 185/** 186 * @tc.name : strftime_0700 187 * @tc.desc : according to different time zones, format date 188 * @tc.level : Level 0 189 */ 190void strftime_0700(void) 191{ 192 const char *handlerChar = test_handle_path("Asia/Shanghai"); 193 if (!handlerChar) { 194 t_error("strftime_0700 failed: handlerChar is NULL\n"); 195 return; 196 } 197 198 setenv("TZ", handlerChar, 1); 199 tzset(); 200 struct tm *timeptr = localtime(&gTime); 201 if (!timeptr) { 202 EXPECT_PTRNE("strftime_0700", timeptr, NULL); 203 return; 204 } 205 char buffer[gBufferSize]; 206 size_t count = strftime(buffer, sizeof(buffer) - 1, "%v", timeptr); 207 EXPECT_TRUE("strftime_0700", count > 0); 208 EXPECT_STREQ("strftime_0700", buffer, "30-Jul-2022"); 209} 210 211/** 212 * @tc.name : strftime_0800 213 * @tc.desc : according to different time zones, format date 214 * @tc.level : Level 0 215 */ 216void strftime_0800(void) 217{ 218 const char *handlerChar = test_handle_path("Asia/Shanghai"); 219 if (!handlerChar) { 220 t_error("strftime_0800 failed: handlerChar is NULL\n"); 221 return; 222 } 223 224 setenv("TZ", handlerChar, 1); 225 tzset(); 226 struct tm *timeptr = localtime(&gTime); 227 if (!timeptr) { 228 EXPECT_PTRNE("strftime_0800", timeptr, NULL); 229 return; 230 } 231 char buffer[gBufferSize]; 232 size_t count = strftime(buffer, sizeof(buffer) - 1, "%j", timeptr); 233 EXPECT_TRUE("strftime_0800", count > 0); 234 EXPECT_STREQ("strftime_0800", buffer, "211"); 235} 236 237/** 238 * @tc.name : strftime_0900 239 * @tc.desc : according to different time zones, format date 240 * @tc.level : Level 0 241 */ 242void strftime_0900(void) 243{ 244 const char *handlerChar = test_handle_path("Asia/Shanghai"); 245 if (!handlerChar) { 246 t_error("strftime_0900 failed: handlerChar is NULL\n"); 247 return; 248 } 249 250 setenv("TZ", handlerChar, 1); 251 tzset(); 252 struct tm *timeptr = localtime(&gTime); 253 if (!timeptr) { 254 EXPECT_PTRNE("strftime_0900", timeptr, NULL); 255 return; 256 } 257 char buffer[gBufferSize]; 258 size_t count = strftime(buffer, sizeof(buffer) - 1, "%l", timeptr); 259 EXPECT_TRUE("strftime_0900", count > 0); 260 EXPECT_STREQ("strftime_0900", buffer, " 6"); 261} 262 263/** 264 * @tc.name : strftime_1000 265 * @tc.desc : according to different time zones, format date 266 * @tc.level : Level 0 267 */ 268void strftime_1000(void) 269{ 270 const char *handlerChar = test_handle_path("Asia/Shanghai"); 271 if (!handlerChar) { 272 t_error("strftime_1000 failed: handlerChar is NULL\n"); 273 return; 274 } 275 276 setenv("TZ", handlerChar, 1); 277 tzset(); 278 struct tm *timeptr = localtime(&gTime); 279 if (!timeptr) { 280 EXPECT_PTRNE("strftime_1000", timeptr, NULL); 281 return; 282 } 283 timeptr->tm_mday = 31; 284 timeptr->tm_mon = 11; 285 timeptr->tm_year = 124; 286 timeptr->tm_wday = 2; 287 timeptr->tm_yday = 365; 288 289 char buffer[gBufferSize]; 290 size_t count = strftime(buffer, sizeof(buffer) - 1, "%V", timeptr); 291 EXPECT_TRUE("strftime_1000", count > 0); 292 EXPECT_STREQ("strftime_1000", buffer, "01"); 293} 294 295/** 296 * @tc.name : strftime_1100 297 * @tc.desc : according to different time zones, format date 298 * @tc.level : Level 0 299 */ 300void strftime_1100(void) 301{ 302 struct tm tm = {0}; 303 char buffer[gBufferSize]; 304 size_t count = strftime(buffer, sizeof(buffer) - 1, "%l", &tm); 305 EXPECT_TRUE("strftime_1100", count > 0); 306 EXPECT_STREQ("strftime_1100", buffer, "12"); 307} 308 309/** 310 * @tc.name : strftime_1200 311 * @tc.desc : according to different time zones, format date 312 * @tc.level : Level 0 313 */ 314void strftime_1200(void) 315{ 316 const char *handlerChar = test_handle_path("Asia/Shanghai"); 317 if (!handlerChar) { 318 t_error("strftime_1200 failed: handlerChar is NULL\n"); 319 return; 320 } 321 322 setenv("TZ", handlerChar, 1); 323 tzset(); 324 struct tm *timeptr = localtime(&gTime); 325 if (!timeptr) { 326 EXPECT_PTRNE("strftime_1200", timeptr, NULL); 327 return; 328 } 329 timeptr->tm_mday = 28; 330 timeptr->tm_mon = 12; 331 timeptr->tm_year = 200; 332 timeptr->tm_wday = 1; 333 timeptr->tm_yday = 362; 334 335 char buffer[gBufferSize]; 336 size_t count = strftime(buffer, sizeof(buffer) - 1, "%G", timeptr); 337 EXPECT_TRUE("strftime_1200", count > 0); 338 EXPECT_STREQ("strftime_1200", buffer, "2101"); 339} 340 341/** 342 * @tc.name : strftime_1300 343 * @tc.desc : according to different time zones, format date 344 * @tc.level : Level 1 345 */ 346void strftime_1300(void) 347{ 348 const char *handlerChar = test_handle_path("Asia/Shanghai"); 349 if (!handlerChar) { 350 t_error("strftime_1300 failed: handlerChar is NULL\n"); 351 return; 352 } 353 354 setenv("TZ", handlerChar, 1); 355 tzset(); 356 struct tm *timeptr = localtime(&gTime); 357 if (!timeptr) { 358 EXPECT_PTRNE("strftime_1300", timeptr, NULL); 359 return; 360 } 361 timeptr->tm_mday = 28; 362 timeptr->tm_mon = 12; 363 timeptr->tm_year = 100; 364 timeptr->tm_wday = 1; 365 timeptr->tm_yday = 362; 366 367 char buffer[gBufferSize]; 368 size_t count = strftime(buffer, sizeof(buffer) - 1, "%G", timeptr); 369 EXPECT_TRUE("strftime_1300", count > 0); 370 EXPECT_STREQ("strftime_1300", buffer, "2000"); 371} 372 373/** 374 * @tc.name : strftime_1400 375 * @tc.desc : according to different time zones, format date 376 * @tc.level : Level 1 377 */ 378void strftime_1400(void) 379{ 380 const char *handlerChar = test_handle_path("Asia/Shanghai"); 381 if (!handlerChar) { 382 t_error("strftime_1400 failed: handlerChar is NULL\n"); 383 return; 384 } 385 386 setenv("TZ", handlerChar, 1); 387 tzset(); 388 struct tm *timeptr = localtime(&gTime); 389 if (!timeptr) { 390 EXPECT_PTRNE("strftime_1400", timeptr, NULL); 391 return; 392 } 393 timeptr->tm_mday = 28; 394 timeptr->tm_mon = 12; 395 timeptr->tm_year = 101; 396 timeptr->tm_wday = 1; 397 timeptr->tm_yday = 362; 398 399 char buffer[gBufferSize]; 400 size_t count = strftime(buffer, sizeof(buffer) - 1, "%G", timeptr); 401 EXPECT_TRUE("strftime_1400", count > 0); 402 EXPECT_STREQ("strftime_1400", buffer, "2002"); 403} 404 405/** 406 * @tc.name : strftime_1500 407 * @tc.desc : according to different time zones, format date 408 * @tc.level : Level 1 409 */ 410void strftime_1500(void) 411{ 412 const char *handlerChar = test_handle_path("Asia/Shanghai"); 413 if (!handlerChar) { 414 t_error("strftime_1500 failed: handlerChar is NULL\n"); 415 return; 416 } 417 418 setenv("TZ", handlerChar, 1); 419 tzset(); 420 struct tm *timeptr = localtime(&gTime); 421 if (!timeptr) { 422 EXPECT_PTRNE("strftime_1500", timeptr, NULL); 423 return; 424 } 425 426 char buffer[gBufferSize]; 427 size_t count = strftime(buffer, sizeof(buffer) - 1, "%B", timeptr); 428 EXPECT_TRUE("strftime_1500", count > 0); 429 EXPECT_STREQ("strftime_1500", buffer, "July"); 430} 431 432/** 433 * @tc.name : strftime_1600 434 * @tc.desc : according to different time zones, format date 435 * @tc.level : Level 1 436 */ 437void strftime_1600(void) 438{ 439 const char *handlerChar = test_handle_path("Asia/Shanghai"); 440 if (!handlerChar) { 441 t_error("strftime_1600 failed: handlerChar is NULL\n"); 442 return; 443 } 444 445 setenv("TZ", handlerChar, 1); 446 tzset(); 447 struct tm *timeptr = localtime(&gTime); 448 if (!timeptr) { 449 EXPECT_PTRNE("strftime_1600", timeptr, NULL); 450 return; 451 } 452 timeptr->tm_mon = 12; 453 454 char buffer[gBufferSize]; 455 size_t count = strftime(buffer, sizeof(buffer) - 1, "%B", timeptr); 456 EXPECT_TRUE("strftime_1600", count > 0); 457 EXPECT_STREQ("strftime_1600", buffer, "-"); 458} 459 460/** 461 * @tc.name : strftime_1700 462 * @tc.desc : according to different time zones, format date 463 * @tc.level : Level 1 464 */ 465void strftime_1700(void) 466{ 467 struct tm tm = {0}; 468 tm.tm_hour = 13; 469 char buffer[gBufferSize]; 470 size_t count = strftime(buffer, sizeof(buffer) - 1, "%l", &tm); 471 EXPECT_TRUE("strftime_1700", count > 0); 472 EXPECT_STREQ("strftime_1700", buffer, " 1"); 473} 474 475/** 476 * @tc.name : strftime_1800 477 * @tc.desc : according to different time zones, format date 478 * @tc.level : Level 1 479 */ 480void strftime_1800(void) 481{ 482 struct tm tm = {0}; 483 char buffer[gBufferSize]; 484 size_t count = strftime(buffer, sizeof(buffer) - 1, "%u", &tm); 485 EXPECT_TRUE("strftime_1800", count > 0); 486 EXPECT_STREQ("strftime_1800", buffer, "7"); 487} 488 489/** 490 * @tc.name : strftime_1900 491 * @tc.desc : according to different time zones, format date 492 * @tc.level : Level 1 493 */ 494void strftime_1900(void) 495{ 496 struct tm tm = {0}; 497 tm.tm_wday = 3; 498 char buffer[gBufferSize]; 499 size_t count = strftime(buffer, sizeof(buffer) - 1, "%u", &tm); 500 EXPECT_TRUE("strftime_1900", count > 0); 501 EXPECT_STREQ("strftime_1900", buffer, "3"); 502} 503 504/** 505 * @tc.name : strftime_2000 506 * @tc.desc : according to different time zones, format date 507 * @tc.level : Level 1 508 */ 509void strftime_2000(void) 510{ 511 struct tm tm = {0}; 512 tm.tm_year = -1999; 513 char buffer[gBufferSize]; 514 size_t count = strftime(buffer, sizeof(buffer) - 1, "%-y", &tm); 515 EXPECT_TRUE("strftime_2000", count > 0); 516 EXPECT_STREQ("strftime_2000", buffer, "99"); 517} 518 519/** 520 * @tc.name : strftime_2100 521 * @tc.desc : according to different time zones, format date 522 * @tc.level : Level 1 523 */ 524void strftime_2100(void) 525{ 526 struct tm tm = {0}; 527 tm.tm_isdst = -1; 528 char buffer[gBufferSize]; 529 size_t count = strftime(buffer, sizeof(buffer) - 1, "%z", &tm); 530 EXPECT_TRUE("strftime_2100", count == 0); 531 EXPECT_STREQ("strftime_2100", buffer, ""); 532} 533 534/** 535 * @tc.name : strftime_2200 536 * @tc.desc : according to different time zones, format date 537 * @tc.level : Level 1 538 */ 539void strftime_2200(void) 540{ 541 struct tm tm = {0}; 542 tm.tm_isdst = -1; 543 char buffer[gBufferSize]; 544 size_t count = strftime(buffer, sizeof(buffer) - 1, "%Z", &tm); 545 EXPECT_TRUE("strftime_2200", count == 0); 546 EXPECT_STREQ("strftime_2200", buffer, ""); 547} 548 549/** 550 * @tc.name : strftime_2300 551 * @tc.desc : according to different time zones, format date 552 * @tc.level : Level 1 553 */ 554void strftime_2300(void) 555{ 556 struct tm tm = {0}; 557 char buffer[gBufferSize]; 558 size_t count = strftime(buffer, sizeof(buffer) - 1, "%E%", &tm); 559 EXPECT_TRUE("strftime_2300", count > 0); 560 EXPECT_STREQ("strftime_2300", buffer, "%"); 561} 562 563/** 564 * @tc.name : strftime_2200 565 * @tc.desc : according to different time zones, format date 566 * @tc.level : Level 1 567 */ 568void strftime_2400(void) 569{ 570 struct tm tm = {0}; 571 char buffer[gBufferSize]; 572 size_t count = strftime(buffer, sizeof(buffer) - 1, "%O%", &tm); 573 EXPECT_TRUE("strftime_2400", count > 0); 574 EXPECT_STREQ("strftime_2400", buffer, "%"); 575} 576 577int main(void) 578{ 579 strftime_0100(); 580 strftime_0200(); 581 strftime_0300(); 582 strftime_0400(); 583 strftime_0500(); 584 strftime_0600(); 585 strftime_0700(); 586 strftime_0800(); 587 strftime_0900(); 588 strftime_1000(); 589 strftime_1100(); 590 strftime_1200(); 591 strftime_1300(); 592 strftime_1400(); 593 strftime_1500(); 594 strftime_1600(); 595 strftime_1700(); 596 strftime_1800(); 597 strftime_1900(); 598 strftime_2000(); 599 strftime_2100(); 600 strftime_2200(); 601 strftime_2300(); 602 strftime_2400(); 603 return t_status; 604}