Skip to content

Commit e97df51

Browse files
author
Зишан Мирза
committed
fix: daylight saving time
1 parent a516da3 commit e97df51

File tree

7 files changed

+21
-29
lines changed

7 files changed

+21
-29
lines changed

libc/src/time/time_utils.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,11 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
140140
} else if (fgets(timezone, sizeof(timezone), fp) == NULL)
141141
return time_utils::out_of_range();
142142

143-
int offset;
143+
// UTC = 0
144+
int offset = 0;
144145
// TODO: Add more timezones
145-
if (internal::same_string(timezone, "UTC") == 0)
146-
offset = 0;
147146
if (internal::same_string(timezone, "Europe/Berlin") == 0)
148-
offset = 2;
147+
offset = 1;
149148

150149
// All the data (years, month and remaining days) was calculated from
151150
// March, 2000. Thus adjust the data to be from January, 1900.
@@ -163,14 +162,11 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
163162
tm->tm_sec =
164163
static_cast<int>(remainingSeconds % TimeConstants::SECONDS_PER_MIN);
165164

166-
if (offset == 0) {
167-
tm->tm_isdst = 0;
168-
} else if (offset > 0) {
169-
tm->tm_isdst = 1;
170-
tm->tm_hour += offset;
171-
} else {
172-
tm->tm_isdst = -1;
165+
set_dst(tm);
166+
if (tm->tm_isdst > 0) {
167+
tm->tm_hour += 1;
173168
}
169+
tm->tm_hour += offset;
174170

175171
fclose(fp);
176172

libc/src/time/time_utils.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ LIBC_INLINE struct tm *localtime(const time_t *t_ptr) {
168168
return nullptr;
169169
}
170170

171-
set_dst(&result);
172-
173171
return &result;
174172
}
175173

@@ -183,8 +181,6 @@ LIBC_INLINE struct tm *localtime_internal(const time_t *t_ptr,
183181
return nullptr;
184182
}
185183

186-
set_dst(input);
187-
188184
return input;
189185
}
190186

libc/test/src/time/ctime_r_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ TEST(LlvmLibcCtimeR, ValidUnixTimestamp0) {
3232
char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
3333
time_t t;
3434
char *result;
35-
// 1970-01-01 00:00:00. Test with a valid buffer size.
35+
// 1970-01-01 01:00:00. Test with a valid buffer size.
3636
t = 0;
3737
result = LIBC_NAMESPACE::ctime_r(&t, buffer);
38-
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
38+
ASSERT_STREQ("Thu Jan 1 01:00:00 1970\n", result);
3939
}
4040

4141
TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
4242
char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
4343
time_t t;
4444
char *result;
45-
// 2038-01-19 03:14:07. Test with a valid buffer size.
45+
// 2038-01-19 04:14:07. Test with a valid buffer size.
4646
t = 2147483647;
4747
result = LIBC_NAMESPACE::ctime_r(&t, buffer);
48-
ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
48+
ASSERT_STREQ("Tue Jan 19 04:14:07 2038\n", result);
4949
}
5050

