1 #include <errno.h> 2 #include <gtest/gtest.h> 3 #include <sys/timeb.h> 4 #include <thread> 5 using namespace testing::ext; 6 7 class TimeTest : public testing::Test { SetUp()8 void SetUp() override {} TearDown()9 void TearDown() override {} 10 }; 11 12 const constexpr int BUFLEN = 512; 13 14 /** 15 * @tc.name: clock_001 16 * @tc.desc: Assess the accuracy of the clock function by comparing the CPU time measurements before and after a known 17 * delay, and verifying that the difference falls within an acceptable threshold of precision. 18 * @tc.type: FUNC 19 **/ 20 HWTEST_F(TimeTest, clock_001, TestSize.Level1) 21 { 22 clock_t time0 = clock(); 23 std::this_thread::sleep_for(std::chrono::seconds(1)); 24 clock_t time1 = clock(); 25 // 1s sleep should cost less than 5ms 26 EXPECT_GT(5 * (CLOCKS_PER_SEC / 1000), time1 - time0); 27 } 28 29 /** 30 * @tc.name: time_001 31 * @tc.desc: Verify the correctness and behavior of the time function by comparing the retrieved system time values, 32 * checking the relationship between consecutive measurements, and validating the functionality of 33 * time(nullptr) in obtaining the current system time. 34 * @tc.type: FUNC 35 **/ 36 HWTEST_F(TimeTest, time_001, TestSize.Level1) 37 { 38 time_t time1; 39 time_t p1 = time(&time1); 40 EXPECT_NE(0, static_cast<int>(time1)); 41 EXPECT_NE(-1, static_cast<int>(time1)); 42 EXPECT_EQ(time1, p1); 43 44 std::this_thread::sleep_for(std::chrono::milliseconds(1001)); 45 time_t time2; 46 time_t p2 = time(&time2); 47 EXPECT_NE(0, static_cast<int>(time2)); 48 EXPECT_NE(-1, static_cast<int>(time2)); 49 EXPECT_EQ(time2, p2); 50 51 EXPECT_GT(p2, p1); 52 EXPECT_GT(2, static_cast<int>(time2 - time1)); 53 54 EXPECT_LE(time(nullptr), p2); 55 EXPECT_GE(1, static_cast<int>(time(nullptr) - time2)); 56 } 57 58 /** 59 * @tc.name: mktime_001 60 * @tc.desc: Verify the correctness of the mktime function by checking the conversion of a struct tm to a time_t value 61 * in two different timezones. It validates that the function produces the expected time_t values for specific 62 * date and time inputs, considering the appropriate timezone information. 63 * @tc.type: FUNC 64 **/ 65 HWTEST_F(TimeTest, mktime_001, TestSize.Level1) 66 { 67 struct tm time; 68 memset(&time, 0, sizeof(tm)); 69 time.tm_year = 2023 - 1900; 70 time.tm_mon = 10; 71 time.tm_mday = 16; 72 73 setenv("TZ", "Asia/Shanghai", 1); 74 tzset(); 75 EXPECT_EQ(mktime(&time), static_cast<time_t>(1700064000)); 76 77 memset(&time, 0, sizeof(tm)); 78 time.tm_year = 2023 - 1900; 79 time.tm_mon = 10; 80 time.tm_mday = 16; 81 82 setenv("TZ", "", 1); 83 tzset(); 84 EXPECT_EQ(mktime(&time), static_cast<time_t>(1700092800)); 85 } 86 87 /** 88 * @tc.name: mktime_002 89 * @tc.desc: Verify the behavior of the mktime function when converting a struct tm representing a future date to a 90 * time_t value. It checks the correctness of the conversion in different timezones ("Asia/Shanghai" and 91 *"UTC") and ensures that no errors occur during the process by checking the value of errno. 92 * @tc.type: FUNC 93 **/ 94 HWTEST_F(TimeTest, mktime_002, TestSize.Level1) 95 { 96 struct tm time; 97 memset(&time, 0, sizeof(tm)); 98 time.tm_year = 2123 - 1900; 99 time.tm_mon = 10; 100 time.tm_mday = 16; 101 EXPECT_LE(64U, sizeof(time_t) * 8); 102 103 setenv("TZ", "Asia/Shanghai", 1); 104 tzset(); 105 errno = 0; 106 EXPECT_EQ(mktime(&time), static_cast<time_t>(4855737600U)); 107 EXPECT_EQ(errno, 0); 108 109 setenv("TZ", "UTC", 1); 110 tzset(); 111 errno = 0; 112 EXPECT_EQ(mktime(&time), static_cast<time_t>(4855766400U)); 113 EXPECT_EQ(errno, 0); 114 } 115 116 /** 117 * @tc.name: strftime_001 118 * @tc.desc: Verify the behavior of the strftime function in correctly formatting a given struct tm time value into a 119 * string representation. It checks the correctness of the formatted string using different format specifiers 120 * ("%s" and "%c") and ensures that the output matches the expected results in the UTC timezone. 121 * @tc.type: FUNC 122 **/ 123 HWTEST_F(TimeTest, strftime_001, TestSize.Level1) 124 { 125 setenv("TZ", "UTC", 1); 126 tzset(); 127 128 struct tm time; 129 memset(&time, 0, sizeof(tm)); 130 time.tm_year = 2123 - 1900; 131 time.tm_mon = 9; 132 time.tm_mday = 16; 133 char buf[BUFLEN]; 134 memset(&buf, 0, sizeof(buf)); 135 EXPECT_EQ(24U, strftime(buf, sizeof(buf), "%c", &time)); 136 EXPECT_STREQ("Sun Oct 16 00:00:00 2123", buf); 137 } 138 139 /** 140 * @tc.name: strftime_002 141 * @tc.desc: Verify the behavior of the strftime function in formatting the time zone information using 142 * the %Z specifier. It ensures that the formatted string reflects the correct time zone based on the local 143 * timezone settings,with the expected results for both the Shanghai and UTC timezones. 144 * @tc.type: FUNC 145 **/ 146 HWTEST_F(TimeTest, strftime_002, TestSize.Level1) 147 { 148 setenv("TZ", "Asia/Shanghai", 1); 149 tzset(); 150 time_t currentTime = time(nullptr); 151 struct tm* tmInfo = localtime(¤tTime); 152 char buf[BUFLEN]; 153 memset(&buf, 0, sizeof(buf)); 154 155 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", tmInfo)); 156 EXPECT_STREQ("<CST>", buf); 157 158 setenv("TZ", "UTC", 1); 159 tzset(); 160 EXPECT_EQ(5U, strftime(buf, sizeof(buf), "<%Z>", tmInfo)); 161 EXPECT_STREQ("<UTC>", buf); 162 } 163 164 /** 165 * @tc.name: strftime_003 166 * @tc.desc: Verify the behavior of strftime and strptime functions in handling time zone information and converting 167 * between Unix time and struct tm representations. It ensures that the generated Unix time and resulting 168 * struct tm representations are correct for both the Los Angeles timezone and UTC. 169 * @tc.type: FUNC 170 **/ 171 HWTEST_F(TimeTest, strftime_003, TestSize.Level1) 172 { 173 char buf[BUFLEN]; 174 memset(&buf, 0, sizeof(buf)); 175 const struct tm tim = { .tm_year = 2023 - 1900, .tm_mon = 9, .tm_mday = 16 }; 176 setenv("TZ", "Americe/Logs_Angeles", 1); 177 strftime(buf, sizeof(buf), "<%s>", &tim); 178 EXPECT_STREQ("<1697414400>", buf); 179 180 setenv("TZ", "UTC", 1); 181 strftime(buf, sizeof(buf), "<%s>", &tim); 182 EXPECT_STREQ("<1697414400>", buf); 183 184 struct tm tm; 185 setenv("TZ", "Asia/Shanghai", 1); 186 tzset(); 187 memset(&tm, 0xff, sizeof(tm)); 188 189 char* p = strptime("1697414400x", "%s", &tm); 190 EXPECT_EQ('x', *p); 191 EXPECT_EQ(0, tm.tm_sec); 192 EXPECT_EQ(0, tm.tm_min); 193 EXPECT_EQ(8, tm.tm_hour); 194 EXPECT_EQ(16, tm.tm_mday); 195 EXPECT_EQ(9, tm.tm_mon); 196 EXPECT_EQ(123, tm.tm_year); 197 EXPECT_EQ(1, tm.tm_wday); 198 EXPECT_EQ(288, tm.tm_yday); 199 EXPECT_EQ(0, tm.tm_isdst); 200 } 201 202 /** 203 * @tc.name: gmtime_001 204 * @tc.desc: Ensure that the gmtime function correctly converts the time value of 0 to a struct tm representation in 205 * UTC, with the expected values for the epoch. 206 * @tc.type: FUNC 207 **/ 208 HWTEST_F(TimeTest, gmtime_001, TestSize.Level1) 209 { 210 time_t tim = 2000000000; 211 struct tm* time = gmtime(&tim); 212 ASSERT_NE(time, nullptr); 213 EXPECT_EQ(20, time->tm_sec); 214 EXPECT_EQ(33, time->tm_min); 215 EXPECT_EQ(3, time->tm_hour); 216 EXPECT_EQ(18, time->tm_mday); 217 EXPECT_EQ(4, time->tm_mon); 218 EXPECT_EQ(2033, time->tm_year + 1900); 219 } 220 221 /** 222 * @tc.name: localtime_001 223 * @tc.desc: Ensure that the localtime function can correctly convert a Unix time value to a struct tm representation 224 * in two different time zones (Asia/Shanghai and America/Atka) using the TZ environment variable. 225 * @tc.type: FUNC 226 **/ 227 HWTEST_F(TimeTest, localtime_001, TestSize.Level1) 228 { 229 time_t tim = 2000000000; 230 setenv("TZ", "Asia/Shanghai", 1); 231 tzset(); 232 struct tm* tmC = localtime(&tim); 233 int ctime = tmC->tm_min; 234 EXPECT_EQ(33, ctime); 235 236 setenv("TZ", "America/Atka", 1); 237 struct tm* tmA = localtime(&tim); 238 int atime = tmA->tm_min; 239 EXPECT_EQ(33, atime); 240 } 241 242 /** 243 * @tc.name: asctime_001 244 * @tc.desc: Ensure that the asctime function correctly converts a struct tm representation of a time value with all 245 * fields set to 0 to a string representation using the standard format. 246 * @tc.type: FUNC 247 **/ 248 HWTEST_F(TimeTest, asctime_001, TestSize.Level1) 249 { 250 struct tm time; 251 memset(&time, 0, sizeof(tm)); 252 char* result = asctime(&time); 253 EXPECT_STREQ("Sun Jan 0 00:00:00 1900\n", result); 254 } 255 256 /** 257 * @tc.name: ctime_001 258 * @tc.desc: Verify that the ctime function correctly converts a Unix time value (0) to a string representation using 259 * the standard format when operating in the UTC timezone. 260 * @tc.type: FUNC 261 **/ 262 HWTEST_F(TimeTest, ctime_001, TestSize.Level1) 263 { 264 setenv("TZ", "UTC", 1); 265 tzset(); 266 const time_t tim = 0; 267 char* result = ctime(&tim); 268 EXPECT_STREQ("Thu Jan 1 00:00:00 1970\n", result); 269 } 270 271 /** 272 * @tc.name: timespec_get_001 273 * @tc.desc: Verify that the timespec_get function can correctly retrieve the current time in the UTC format and store 274 * it in a struct timespec variable. 275 * @tc.type: FUNC 276 **/ 277 HWTEST_F(TimeTest, timespec_get_001, TestSize.Level1) 278 { 279 struct timespec ts; 280 int result = timespec_get(&ts, TIME_UTC); 281 EXPECT_EQ(1, result); 282 } 283 284 /** 285 * @tc.name: strftime_l_001 286 * @tc.desc: Verify that the strftime_l function correctly formats a struct tm time value to a string representation 287 * using the specified locale. 288 * @tc.type: FUNC 289 **/ 290 HWTEST_F(TimeTest, strftime_l_001, TestSize.Level1) 291 { 292 char s[BUFLEN]; 293 memset(&s, 0, sizeof(s)); 294 size_t maxsize = sizeof(s); 295 setenv("TZ", "UTC", 1); 296 struct tm tp; 297 memset(&tp, 0, sizeof(tm)); 298 tp.tm_year = 2023 - 1900; 299 tp.tm_mon = 9; 300 tp.tm_mday = 16; 301 locale_t loc = newlocale(LC_ALL, "C.UTF-8", nullptr); 302 locale_t oldLoc = uselocale(loc); 303 size_t result = strftime_l(s, maxsize, "%c", &tp, loc); 304 EXPECT_EQ(24U, result); 305 EXPECT_STREQ("Sun Oct 16 00:00:00 2023", s); 306 307 uselocale(oldLoc); 308 freelocale(loc); 309 } 310 311 /** 312 * @tc.name: gmtime_r_001 313 * @tc.desc: Verify that the gmtime_r function correctly converts a given time in seconds since the epoch into a 314 * struct tm representation in UTC 315 * @tc.type: FUNC 316 **/ 317 HWTEST_F(TimeTest, gmtime_r_001, TestSize.Level1) 318 { 319 time_t tim = 2000000000; 320 struct tm tm = {}; 321 322 struct tm* time = gmtime_r(&tim, &tm); 323 EXPECT_EQ(time, &tm); 324 EXPECT_EQ(20, time->tm_sec); 325 EXPECT_EQ(33, time->tm_min); 326 EXPECT_EQ(3, time->tm_hour); 327 EXPECT_EQ(18, time->tm_mday); 328 EXPECT_EQ(4, time->tm_mon); 329 EXPECT_EQ(2033, time->tm_year + 1900); 330 } 331 332 /** 333 * @tc.name: localtime_r_001 334 * @tc.desc: Determine when the time zone is "Asia/Shanghai" When "localtime_r" Can correctly return to local time 335 * @tc.type: FUNC 336 **/ 337 HWTEST_F(TimeTest, localtime_r_001, TestSize.Level1) 338 { 339 time_t tim = 2000000000; 340 struct tm tm; 341 342 setenv("TZ", "Asia/Shanghai", 1); 343 tzset(); 344 ASSERT_NE(localtime_r(&tim, &tm), nullptr); 345 EXPECT_EQ(18, tm.tm_mday); 346 } 347 348 /** 349 * @tc.name: localtime_r_002 350 * @tc.desc: Determine when the time zone is "America/Los_Angeles" When "localtime_r" Can correctly return to local time 351 * @tc.type: FUNC 352 **/ 353 HWTEST_F(TimeTest, localtime_r_002, TestSize.Level1) 354 { 355 time_t tim = 2000000000; 356 struct tm tm; 357 setenv("TZ", "America/Los_Angeles", 1); 358 tzset(); 359 ASSERT_NE(localtime_r(&tim, &tm), nullptr); 360 EXPECT_EQ(17, tm.tm_mday); 361 } 362 363 /** 364 * @tc.name: localtime_r_003 365 * @tc.desc: Determine when the time zone is "Europe/London" When "localtime_r" Can correctly return to local time 366 * @tc.type: FUNC 367 **/ 368 HWTEST_F(TimeTest, localtime_r_003, TestSize.Level1) 369 { 370 time_t tim = 2000000000; 371 struct tm tm; 372 setenv("TZ", "Europe/London", 1); 373 tzset(); 374 ASSERT_NE(localtime_r(&tim, &tm), nullptr); 375 EXPECT_EQ(18, tm.tm_mday); 376 } 377 378 /** 379 * @tc.name: localtime_r_004 380 * @tc.desc: Determine when the time zone is "Pacific/Apia" When "localtime_r" Can correctly return to local time 381 * @tc.type: FUNC 382 **/ 383 HWTEST_F(TimeTest, localtime_r_004, TestSize.Level1) 384 { 385 time_t tim = 2000000000; 386 struct tm tm; 387 setenv("TZ", "Pacific/Apia", 1); 388 tzset(); 389 ASSERT_NE(localtime_r(&tim, &tm), nullptr); 390 EXPECT_EQ(18, tm.tm_mday); 391 } 392 393 /** 394 * @tc.name: localtime_r_005 395 * @tc.desc: Determine when the time zone is "Pacific/Honolulu" When "localtime_r" Can correctly return to local time 396 * @tc.type: FUNC 397 **/ 398 HWTEST_F(TimeTest, localtime_r_005, TestSize.Level1) 399 { 400 time_t tim = 2000000000; 401 struct tm tm; 402 setenv("TZ", "Pacific/Honolulu", 1); 403 tzset(); 404 ASSERT_NE(localtime_r(&tim, &tm), nullptr); 405 EXPECT_EQ(17, tm.tm_mday); 406 } 407 408 /** 409 * @tc.name: localtime_r_006 410 * @tc.desc: Determine when the time zone is "Asia/Magadan" When "localtime_r" Can correctly return to local time 411 * @tc.type: FUNC 412 **/ 413 HWTEST_F(TimeTest, localtime_r_006, TestSize.Level1) 414 { 415 time_t tim = 2000000000; 416 struct tm tm; 417 setenv("TZ", "Asia/Magadan", 1); 418 tzset(); 419 ASSERT_NE(localtime_r(&tim, &tm), nullptr); 420 EXPECT_EQ(18, tm.tm_mday); 421 } 422 423 /** 424 * @tc.name: asctime_r_001 425 * @tc.desc: Verify that the asctime_r function correctly converts a struct tm representation of a time into a 426 * formatted string 427 * @tc.type: FUNC 428 **/ 429 HWTEST_F(TimeTest, asctime_r_001, TestSize.Level1) 430 { 431 const struct tm tm = {}; 432 char buf[BUFLEN]; 433 memset(&buf, 0, sizeof(buf)); 434 char* result = asctime_r(&tm, buf); 435 EXPECT_EQ(buf, result); 436 EXPECT_STREQ("Sun Jan 0 00:00:00 1900\n", buf); 437 } 438 439 /** 440 * @tc.name: ctime_r_001 441 * @tc.desc: Verify that the ctime_r function correctly converts a time value in seconds since the epoch into a 442 * formatted string. 443 * @tc.type: FUNC 444 **/ 445 HWTEST_F(TimeTest, ctime_r_001, TestSize.Level1) 446 { 447 setenv("TZ", "UTC", 1); 448 const time_t tim = 0; 449 char buf[BUFLEN]; 450 memset(&buf, 0, sizeof(buf)); 451 char* result = ctime_r(&tim, buf); 452 EXPECT_EQ(buf, result); 453 EXPECT_STREQ("Thu Jan 1 00:00:00 1970\n", buf); 454 } 455 456 /** 457 * @tc.name: nanosleep_001 458 * @tc.desc: Verify that the nanosleep function correctly suspends the execution of a thread for a specified duration. 459 * It checks that the function returns 0 when the sleep completes uninterrupted. 460 * @tc.type: FUNC 461 **/ 462 HWTEST_F(TimeTest, nanosleep_001, TestSize.Level1) 463 { 464 time_t time0 = time(nullptr); 465 const timespec ts = { .tv_sec = 1 }; 466 int result = nanosleep(&ts, nullptr); 467 EXPECT_EQ(0, result); 468 time_t time1 = time(nullptr); 469 EXPECT_LE(static_cast<int>(1), time1 - time0); 470 } 471 472 /** 473 * @tc.name: nanosleep_002 474 * @tc.desc: Verify that the nanosleep function correctly handles an invalid sleep duration. It checks that the 475 * function returns -1 and sets errno to EINVAL when provided with a negative nanosecond value. 476 * @tc.type: FUNC 477 **/ 478 HWTEST_F(TimeTest, nanosleep_002, TestSize.Level1) 479 { 480 timespec ts = { .tv_nsec = -10000000 }; 481 errno = 0; 482 int result = nanosleep(&ts, nullptr); 483 EXPECT_EQ(-1, result); 484 EXPECT_EQ(EINVAL, errno); 485 } 486 487 /** 488 * @tc.name: strptime_001 489 * @tc.desc: Verify the correct behavior of the strptime and strftime functions in parsing and formatting time strings. 490 * It checks that the functions correctly parse the provided time strings according to the specified format 491 * and generate accurate formatted time strings. 492 * @tc.type: FUNC 493 **/ 494 HWTEST_F(TimeTest, strptime_001, TestSize.Level1) 495 { 496 setenv("TZ", "UTC", 1); 497 struct tm tim; 498 char buf[BUFLEN]; 499 memset(&buf, 0, sizeof(buf)); 500 501 memset(&tim, 0, sizeof(tim)); 502 strptime("09:57", "%R", &tim); 503 strftime(buf, sizeof(buf), "%H:%M", &tim); 504 EXPECT_STREQ("09:57", buf); 505 506 memset(&tim, 0, sizeof(tim)); 507 strptime("09:58:55", "%T", &tim); 508 strftime(buf, sizeof(buf), "%H:%M:%S", &tim); 509 EXPECT_STREQ("09:58:55", buf); 510 } 511 512 /** 513 * @tc.name: strptime_002 514 * @tc.desc: Ensure that the "strptime" function correctly parses dates in the "%F" format and produces the expected 515 * output, which is important for various applications that require date/time processing. 516 * @tc.type: FUNC 517 **/ 518 HWTEST_F(TimeTest, strptime_002, TestSize.Level1) 519 { 520 setenv("TZ", "UTC", 1); 521 struct tm tim = {}; 522 char* result = strptime("2023-10-20", "%F", &tim); 523 EXPECT_EQ('\0', *result); 524 EXPECT_EQ(123, tim.tm_year); 525 EXPECT_EQ(9, tim.tm_mon); 526 EXPECT_EQ(20, tim.tm_mday); 527 } 528 529 /** 530 * @tc.name: strftime_003 531 * @tc.desc: Ensure that the 'strptime' function correctly parses the timestamp in the '% s' format and generates 532 * the expected output 533 * @tc.type: FUNC 534 **/ 535 HWTEST_F(TimeTest, strptime_003, TestSize.Level1) 536 { 537 char buf[BUFLEN]; 538 memset(&buf, 0, sizeof(buf)); 539 struct tm tim = { .tm_year = 2023 - 1900, .tm_mon = 9, .tm_mday = 16, .tm_isdst = -1 }; 540 setenv("TZ", "Americe/Los_Angeles", 1); 541 strftime(buf, sizeof(buf), "<%s>", &tim); 542 EXPECT_STREQ("<1697414400>", buf); 543 544 setenv("TZ", "UTC", 1); 545 strftime(buf, sizeof(buf), "<%s>", &tim); 546 EXPECT_STREQ("<1697414400>", buf); 547 548 struct tm tm; 549 550 setenv("TZ", "Asia/Shanghai", 1); 551 tzset(); 552 memset(&tm, 0xff, sizeof(tm)); 553 554 char* p = strptime("1697414400x", "%s", &tm); 555 EXPECT_EQ('x', *p); 556 EXPECT_EQ(0, tm.tm_sec); 557 EXPECT_EQ(0, tm.tm_min); 558 EXPECT_EQ(8, tm.tm_hour); 559 EXPECT_EQ(16, tm.tm_mday); 560 EXPECT_EQ(9, tm.tm_mon); 561 EXPECT_EQ(123, tm.tm_year); 562 EXPECT_EQ(1, tm.tm_wday); 563 EXPECT_EQ(288, tm.tm_yday); 564 EXPECT_EQ(0, tm.tm_isdst); 565 } 566 567 /** 568 * @tc.name: strptime_004 569 * @tc.desc: Ensure that the "strptime" function correctly handles different representations of the AM/PM indicator 570 * ("%p" and "%P") and does not modify the hour field when the indicator is parsed. 571 * @tc.type: FUNC 572 **/ 573 HWTEST_F(TimeTest, strptime_004, TestSize.Level1) 574 { 575 setenv("TZ", "UTC", 1); 576 577 struct tm tim = { .tm_hour = 12 }; 578 EXPECT_EQ('\0', *strptime("AM", "%p", &tim)); 579 EXPECT_EQ(0, tim.tm_hour); 580 581 tim = { .tm_hour = 12 }; 582 EXPECT_EQ('\0', *strptime("am", "%p", &tim)); 583 EXPECT_EQ(0, tim.tm_hour); 584 585 tim = { .tm_hour = 12 }; 586 EXPECT_EQ('\0', *strptime("AM", "%P", &tim)); 587 EXPECT_EQ(0, tim.tm_hour); 588 589 tim = { .tm_hour = 12 }; 590 EXPECT_EQ('\0', *strptime("am", "%P", &tim)); 591 EXPECT_EQ(0, tim.tm_hour); 592 } 593 594 /** 595 * @tc.name: strptime_005 596 * @tc.desc: Ensure that the "strptime" function correctly parses and assigns the weekday information based on the "%u" 597 * format specifier, allowing for accurate conversions between weekday representations and "struct tm" 598 *objects. 599 * @tc.type: FUNC 600 **/ 601 HWTEST_F(TimeTest, strptime_005, TestSize.Level1) 602 { 603 setenv("TZ", "UTC", 1); 604 struct tm tim = {}; 605 char* result = strptime("5", "%u", &tim); 606 EXPECT_EQ('\0', *result); 607 EXPECT_EQ(5, tim.tm_wday); 608 } 609 610 /** 611 * @tc.name: strptime_006 612 * @tc.desc: Verify that the "strptime" function correctly parses and assigns the date components based on the "%v" 613 * format specifier, allowing for accurate conversions between date representations and "struct tm" objects. 614 * @tc.type: FUNC 615 **/ 616 HWTEST_F(TimeTest, strptime_006, TestSize.Level1) 617 { 618 setenv("TZ", "UTC", 1); 619 struct tm tim = {}; 620 char* result = strptime("25-Mar-2023", "%v", &tim); 621 EXPECT_EQ('\0', *result); 622 EXPECT_EQ(123, tim.tm_year); 623 EXPECT_EQ(2, tim.tm_mon); 624 EXPECT_EQ(25, tim.tm_mday); 625 } 626 627 /** 628 * @tc.name: strptime_007 629 * @tc.desc: Verify that the "strptime" function properly handles invalid inputs and does not assign any 630 * values to the "struct tm" object in such cases. 631 * @tc.type: FUNC 632 **/ 633 HWTEST_F(TimeTest, strptime_007, TestSize.Level1) 634 { 635 struct tm tim; 636 char* result = strptime("x", "%s", &tim); 637 EXPECT_EQ(nullptr, result); 638 } 639 640 /** 641 * @tc.name: wcsftime_001 642 * @tc.desc: Verify that the "wcsftime" function correctly formats a "struct tm" object as a wide character string 643 * according to the specified format specifier and the current locale. 644 * @tc.type: FUNC 645 **/ 646 HWTEST_F(TimeTest, wcsftime_001, TestSize.Level1) 647 { 648 setenv("TZ", "UTC", 1); 649 struct tm tim; 650 memset(&tim, 0, sizeof(tm)); 651 tim.tm_year = 2023 - 1900; 652 tim.tm_mon = 9; 653 tim.tm_mday = 16; 654 655 wchar_t buf[BUFLEN]; 656 memset(&buf, 0, sizeof(buf)); 657 size_t result = wcsftime(buf, sizeof(buf), L"%c", &tim); 658 EXPECT_EQ(24U, result); 659 EXPECT_STREQ(L"Sun Oct 16 00:00:00 2023", buf); 660 } 661 662 /** 663 * @tc.name: gettimeofday_001 664 * @tc.desc: Verify that the "gettimeofday" function is correctly retrieving the current time and that the obtained 665 * values are reasonably close to the system time obtained through other means. 666 * @tc.type: FUNC 667 **/ 668 HWTEST_F(TimeTest, gettimeofday_001, TestSize.Level1) 669 { 670 timeval tv1; 671 EXPECT_EQ(gettimeofday(&tv1, nullptr), 0); 672 auto currentTime = std::chrono::system_clock::now(); 673 auto duration = currentTime.time_since_epoch(); 674 675 auto sec = std::chrono::duration_cast<std::chrono::seconds>(duration).count(); 676 auto usec = std::chrono::duration_cast<std::chrono::microseconds>(duration).count() % 1000000; 677 678 long secDiff = sec - tv1.tv_sec; 679 long usecDiff = usec - tv1.tv_usec; 680 681 if (usecDiff < 0) { 682 --secDiff; 683 usecDiff += 1000000; 684 } 685 long maxSecDiff = 0; 686 EXPECT_LE(secDiff, maxSecDiff); 687 }