Skip to content

Commit 41b2238

Browse files
author
Зишан Мирза
committed
[libc] implement localtime
This is an implementation of localtime. Closes #107597 and #109892
1 parent 29d0a84 commit 41b2238

File tree

19 files changed

+408
-9
lines changed

19 files changed

+408
-9
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ set(TARGET_LIBC_ENTRYPOINTS
205205
libc.src.time.asctime_r
206206
libc.src.time.ctime
207207
libc.src.time.ctime_r
208+
libc.src.time.localtime
209+
libc.src.time.localtime_r
208210
libc.src.time.difftime
209211
libc.src.time.gmtime
210212
libc.src.time.gmtime_r

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ set(TARGET_LIBC_ENTRYPOINTS
201201
libc.src.time.asctime_r
202202
libc.src.time.ctime
203203
libc.src.time.ctime_r
204+
libc.src.time.localtime
205+
libc.src.time.localtime_r
204206
libc.src.time.difftime
205207
libc.src.time.gmtime
206208
libc.src.time.gmtime_r

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,8 @@ if(LLVM_LIBC_FULL_BUILD)
950950
libc.src.time.asctime_r
951951
libc.src.time.ctime
952952
libc.src.time.ctime_r
953+
libc.src.time.localtime
954+
libc.src.time.localtime_r
953955
libc.src.time.clock
954956
libc.src.time.clock_gettime
955957
libc.src.time.difftime

libc/config/linux/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,8 @@ if(LLVM_LIBC_FULL_BUILD)
885885
libc.src.time.asctime_r
886886
libc.src.time.ctime
887887
libc.src.time.ctime_r
888+
libc.src.time.localtime
889+
libc.src.time.localtime_r
888890
libc.src.time.clock
889891
libc.src.time.clock_gettime
890892
libc.src.time.difftime

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,8 @@ if(LLVM_LIBC_FULL_BUILD)
10051005
libc.src.time.asctime_r
10061006
libc.src.time.ctime
10071007
libc.src.time.ctime_r
1008+
libc.src.time.localtime
1009+
libc.src.time.localtime_r
10081010
libc.src.time.clock
10091011
libc.src.time.clock_gettime
10101012
libc.src.time.difftime

libc/newhdrgen/yaml/time.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ functions:
3737
arguments:
3838
- type: const time_t *
3939
- type: char *
40+
- name: localtime
41+
standard:
42+
- stdc
43+
return_type: struct tm *
44+
arguments:
45+
- type: const time_t *
46+
- name: localtime_r
47+
standard:
48+
- stdc
49+
return_type: struct tm *
50+
arguments:
51+
- type: const time_t *
52+
- type: struct tm *
4053
- name: clock
4154
standard:
4255
- stdc

libc/spec/stdc.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,19 @@ def StdC : StandardSpec<"stdc"> {
16151615
ArgSpec<CharPtr>,
16161616
]
16171617
>,
1618+
FunctionSpec<
1619+
"localtime",
1620+
RetValSpec<StructTmPtr>,
1621+
[ArgSpec<TimeTTypePtr>]
1622+
>,
1623+
FunctionSpec<
1624+
"localtime_r",
1625+
RetValSpec<StructTmPtr>,
1626+
[
1627+
ArgSpec<TimeTTypePtr>,
1628+
ArgSpec<StructTmPtr>,
1629+
]
1630+
>,
16181631
FunctionSpec<
16191632
"clock",
16201633
RetValSpec<ClockT>,

libc/src/time/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ add_entrypoint_object(
6060
libc.include.time
6161
)
6262

63+
add_entrypoint_object(
64+
localtime
65+
SRCS
66+
localtime.cpp
67+
HDRS
68+
localtime.h
69+
DEPENDS
70+
.time_utils
71+
libc.hdr.types.time_t
72+
libc.include.time
73+
)
74+
75+
add_entrypoint_object(
76+
localtime_r
77+
SRCS
78+
localtime_r.cpp
79+
HDRS
80+
localtime_r.h
81+
DEPENDS
82+
.time_utils
83+
libc.hdr.types.time_t
84+
libc.include.time
85+
)
86+
6387
add_entrypoint_object(
6488
difftime
6589
SRCS

libc/src/time/localtime.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Implementation of 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 "src/time/localtime.h"
10+
#include "src/__support/CPP/limits.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
#include "src/time/time_utils.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(struct tm *, localtime, (const time_t *t_ptr)) {
18+
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
19+
return nullptr;
20+
}
21+
22+
return time_utils::localtime(t_ptr);
23+
}
24+
25+
} // namespace LIBC_NAMESPACE_DECL

libc/src/time/localtime.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header of 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_H
10+
#define LLVM_LIBC_SRC_TIME_LOCALTIME_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include <time.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
struct tm *localtime(const time_t *t_ptr);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_TIME_LOCALTIME_H

0 commit comments

Comments
 (0)