5151
TEST(LlvmLibcCtimeR, InvalidArgument) {

libc/test/src/time/ctime_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ TEST(LlvmLibcCtime, ValidUnixTimestamp0) {
2222
char *result;
2323
t = 0;
2424
result = LIBC_NAMESPACE::ctime(&t);
25-
ASSERT_STREQ("Thu Jan 1 00:00:00 1970\n", result);
25+
ASSERT_STREQ("Thu Jan 1 01:00:00 1970\n", result);
2626
}
2727

2828
TEST(LlvmLibcCtime, ValidUnixTimestamp32Int) {
2929
time_t t;
3030
char *result;
3131
t = 2147483647;
3232
result = LIBC_NAMESPACE::ctime(&t);
33-
ASSERT_STREQ("Tue Jan 19 03:14:07 2038\n", result);
33+
ASSERT_STREQ("Tue Jan 19 04:14:07 2038\n", result);
3434
}
3535

3636
TEST(LlvmLibcCtime, InvalidArgument) {

libc/test/src/time/localtime_r_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp0) {
2121
ASSERT_EQ(70, input.tm_year);
2222
ASSERT_EQ(0, input.tm_mon);
2323
ASSERT_EQ(1, input.tm_mday);
24-
ASSERT_EQ(2, input.tm_hour);
24+
ASSERT_EQ(1, input.tm_hour);
2525
ASSERT_EQ(0, input.tm_min);
2626
ASSERT_EQ(0, input.tm_sec);
2727
ASSERT_EQ(4, input.tm_wday);
@@ -31,7 +31,7 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp0) {
3131
ASSERT_EQ(70, result->tm_year);
3232
ASSERT_EQ(0, result->tm_mon);
3333
ASSERT_EQ(1, result->tm_mday);
34-
ASSERT_EQ(2, result->tm_hour);
34+
ASSERT_EQ(1, result->tm_hour);
3535
ASSERT_EQ(0, result->tm_min);
3636
ASSERT_EQ(0, result->tm_sec);
3737
ASSERT_EQ(4, result->tm_wday);
@@ -55,7 +55,7 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32Int) {
5555
ASSERT_EQ(138, input.tm_year);
5656
ASSERT_EQ(0, input.tm_mon);
5757
ASSERT_EQ(19, input.tm_mday);
58-
ASSERT_EQ(5, input.tm_hour);
58+
ASSERT_EQ(4, input.tm_hour);
5959
ASSERT_EQ(14, input.tm_min);
6060
ASSERT_EQ(7, input.tm_sec);
6161
ASSERT_EQ(2, input.tm_wday);
@@ -65,7 +65,7 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32Int) {
6565
ASSERT_EQ(138, result->tm_year);
6666
ASSERT_EQ(0, result->tm_mon);
6767
ASSERT_EQ(19, result->tm_mday);
68-
ASSERT_EQ(5, result->tm_hour);
68+
ASSERT_EQ(4, result->tm_hour);
6969
ASSERT_EQ(14, result->tm_min);
7070
ASSERT_EQ(7, result->tm_sec);
7171
ASSERT_EQ(2, result->tm_wday);

libc/test/src/time/localtime_s_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ TEST(LlvmLibcLocaltimeS, ValidUnixTimestamp0) {
3131
ASSERT_EQ(70, input.tm_year);
3232
ASSERT_EQ(0, input.tm_mon);
3333
ASSERT_EQ(1, input.tm_mday);
34-
ASSERT_EQ(2, input.tm_hour);
34+
ASSERT_EQ(1, input.tm_hour);
3535
ASSERT_EQ(0, input.tm_min);
3636
ASSERT_EQ(0, input.tm_sec);
3737
ASSERT_EQ(4, input.tm_wday);
@@ -56,7 +56,7 @@ TEST(LlvmLibcLocaltimeS, ValidUnixTimestamp32Int) {
5656
ASSERT_EQ(138, input.tm_year);
5757
ASSERT_EQ(0, input.tm_mon);
5858
ASSERT_EQ(19, input.tm_mday);
59-
ASSERT_EQ(5, input.tm_hour);
59+
ASSERT_EQ(4, input.tm_hour);
6060
ASSERT_EQ(14, input.tm_min);
6161
ASSERT_EQ(7, input.tm_sec);
6262
ASSERT_EQ(2, input.tm_wday);

libc/test/src/time/localtime_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ TEST(LlvmLibcLocaltime, ValidUnixTimestamp0) {
1717
ASSERT_EQ(70, result->tm_year);
1818
ASSERT_EQ(0, result->tm_mon);
1919
ASSERT_EQ(1, result->tm_mday);
20-
ASSERT_EQ(2, result->tm_hour);
20+
ASSERT_EQ(1, result->tm_hour);
2121
ASSERT_EQ(0, result->tm_min);
2222
ASSERT_EQ(0, result->tm_sec);
2323
ASSERT_EQ(4, result->tm_wday);
@@ -31,7 +31,7 @@ TEST(LlvmLibcLocaltime, ValidUnixTimestamp32Int) {
3131
ASSERT_EQ(138, result->tm_year);
3232
ASSERT_EQ(0, result->tm_mon);
3333
ASSERT_EQ(19, result->tm_mday);
34-
ASSERT_EQ(5, result->tm_hour);
34+
ASSERT_EQ(4, result->tm_hour);
3535
ASSERT_EQ(14, result->tm_min);
3636
ASSERT_EQ(7, result->tm_sec);
3737
ASSERT_EQ(2, result->tm_wday);

0 commit comments

Comments
 (0)