Skip to content

Commit 3a546c1

Browse files
author
Zishan Mirza
committed
refactor: move linux specific implementation to libc/src/time/linux/
1 parent c06d335 commit 3a546c1

File tree

12 files changed

+145
-138
lines changed

12 files changed

+145
-138
lines changed

libc/src/time/CMakeLists.txt

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ add_object_library(
2929
libc.hdr.types.struct_tm
3030
)
3131

32-
add_object_library(
33-
timezone
34-
SRCS
35-
timezone.cpp
36-
HDRS
37-
timezone.h
38-
DEPENDS
39-
libc.include.time
40-
libc.src.__support.CPP.limits
41-
libc.src.errno.errno
42-
)
43-
4432
add_entrypoint_object(
4533
asctime
4634
SRCS
@@ -73,10 +61,9 @@ add_entrypoint_object(
7361
ctime.cpp
7462
HDRS
7563
ctime.h
76-
timezone.h
64+
time_utils.h
7765
DEPENDS
7866
.time_utils
79-
.time_constants
8067
.timezone
8168
libc.hdr.types.time_t
8269
libc.include.time
@@ -87,12 +74,13 @@ add_entrypoint_object(
8774
ctime_r
8875
SRCS
8976
ctime_r.cpp
90-
timezone.h
9177
HDRS
9278
ctime_r.h
79+
time_utils.h
9380
DEPENDS
81+
.linux.localtime_utils
82+
.linux.timezone
9483
.time_utils
95-
.time_constants
9684
.timezone
9785
libc.hdr.types.time_t
9886
libc.include.time
@@ -105,12 +93,10 @@ add_entrypoint_object(
10593
linux/localtime.cpp
10694
HDRS
10795
localtime.h
108-
timezone.h
10996
DEPENDS
97+
.linux.localtime_utils
98+
.linux.timezone
11099
.time_utils
111-
.timezone
112-
libc.hdr.types.time_t
113-
libc.include.time
114100
libc.src.stdio.fopen
115101
)
116102

@@ -120,12 +106,10 @@ add_entrypoint_object(
120106
linux/localtime_r.cpp
121107
HDRS
122108
localtime_r.h
123-
timezone.h
124109
DEPENDS
110+
.linux.localtime_utils
111+
.linux.timezone
125112
.time_utils
126-
.timezone
127-
libc.hdr.types.time_t
128-
libc.include.time
129113
libc.src.stdio.fopen
130114
)
131115

@@ -135,12 +119,10 @@ add_entrypoint_object(
135119
linux/localtime_s.cpp
136120
HDRS
137121
localtime_s.h
138-
timezone.h
139122
DEPENDS
123+
.linux.localtime_utils
124+
.linux.timezone
140125
.time_utils
141-
.timezone
142-
libc.hdr.types.time_t
143-
libc.include.time
144126
libc.src.stdio.fopen
145127
)
146128

@@ -162,7 +144,7 @@ add_entrypoint_object(
162144
HDRS
163145
gmtime.h
164146
DEPENDS
165-
.timezone
147+
# .timezone
166148
.time_utils
167149
libc.include.time
168150
libc.hdr.types.time_t
@@ -176,7 +158,7 @@ add_entrypoint_object(
176158
HDRS
177159
gmtime_r.h
178160
DEPENDS
179-
.timezone
161+
# .timezone
180162
.time_utils
181163
libc.include.time
182164
libc.hdr.types.time_t
@@ -190,7 +172,7 @@ add_entrypoint_object(
190172
HDRS
191173
mktime.h
192174
DEPENDS
193-
.timezone
175+
# .timezone
194176
.time_utils
195177
.time_constants
196178
libc.include.time

libc/src/time/linux/CMakeLists.txt

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,26 @@ add_entrypoint_object(
6767
libc.src.errno.errno
6868
)
6969

70-
add_entrypoint_object(
71-
localtime
70+
add_object_library(
71+
localtime_utils
7272
SRCS
73-
localtime.cpp
73+
localtime_utils.cpp
7474
HDRS
75-
../localtime.h
76-
../timezone.h
75+
localtime_utils.h
7776
DEPENDS
78-
libc.hdr.types.time_t
79-
libc.include.time
80-
)
81-
82-
add_entrypoint_object(
83-
localtime_r
84-
SRCS
85-
localtime_r.cpp
86-
HDRS
87-
../localtime_r.h
88-
../timezone.h
89-
DEPENDS
90-
libc.hdr.types.time_t
9177
libc.include.time
78+
libc.src.__support.CPP.limits
79+
libc.src.errno.errno
9280
)
9381

94-
add_entrypoint_object(
95-
localtime_s
82+
add_object_library(
83+
timezone
9684
SRCS
97-
localtime_s.cpp
85+
timezone.cpp
9886
HDRS
99-
../localtime_s.h
100-
../timezone.h
87+
timezone.h
10188
DEPENDS
102-
libc.hdr.types.time_t
10389
libc.include.time
90+
libc.src.__support.CPP.limits
91+
libc.src.errno.errno
10492
)

libc/src/time/linux/localtime.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/time/linux/localtime_utils.h"
910
#include "src/time/localtime.h"
1011
#include "src/time/time_utils.h"
1112

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===-- Linux implementation of the localtime function --------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "localtime_utils.h"
10+
#include "src/time/linux/timezone.h"
11+
#include "src/time/time_utils.h"
12+
13+
namespace LIBC_NAMESPACE_DECL {
14+
namespace localtime_utils {
15+
16+
timezone::tzset *get_localtime(struct tm *tm) {
17+
char *tz_filename = time_utils::get_env_var("TZ");
18+
if ((tz_filename == nullptr) == 1 || tz_filename[0] == '\0') {
19+
static char localtime[] = "/etc/localtime";
20+
tz_filename = localtime;
21+
} else {
22+
char tmp[64];
23+
char prefix[21] = "/usr/share/zoneinfo/";
24+
size_t i = 0;
25+
while (prefix[i] != '\0') {
26+
tmp[i] = prefix[i];
27+
i++;
28+
}
29+
30+
i = 0;
31+
while (tz_filename[i] != '\0') {
32+
tmp[i + 20] = tz_filename[i];
33+
i++;
34+
}
35+
36+
tz_filename = tmp;
37+
while (tz_filename[i] != '\0') {
38+
if (tz_filename[i] == (char)0xFFFFFFAA) {
39+
tz_filename[i] = '\0';
40+
}
41+
i++;
42+
}
43+
}
44+
45+
ErrorOr<File *> error_or_file = time_utils::acquire_file(tz_filename);
46+
File *file = error_or_file.value();
47+
48+
timezone::tzset *ptr_tzset = timezone::get_tzset(file);
49+
if (ptr_tzset == nullptr) {
50+
time_utils::release_file(file);
51+
return nullptr;
52+
}
53+
54+
for (size_t i = 0; i < *ptr_tzset->ttinfo->size; i++) {
55+
if (time_utils::is_dst(tm) == ptr_tzset->ttinfo[i].tt_isdst) {
56+
ptr_tzset->global_offset =
57+
static_cast<int8_t>(ptr_tzset->ttinfo[i].tt_utoff / 3600);
58+
ptr_tzset->global_isdst =
59+
static_cast<int8_t>(ptr_tzset->ttinfo[i].tt_isdst);
60+
}
61+
}
62+
63+
if (time_utils::file_usage == 1) {
64+
time_utils::release_file(file);
65+
}
66+
67+
return ptr_tzset;
68+
}
69+
70+
} // namespace localtime_utils
71+
} // namespace LIBC_NAMESPACE_DECL
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Collection of utils for localtime - -------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_TIME_LOCALTIME_UTILS_H
10+
#define LLVM_LIBC_SRC_TIME_LOCALTIME_UTILS_H
11+
12+
#include "hdr/types/time_t.h"
13+
#include "src/__support/CPP/limits.h"
14+
#include "src/errno/libc_errno.h"
15+
#include "src/time/linux/timezone.h"
16+
#include <time.h>
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
namespace localtime_utils {
20+
21+
extern timezone::tzset *get_localtime(struct tm *tm);
22+
23+
} // namespace localtime_utils
24+
} // namespace LIBC_NAMESPACE_DECL
25+
26+
#endif // LLVM_LIBC_SRC_TIME_LINUX_LOCALTIME_UTILS_H

libc/src/time/timezone.cpp renamed to libc/src/time/linux/timezone.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "src/__support/common.h"
1414
#include "src/time/time_utils.h"
15-
#include "src/time/timezone.h"
15+
#include "src/time/linux/timezone.h"
1616

1717
namespace LIBC_NAMESPACE_DECL {
1818
namespace timezone {

libc/src/time/timezone.h renamed to libc/src/time/linux/timezone.h

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

9-
#ifndef LLVM_LIBC_SRC_TIME_TIMEZONE_H
10-
#define LLVM_LIBC_SRC_TIME_TIMEZONE_H
9+
#ifndef LLVM_LIBC_SRC_TIME_LINUX_TIMEZONE_H
10+
#define LLVM_LIBC_SRC_TIME_LINUX_TIMEZONE_H
1111

1212
#include "src/__support/File/file.h"
1313
#include "src/__support/common.h"
@@ -54,4 +54,4 @@ tzset *get_tzset(File *file);
5454
} // namespace timezone
5555
} // namespace LIBC_NAMESPACE_DECL
5656

57-
#endif // LLVM_LIBC_SRC_TIME_TIMEZONE_H
57+
#endif // LLVM_LIBC_SRC_TIME_LINUX_TIMEZONE_H

libc/src/time/time_utils.cpp

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ static int64_t computeRemainingYears(int64_t daysPerYears,
2323
return years;
2424
}
2525

26-
volatile int file_usage = 0;
27-
2826
void release_file(ErrorOr<File *> error_or_file) {
2927
file_usage = 0;
3028
error_or_file.value()->close();
@@ -196,60 +194,6 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
196194
return 0;
197195
}
198196

199-
timezone::tzset *get_localtime(struct tm *tm) {
200-
char *tz_filename = get_env_var("TZ");
201-
if ((tz_filename == nullptr) == 1 || tz_filename[0] == '\0') {
202-
static char localtime[] = "/etc/localtime";
203-
tz_filename = localtime;
204-
} else {
205-
char tmp[64];
206-
char prefix[21] = "/usr/share/zoneinfo/";
207-
size_t i = 0;
208-
while (prefix[i] != '\0') {
209-
tmp[i] = prefix[i];
210-
i++;
211-
}
212-
213-
i = 0;
214-
while (tz_filename[i] != '\0') {
215-
tmp[i + 20] = tz_filename[i];
216-
i++;
217-
}
218-
219-
tz_filename = tmp;
220-
while (tz_filename[i] != '\0') {
221-
if (tz_filename[i] == (char)0xFFFFFFAA) {
222-
tz_filename[i] = '\0';
223-
}
224-
i++;
225-
}
226-
}
227-
228-
ErrorOr<File *> error_or_file = acquire_file(tz_filename);
229-
File *file = error_or_file.value();
230-
231-
timezone::tzset *ptr_tzset = timezone::get_tzset(file);
232-
if (ptr_tzset == nullptr) {
233-
release_file(file);
234-
return nullptr;
235-
}
236-
237-
for (size_t i = 0; i < *ptr_tzset->ttinfo->size; i++) {
238-
if (is_dst(tm) == ptr_tzset->ttinfo[i].tt_isdst) {
239-
ptr_tzset->global_offset =
240-
static_cast<int8_t>(ptr_tzset->ttinfo[i].tt_utoff / 3600);
241-
ptr_tzset->global_isdst =
242-
static_cast<int8_t>(ptr_tzset->ttinfo[i].tt_isdst);
243-
}
244-
}
245-
246-
if (file_usage == 1) {
247-
release_file(file);
248-
}
249-
250-
return ptr_tzset;
251-
}
252-
253197
unsigned char is_dst(struct tm *tm) {
254198
unsigned int dst;
255199

0 commit comments

Comments
 (0)