Skip to content

Commit 4280c13

Browse files
author
Зишан Мирза
committed
[libc] implement localtime
format code with clang-format
1 parent 41b2238 commit 4280c13

File tree

5 files changed

+39
-43
lines changed

5 files changed

+39
-43
lines changed

libc/src/time/localtime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
1616

1717
LLVM_LIBC_FUNCTION(struct tm *, localtime, (const time_t *t_ptr)) {
1818
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
19-
return nullptr;
19+
return nullptr;
2020
}
2121

2222
return time_utils::localtime(t_ptr);

libc/src/time/localtime_r.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616

17-
LLVM_LIBC_FUNCTION(struct tm *, localtime_r, (const time_t *t_ptr, struct tm *tm)) {
17+
LLVM_LIBC_FUNCTION(struct tm *, localtime_r,
18+
(const time_t *t_ptr, struct tm *tm)) {
1819
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
19-
return nullptr;
20+
return nullptr;
2021
}
2122

2223
return time_utils::localtime_internal(t_ptr, tm);

libc/src/time/time_utils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <stdio.h>
109
#include "src/time/time_utils.h"
1110
#include "src/__support/CPP/limits.h" // INT_MIN, INT_MAX
1211
#include "src/__support/common.h"
1312
#include "src/__support/macros/config.h"
13+
#include <stdio.h>
1414

1515
namespace LIBC_NAMESPACE_DECL {
1616
namespace time_utils {
@@ -143,10 +143,10 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
143143

144144
int offset;
145145
if (internal::same_string(timezone, "UTC") == 0) {
146-
offset = 0;
146+
offset = 0;
147147
}
148148
if (internal::same_string(timezone, "Europe/Berlin") == 0) {
149-
offset = 2;
149+
offset = 2;
150150
}
151151

152152
// All the data (years, month and remaining days) was calculated from
@@ -185,7 +185,7 @@ int calculate_dst(struct tm *tm) {
185185
} else if (tm->tm_mon > 3 && tm->tm_mon < 11) {
186186
return 1;
187187
} else if (tm->tm_mon == 3) {
188-
return sunday >= 8;
188+
return sunday >= 8;
189189
}
190190

191191
return sunday <= 0;

libc/src/time/time_utils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
#include <stddef.h> // For size_t.
1313

14+
#include "src/__support/CPP/limits.h"
1415
#include "src/__support/common.h"
1516
#include "src/__support/macros/config.h"
1617
#include "src/errno/libc_errno.h"
1718
#include "src/time/mktime.h"
18-
#include "src/__support/CPP/limits.h"
1919

2020
#include <stdint.h>
2121

@@ -175,8 +175,9 @@ LIBC_INLINE struct tm *localtime(const time_t *t_ptr) {
175175
return &result;
176176
}
177177

178-
LIBC_INLINE struct tm *localtime_internal(const time_t *t_ptr, struct tm *result) {
179-
//time_t time = *t;
178+
LIBC_INLINE struct tm *localtime_internal(const time_t *t_ptr,
179+
struct tm *result) {
180+
// time_t time = *t;
180181
int64_t t = *t_ptr;
181182

182183
// Update the tm structure's year, month, day, etc. from seconds.

libc/test/src/time/localtime_r_test.cpp

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@
1313

1414
TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp0) {
1515
const time_t t_ptr = 1;
16-
static struct tm input = (struct tm) {
17-
.tm_sec = 0,
18-
.tm_min = 0,
19-
.tm_hour = 0,
20-
.tm_mday = 0,
21-
.tm_mon = 0,
22-
.tm_year = 0,
23-
.tm_wday = 0,
24-
.tm_yday = 0,
25-
.tm_isdst = 0
26-
};
16+
static struct tm input = (struct tm){.tm_sec = 0,
17+
.tm_min = 0,
18+
.tm_hour = 0,
19+
.tm_mday = 0,
20+
.tm_mon = 0,
21+
.tm_year = 0,
22+
.tm_wday = 0,
23+
.tm_yday = 0,
24+
.tm_isdst = 0};
2725
struct tm *result = LIBC_NAMESPACE::localtime_r(&t_ptr, &input);
2826
ASSERT_EQ(70, result->tm_year);
2927
ASSERT_EQ(0, result->tm_mon);
@@ -38,17 +36,15 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp0) {
3836

3937
TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32Int) {
4038
time_t t_ptr = 2147483647;
41-
static struct tm input = (struct tm) {
42-
.tm_sec = 0,
43-
.tm_min = 0,
44-
.tm_hour = 0,
45-
.tm_mday = 0,
46-
.tm_mon = 0,
47-
.tm_year = 0,
48-
.tm_wday = 0,
49-
.tm_yday = 0,
50-
.tm_isdst = 0
51-
};
39+
static struct tm input = (struct tm){.tm_sec = 0,
40+
.tm_min = 0,
41+
.tm_hour = 0,
42+
.tm_mday = 0,
43+
.tm_mon = 0,
44+
.tm_year = 0,
45+
.tm_wday = 0,
46+
.tm_yday = 0,
47+
.tm_isdst = 0};
5248
struct tm *result = LIBC_NAMESPACE::localtime_r(&t_ptr, &input);
5349
ASSERT_EQ(138, result->tm_year);
5450
ASSERT_EQ(0, result->tm_mon);
@@ -63,17 +59,15 @@ TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32Int) {
6359

6460
TEST(LlvmLibcLocaltimeR, ValidUnixTimestamp32IntDst) {
6561
time_t t_ptr = 1627225465;
66-
static struct tm input = (struct tm) {
67-
.tm_sec = 0,
68-
.tm_min = 0,
69-
.tm_hour = 0,
70-
.tm_mday = 0,
71-
.tm_mon = 0,
72-
.tm_year = 0,
73-
.tm_wday = 0,
74-
.tm_yday = 0,
75-
.tm_isdst = 0
76-
};
62+
static struct tm input = (struct tm){.tm_sec = 0,
63+
.tm_min = 0,
64+
.tm_hour = 0,
65+
.tm_mday = 0,
66+
.tm_mon = 0,
67+
.tm_year = 0,
68+
.tm_wday = 0,
69+
.tm_yday = 0,
70+
.tm_isdst = 0};
7771
struct tm *result = LIBC_NAMESPACE::localtime_r(&t_ptr, &input);
7872
ASSERT_EQ(121, result->tm_year);
7973
ASSERT_EQ(6, result->tm_mon);

0 commit comments

Comments
 (0)