| 1 | /* Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #ifdef _WIN32_WINNT |
| 25 | #if (_WIN32_WINNT < 0x0602) |
| 26 | #undef _WIN32_WINNT |
| 27 | // We need at least _WIN32_WINNT_WIN8 i.e. 0x0602 for |
| 28 | // EnumDynamicTimeZoneInformation |
| 29 | #define _WIN32_WINNT 0x0602 |
| 30 | #endif // (_WIN32_WINNT < 0x0602) |
| 31 | #endif // _WIN32_WINNT |
| 32 | |
| 33 | #include <gtest/gtest.h> |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | #include "test_utils.h" |
| 37 | |
| 38 | #include <mysql/components/services/log_shared.h> |
| 39 | #include "../sql/log.h" |
| 40 | |
| 41 | // CET: 32 bytes |
| 42 | // date (10), 'T', time (8), '.', microseconds (6), timezone offset (6) |
| 43 | #define LEN_MS_CET 32 |
| 44 | |
| 45 | // UTC: 27 bytes |
| 46 | // date (10), 'T', time (8), '.', microseconds (6), 'Z' |
| 47 | #define LEN_MS_UTC 27 |
| 48 | |
| 49 | // we use micro-seconds since the epoch |
| 50 | #define MICRO_FAC ((ulonglong)1000000L) |
| 51 | |
| 52 | namespace log_timestamp_unittest { |
| 53 | |
| 54 | using my_testing::Server_initializer; |
| 55 | |
| 56 | class LogTimestampTest : public ::testing::Test { |
| 57 | protected: |
| 58 | void SetUp() override { initializer.SetUp(); } |
| 59 | void TearDown() override { initializer.TearDown(); } |
| 60 | |
| 61 | THD *thd() { return initializer.thd(); } |
| 62 | |
| 63 | Server_initializer initializer; |
| 64 | }; |
| 65 | |
| 66 | /* |
| 67 | Test basic functionality - throttling, eligibility, printing of summary of |
| 68 | Slow_log_throttle. |
| 69 | */ |
| 70 | TEST_F(LogTimestampTest, iso8601) { |
| 71 | char time_buff[iso8601_size]; |
| 72 | #ifdef WIN32 |
| 73 | DYNAMIC_TIME_ZONE_INFORMATION original_dti = {}; |
| 74 | DWORD original_dti_result = GetDynamicTimeZoneInformation(&original_dti); |
| 75 | EXPECT_NE(original_dti_result, TIME_ZONE_ID_INVALID); |
| 76 | |
| 77 | if (original_dti.DaylightDate.wMonth == 0) { |
| 78 | /* |
| 79 | Current system time zone does not support Daylight savings. If the Windows |
| 80 | system time zone has no daylight saving, then attempting to set TZ to a |
| 81 | timezone that does have daylight saving will result in localtime_r |
| 82 | producing inaccurate results. Skipping the test. |
| 83 | Bug#34380460 will be tracking this issue. |
| 84 | */ |
| 85 | GTEST_SKIP() |
| 86 | << "Current system time zone does not support Daylight savings. If the " |
| 87 | "Windows system time zone has no daylight saving, then attempting " |
| 88 | "to set TZ to a timezone that does have daylight saving will result " |
| 89 | "in localtime_r producing inaccurate results. Skipping the test. "; |
| 90 | } |
| 91 | char tz[] = "TZ=CET-1CES"; |
| 92 | #else |
| 93 | char tz[] = "TZ=CET"; |
| 94 | #endif |
| 95 | int time_buff_len; |
| 96 | |
| 97 | EXPECT_EQ(((iso8601_size)-1), LEN_MS_CET); |
| 98 | EXPECT_EQ(((LEN_MS_CET)-5), LEN_MS_UTC); // timezone "Z" instead of "+12:34" |
| 99 | |
| 100 | // set up timezone (central european time) |
| 101 | putenv(tz); |
| 102 | EXPECT_STREQ(&(tz[3]), getenv("TZ")); |
| 103 | tzset(); |
| 104 | |
| 105 | /// 1970/01/01 .000001 (1) |
| 106 | |
| 107 | // UTC (winter) |
| 108 | time_buff_len = make_iso8601_timestamp(time_buff, 1, iso8601_utc); |
| 109 | EXPECT_EQ(LEN_MS_UTC, time_buff_len); |
| 110 | EXPECT_STREQ("1970-01-01T00:00:00.000001Z", time_buff); |
| 111 | |
| 112 | // CET (winter) +1h |
| 113 | time_buff_len = make_iso8601_timestamp(time_buff, 1, iso8601_system_time); |
| 114 | EXPECT_EQ(LEN_MS_CET, time_buff_len); |
| 115 | EXPECT_STREQ("1970-01-01T01:00:00.000001+01:00", time_buff); |
| 116 | |
| 117 | /// 2011-07-07 (1309996800) |
| 118 | |
| 119 | // UTC (summer) |
| 120 | time_buff_len = |
| 121 | make_iso8601_timestamp(time_buff, MICRO_FAC * 1309996800, iso8601_utc); |
| 122 | EXPECT_EQ(LEN_MS_UTC, time_buff_len); |
| 123 | EXPECT_STREQ("2011-07-07T00:00:00.000000Z", time_buff); |
| 124 | |
| 125 | // CET (summer) +2h |
| 126 | time_buff_len = make_iso8601_timestamp(time_buff, MICRO_FAC * 1309996800, |
| 127 | iso8601_system_time); |
| 128 | EXPECT_EQ(LEN_MS_CET, time_buff_len); |
| 129 | EXPECT_STREQ("2011-07-07T02:00:00.000000+02:00", time_buff); |
| 130 | |
| 131 | /// 1987-06-05 04:03:02.123456 |
| 132 | |
| 133 | // UTC |
| 134 | time_buff_len = make_iso8601_timestamp( |
| 135 | time_buff, (MICRO_FAC * 549864182) + 123456, iso8601_utc); |
| 136 | EXPECT_EQ(LEN_MS_UTC, time_buff_len); |
| 137 | EXPECT_STREQ("1987-06-05T04:03:02.123456Z", time_buff); |
| 138 | } |
| 139 | // End of Error_log_timestamp test cases. |
| 140 | |
| 141 | } // namespace log_timestamp_unittest |