1#include <gtest/gtest.h>
2
3#include <base/system.h>
4#include <cstdlib>
5
6class TimestampTest : public testing::Test
7{
8protected:
9 void SetUp() override
10 {
11#if defined(CONF_FAMILY_WINDOWS)
12 _putenv_s("TZ", "UTC");
13 _tzset();
14#else
15 setenv(name: "TZ", value: "UTC", replace: 1);
16 tzset();
17#endif
18 }
19};
20
21TEST_F(TimestampTest, FromStr)
22{
23 time_t Timestamp;
24 EXPECT_TRUE(timestamp_from_str("2023-12-31_12-58-55", FORMAT_NOSPACE, &Timestamp));
25 EXPECT_EQ(Timestamp, 1704027535);
26
27 EXPECT_TRUE(timestamp_from_str("2012-02-29_13-00-00", FORMAT_NOSPACE, &Timestamp));
28 EXPECT_EQ(Timestamp, 1330520400);
29
30 EXPECT_TRUE(timestamp_from_str("2004-05-15 18:13:53", FORMAT_SPACE, &Timestamp));
31 EXPECT_EQ(Timestamp, 1084644833);
32}
33
34TEST_F(TimestampTest, FromStrFailing)
35{
36 time_t Timestamp;
37 // Invalid time string
38 EXPECT_FALSE(timestamp_from_str("123 2023-12-31_12-58-55", FORMAT_NOSPACE, &Timestamp));
39
40 // Invalid time string
41 EXPECT_FALSE(timestamp_from_str("555-02-29_13-12-7", FORMAT_NOSPACE, &Timestamp));
42
43 // Time string does not fit the format
44 EXPECT_FALSE(timestamp_from_str("2004-05-15 18-13-53", FORMAT_SPACE, &Timestamp));
45
46 // Invalid time string
47 EXPECT_FALSE(timestamp_from_str("2000-01-01 00:00:00:00", FORMAT_SPACE, &Timestamp));
48}
49
50TEST_F(TimestampTest, WithSpecifiedFormatAndTimestamp)
51{
52 char aTimestamp[20];
53 str_timestamp_ex(time: 1704027535, buffer: aTimestamp, buffer_size: sizeof(aTimestamp), FORMAT_NOSPACE);
54 EXPECT_STREQ(aTimestamp, "2023-12-31_12-58-55");
55
56 str_timestamp_ex(time: 1330520400, buffer: aTimestamp, buffer_size: sizeof(aTimestamp), FORMAT_NOSPACE);
57 EXPECT_STREQ(aTimestamp, "2012-02-29_13-00-00");
58
59 str_timestamp_ex(time: 1084644833, buffer: aTimestamp, buffer_size: sizeof(aTimestamp), FORMAT_SPACE);
60 EXPECT_STREQ(aTimestamp, "2004-05-15 18:13:53");
61}
62