| 1 | #include <base/detect.h> |
| 2 | #include <base/time.h> |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | |
| 8 | class TimestampTest : public testing::Test // NOLINT(readability-identifier-naming) |
| 9 | { |
| 10 | protected: |
| 11 | static void SetTimezone(const char *pTimezone) |
| 12 | { |
| 13 | #if defined(CONF_FAMILY_WINDOWS) |
| 14 | _putenv_s("TZ" , pTimezone); |
| 15 | _tzset(); |
| 16 | #else |
| 17 | setenv(name: "TZ" , value: pTimezone, replace: 1); |
| 18 | tzset(); |
| 19 | #endif |
| 20 | } |
| 21 | |
| 22 | void SetUp() override |
| 23 | { |
| 24 | SetTimezone("UTC" ); |
| 25 | #if defined(CONF_FAMILY_WINDOWS) |
| 26 | _dstbias = 0; |
| 27 | #endif |
| 28 | } |
| 29 | |
| 30 | void TearDown() override |
| 31 | { |
| 32 | SetTimezone("UTC" ); |
| 33 | #if defined(CONF_FAMILY_WINDOWS) |
| 34 | _dstbias = 0; |
| 35 | #endif |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | TEST_F(TimestampTest, FromStr) |
| 40 | { |
| 41 | time_t Timestamp; |
| 42 | EXPECT_TRUE(timestamp_from_str("2023-12-31_12-58-55" , TimestampFormat::NOSPACE, &Timestamp)); |
| 43 | EXPECT_EQ(Timestamp, 1704027535); |
| 44 | |
| 45 | EXPECT_TRUE(timestamp_from_str("2012-02-29_13-00-00" , TimestampFormat::NOSPACE, &Timestamp)); |
| 46 | EXPECT_EQ(Timestamp, 1330520400); |
| 47 | |
| 48 | EXPECT_TRUE(timestamp_from_str("2004-05-15 18:13:53" , TimestampFormat::SPACE, &Timestamp)); |
| 49 | EXPECT_EQ(Timestamp, 1084644833); |
| 50 | } |
| 51 | |
| 52 | TEST_F(TimestampTest, FromStrDaylightSavingTime) |
| 53 | { |
| 54 | SetTimezone("CET-1CEST,M3.5.0,M10.5.0/3" ); |
| 55 | #if defined(CONF_FAMILY_WINDOWS) |
| 56 | // Windows is annoying, workaround for test |
| 57 | _dstbias = -3600; |
| 58 | #endif |
| 59 | |
| 60 | // Format and parse must round trip during daylight saving time |
| 61 | char aTimestamp[20]; |
| 62 | str_timestamp_ex(time: 1690000000, buffer: aTimestamp, buffer_size: sizeof(aTimestamp), format: TimestampFormat::NOSPACE); |
| 63 | EXPECT_STREQ(aTimestamp, "2023-07-22_06-26-40" ); |
| 64 | |
| 65 | time_t Timestamp; |
| 66 | EXPECT_TRUE(timestamp_from_str(aTimestamp, TimestampFormat::NOSPACE, &Timestamp)); |
| 67 | EXPECT_EQ(Timestamp, 1690000000); |
| 68 | } |
| 69 | |
| 70 | TEST_F(TimestampTest, FromStrFailing) |
| 71 | { |
| 72 | time_t Timestamp; |
| 73 | // Invalid time string |
| 74 | EXPECT_FALSE(timestamp_from_str("123 2023-12-31_12-58-55" , TimestampFormat::NOSPACE, &Timestamp)); |
| 75 | |
| 76 | // Invalid time string |
| 77 | EXPECT_FALSE(timestamp_from_str("555-02-29_13-12-7" , TimestampFormat::NOSPACE, &Timestamp)); |
| 78 | |
| 79 | // Time string does not fit the format |
| 80 | EXPECT_FALSE(timestamp_from_str("2004-05-15 18-13-53" , TimestampFormat::SPACE, &Timestamp)); |
| 81 | |
| 82 | // Invalid time string |
| 83 | EXPECT_FALSE(timestamp_from_str("2000-01-01 00:00:00:00" , TimestampFormat::SPACE, &Timestamp)); |
| 84 | } |
| 85 | |
| 86 | TEST_F(TimestampTest, WithSpecifiedFormatAndTimestamp) |
| 87 | { |
| 88 | char aTimestamp[20]; |
| 89 | str_timestamp_ex(time: 1704027535, buffer: aTimestamp, buffer_size: sizeof(aTimestamp), format: TimestampFormat::NOSPACE); |
| 90 | EXPECT_STREQ(aTimestamp, "2023-12-31_12-58-55" ); |
| 91 | |
| 92 | str_timestamp_ex(time: 1330520400, buffer: aTimestamp, buffer_size: sizeof(aTimestamp), format: TimestampFormat::NOSPACE); |
| 93 | EXPECT_STREQ(aTimestamp, "2012-02-29_13-00-00" ); |
| 94 | |
| 95 | str_timestamp_ex(time: 1084644833, buffer: aTimestamp, buffer_size: sizeof(aTimestamp), format: TimestampFormat::SPACE); |
| 96 | EXPECT_STREQ(aTimestamp, "2004-05-15 18:13:53" ); |
| 97 | } |
| 98 | |