Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
226 commits
Select commit Hold shift + click to select a range
a4d501c
[libc] implement localtime
Sep 28, 2024
221d2bc
[libc] implement localtime
Sep 28, 2024
90ddc6d
[libc] implement localtime
Sep 28, 2024
9dd0690
[libc] implement localtime
Sep 28, 2024
e88e589
[libc] implement localtime
Sep 28, 2024
0ca877f
[libc] implement localtime
Sep 28, 2024
7ebb945
[libc] implement localtime
Sep 28, 2024
ce7edf9
localtime_s is for windows only, implement localtime_s on gnu/linux for
Jan 16, 2025
2082bee
fix: localtime_s
Oct 6, 2024
385bed6
fix: path for mktime
Oct 6, 2024
b0efb23
fix: localtime_s and dst function
Oct 6, 2024
95752bf
added tests for localtime_r
Oct 6, 2024
5950145
format code with clang-format
Oct 6, 2024
6bfb76e
refactor: timezone
Oct 6, 2024
6e3614d
removing mktime as dependency to localtime_s
Oct 6, 2024
ee28f97
revert ctime tests
Oct 6, 2024
8fbb435
revert ctime_r tests
Oct 6, 2024
cce348e
fix: daylight saving time
Oct 6, 2024
39c375b
update documentation for libc
Oct 6, 2024
04fe862
refactor: file read
Oct 6, 2024
8050b80
fix: file read
Oct 6, 2024
4b95085
fix: timezone char length
zimirza Feb 12, 2025
30b94e1
timezone implementation
Oct 8, 2024
b858ed3
format code with clang-format
Oct 8, 2024
a771ae1
use stack allocation of memory instead of heap allocation
Oct 8, 2024
8709b2a
remove `free` function because stack allocation of memory is now used
Oct 8, 2024
b617cae
added more timezones
Oct 8, 2024
299317c
added more timezones
Oct 8, 2024
2fd327d
refactor to parse `/etc/localtime`
Nov 23, 2024
8d26882
parse `/etc/localtime` implementation
Nov 23, 2024
072f090
add support for `tzfile` v3
Nov 23, 2024
c4225a6
add `close` function
Nov 23, 2024
22d5647
add support for `tzfile` version 4
Nov 23, 2024
89e605e
fix: bytes for timezone file
Nov 23, 2024
5129e61
fix: merge from main branch
Dec 23, 2024
40e2e8f
define structs for timezone data
Dec 23, 2024
8107ae2
use pointers in struct
Dec 23, 2024
f4d2a49
refactor: get timezone data from tzfile
Dec 23, 2024
f4c22bf
refactor: rename function since it returns tzset struct
Dec 23, 2024
8e2ec92
update struct fields with additional fields and parse timezone data
Dec 24, 2024
81650dd
remove last char in timezone text
Dec 24, 2024
6b576c7
use `open` instead of `fopen`
Dec 24, 2024
e3d5950
use `static` and add number of transitions times for timezone
Dec 24, 2024
f664276
format code with clang-format
Dec 24, 2024
71153b9
refactor: parse of tzset file
Dec 25, 2024
90a1611
refactor: parse of offsets
Dec 25, 2024
da91687
refactor: parse of offsets
Dec 26, 2024
19f756b
use timezone implementation for localtime function implementation
Dec 26, 2024
03e7627
format code with clang-format
Dec 26, 2024
7ce31da
add header file for time_utils in asctime_test
Dec 26, 2024
7c34853
fix function name for asctime
Dec 26, 2024
6e8220c
fix: add `get_tzset` function to time_utils header file
Dec 26, 2024
858f8c4
fix: add timezone header file to time_utils header file
Dec 26, 2024
19a94e5
fix: tzet function in time_utils header file
Dec 26, 2024
003c050
fix: add timezone header file to tests
Dec 26, 2024
1f90338
fix: add header file for size_t
Dec 26, 2024
3556f4f
add todo in tests
Dec 26, 2024
b7201cb
add timezone to dependencies for tests
Dec 26, 2024
1549104
fix path for timezone header file for tests
Dec 26, 2024
9a12c24
format code with clang-format
Dec 26, 2024
69d1b90
fix dependencies for tests
Dec 26, 2024
c53dab0
add header file for size_t
Dec 26, 2024
9d65255
add timezone header file to gmtime tests
Dec 26, 2024
074e31e
format code with clang-format
Dec 26, 2024
09aba03
add timezone to gmtime tests
Dec 26, 2024
ecdab14
format code with clang-format
Dec 26, 2024
fc98df6
add timezone to mktime
Dec 26, 2024
2f1ea3d
add timezone to gmtime
Dec 26, 2024
aab0bd5
add timezone to mktime
Dec 26, 2024
a0edf88
use `static_cast`
Dec 26, 2024
309746c
fix tests for timezone
Dec 26, 2024
aae094b
refactor: move definitions to scope
Dec 26, 2024
76d0784
format code with clang-format
Dec 26, 2024
4df6eac
fix: dependencies for timezone for tests
Dec 26, 2024
43301b3
fix: tests
Dec 26, 2024
3011ffa
refactor: fix tests
Dec 26, 2024
776afe6
refactor: localtime
Jan 1, 2025
d730a92
uncomment tests for asctime
Jan 1, 2025
f896a65
undo asctime
Jan 1, 2025
891c06b
format code with clang-format
Jan 1, 2025
a35ed74
undo asctime
Jan 1, 2025
5bfde5d
fix: ctime_r
Jan 1, 2025
5bb0514
refactor: use `openfile`
Jan 1, 2025
99af1e7
fix: add header file for time_utils in localtime
Jan 1, 2025
d8126c1
fix localtime
Jan 2, 2025
083389c
fix: fixed tests for asctime and removed unnecessary header include
Jan 3, 2025
3434b04
refactor: move linux specific implementation to libc/src/time/linux/
Jan 4, 2025
2bd53a8
format code with clang-format
Jan 4, 2025
5b0e39d
fix: tests
Jan 4, 2025
9ead91c
fix: tests
Jan 4, 2025
ee617ad
fix: add if macro for linux implementations
Jan 4, 2025
abfc697
fix: check if timer variable is nullptr
Jan 4, 2025
7a36e3e
fix: return type for localtime_s implementation
Jan 4, 2025
eeafc70
fix: tests for difftime
Jan 4, 2025
514c153
use LIBC_TARGET_OS for platform independent implementations
Jan 4, 2025
6cb524b
fix tests for clock
Jan 5, 2025
e6d5010
add localtime
Jan 5, 2025
ba6d3a8
remove implementation for a different pull request
Jan 16, 2025
8986bce
remove implementation for a different pull request
Jan 16, 2025
35e1e7f
remove unit tests for a different pull request
Jan 16, 2025
106a389
uncheck ctime and localtime in docs
Jan 16, 2025
eed8762
remove entrypoints
Jan 16, 2025
057de25
fix merge and tests
Jan 17, 2025
a184e9c
format code with clang-format
Jan 17, 2025
1a0efb6
fix merge and time
Jan 17, 2025
4355e2f
add fopen to mktime
Jan 17, 2025
6632dae
comment out offset and isdst
Jan 17, 2025
7905d54
update time
Jan 17, 2025
f25d380
uncomment get env var function and fix tests
Jan 17, 2025
adc641e
add unistd.h to tests
Jan 17, 2025
bb3e789
removed time_t.h from localtime_utils
Jan 17, 2025
f1e95fa
fix: time
Jan 19, 2025
9293534
tmp
Jan 19, 2025
88ff1a4
environment variables is only tested on gnu/linux
Jan 19, 2025
077611a
fix: environment variables
Jan 19, 2025
8479729
remove system header file for time.h
zimirza Jan 24, 2025
6c64877
remove local time.h header file and add system time.h header file
zimirza Jan 24, 2025
734a687
use system header files for integer types
zimirza Jan 24, 2025
9f84b54
undo cmake file for tests
zimirza Feb 1, 2025
425a9d9
undo header include for tests
zimirza Feb 1, 2025
ec4b06d
undo header include and fixed ctime header include
zimirza Feb 1, 2025
e44db21
undo
zimirza Feb 1, 2025
ee72a1c
undo
zimirza Feb 1, 2025
c5ddcda
fixed header include for ctime_r tests
zimirza Feb 1, 2025
55467aa
mktime requires fopen
zimirza Feb 1, 2025
ad11374
add file dependency
zimirza Feb 12, 2025
617ae23
fix: merge from main branch
zimirza Feb 12, 2025
32e5c79
remove file dependency from ctime_r
zimirza Feb 12, 2025
d508cc3
removed localtime_s for this pull request
zimirza Apr 22, 2025
e4133b7
remove localtime_s for this pull request
zimirza Apr 22, 2025
ce3b89d
removed localtime_s for this pull request
zimirza Apr 22, 2025
8a32b66
removed localtime_s for this pull request
zimirza Apr 22, 2025
481c0a7
moved linux specific code
zimirza Apr 22, 2025
03d2bea
removed localtime_utc function and fixed localtime function
zimirza Apr 22, 2025
986472f
use `getenv` implementation of using `unistd.h`
zimirza Apr 29, 2025
027763c
fix: function signature for localtime
zimirza Apr 29, 2025
5b991a7
revert: function signature for `update_for_seconds`
zimirza Apr 29, 2025
99b81c6
format code with clang-format
zimirza Apr 29, 2025
5900f59
remove `fopen`
zimirza Apr 29, 2025
588c527
fix: environment pointer
zimirza Apr 29, 2025
47fd94a
fix: environment pointer
zimirza Apr 29, 2025
79df4ac
format code with clang-format
zimirza Apr 29, 2025
6f6418b
remove file.h from timezone
zimirza Apr 29, 2025
155fd73
use FILE
zimirza Apr 29, 2025
594b245
include file
zimirza Apr 30, 2025
ef93900
format code with clang-format
zimirza Apr 30, 2025
c820c04
revert ctime_r_test and remove dst function for another pull request
zimirza Apr 30, 2025
e644279
remove constant for a different pull request
zimirza Apr 30, 2025
9b16b05
revert ctime and remove timezone implementation
zimirza Apr 30, 2025
78bc296
add `fopen` to `ctime_r`
zimirza Apr 30, 2025
67e52dc
revert: ctime
zimirza Apr 30, 2025
b5cbc48
revert ctime unit tests
zimirza Apr 30, 2025
47510cd
revert: ctime unit tests
zimirza Apr 30, 2025
53d7a2a
revert: ctime
zimirza Apr 30, 2025
3386bd5
revert: update_from_seconds
zimirza Apr 30, 2025
c105779
revert: update_from_seconds
zimirza Apr 30, 2025
94e69fa
rerun github actions
zimirza Apr 30, 2025
0d2d560
remove localtime_macros.h
zimirza May 2, 2025
1728f2e
function `get_localtime` should not be `extern`
zimirza May 2, 2025
9e6e9b3
remove `get_env_var` function from this pull request
zimirza May 2, 2025
86a5e91
use c++-style references
zimirza May 2, 2025
a5fa5a2
format code with clang-format
zimirza May 2, 2025
fb95bcc
use c++ style references
zimirza May 5, 2025
7a70610
removed timezone implementation definitions
zimirza May 6, 2025
e391fe8
removed app.h from time_utils.cpp
zimirza May 6, 2025
db9e29c
fix: code format
zimirza May 6, 2025
1fe5249
use localtime_internal instead of gmtime_internal
zimirza May 8, 2025
bf0c2e3
remove comment
zimirza May 8, 2025
a6a15cc
add struct for tzset
zimirza May 8, 2025
389711f
fix: pointers
zimirza May 8, 2025
e2bba73
removed invalid value from localtime_internal function
zimirza May 8, 2025
73591cb
rename and add todo
zimirza May 8, 2025
6d072c6
add message to stderr for timezone database implementation
zimirza May 8, 2025
17bcf44
add localtime and localtime_r to x86_64 entrypoints
zimirza May 9, 2025
b483ac7
removed unused variable
zimirza May 9, 2025
207b691
remove localtime_utils and timezone from this patch
zimirza May 9, 2025
0fe6296
update dependencies for tests
zimirza May 9, 2025
6159ed0
remove c++ version dependency
zimirza May 9, 2025
95f174e
add localtime and localtime_r to entrypoint objects for linux
zimirza May 9, 2025
3861d2d
update comment
zimirza May 9, 2025
b4d011c
removed dependencies for baremetal implementation of localtime and added
zimirza May 9, 2025
f3ddce0
add localtime and localtime_r to aarch64 baremetal entrypoints
zimirza May 9, 2025
b6334f4
remove fortify source and fortify level options
zimirza May 9, 2025
a99e1f4
fix: dependencies for localtime baremetal implementation
zimirza May 9, 2025
ce8c367
fix: localtime implementation for baremetal
zimirza May 9, 2025
3717dea
add implementation for localtime_r for baremetal
zimirza May 9, 2025
8a6caec
remove extern inlines from localtime overlay
zimirza May 9, 2025
9221ca3
remove extern inline from localtime overlay
zimirza May 9, 2025
8c4a713
add extern inlines and add localtime_r to localtime overlay
zimirza May 9, 2025
895895e
fix: dependencies for localtime and localtime_r
zimirza May 9, 2025
1282352
remove stderr message
zimirza May 9, 2025
17ac718
remove TmHelper.h from localtime and localtime_r tests
zimirza May 9, 2025
17be9c7
move localtime.cpp and localtime_r.cpp to time/ instead of time/linux/
zimirza Jun 26, 2025
ed0a917
update CMakeFiles.txt for linux
zimirza Jun 26, 2025
d18638f
update CMakeFiles.txt for time
zimirza Jun 26, 2025
2815947
update tests for localtime and localtime_r
zimirza Jun 26, 2025
7a54ff7
update localtime and localtime_r
zimirza Jun 26, 2025
2a03937
format code with clang-format
zimirza Jun 26, 2025
8399219
include common.h first
zimirza Jun 26, 2025
e9ad746
include common.h first
zimirza Jun 26, 2025
8054387
move including of common.h to localtime.h and localtime_r.h
zimirza Jun 26, 2025
6883736
rename variables
zimirza Aug 29, 2025
9ce9b13
use `LIBC_CRASH_ON_NULLPTR` instead of returning `nullptr`
zimirza Aug 30, 2025
00bff09
add headers for time_utils.h
zimirza Aug 30, 2025
ec6a05c
add includes for struct_tm and time_t
zimirza Aug 30, 2025
8443717
format code with clang-format
zimirza Aug 30, 2025
3630c56
update CMakeLists file for tests for localtime
zimirza Aug 30, 2025
a8f7457
add tests for localtime and localtime_r
zimirza Aug 30, 2025
aafcf1d
use `LIBC_CRASH_ON_NULLPTR` for localtime and localtime_r
zimirza Aug 30, 2025
77d29e2
format code with clang-format
zimirza Aug 30, 2025
e26d5fe
add `LIBC_CRASH_ON_NULLPTR` for struct
zimirza Aug 31, 2025
2ad113d
fix tests for localtime_r
zimirza Aug 31, 2025
9f68eea
format code with clang-format
zimirza Aug 31, 2025
c05de05
add tests for nullptr
zimirza Aug 31, 2025
efa0372
format code with clang-format
zimirza Aug 31, 2025
2e11cb4
add localtime and localtime_r to docs
zimirza Aug 31, 2025
bbc3bee
add includes to `struct tm` and `time_t` for localtime and localtime_r
zimirza Aug 31, 2025
5ae0fab
fix: rebase from main branch for time.rst file
zimirza Aug 31, 2025
da8d1ff
fix: rebase from main branch - add newline char in time.rst file
zimirza Aug 31, 2025
edfee68
Revert "fix: rebase from main branch - add newline char in time.rst f…
zimirza Aug 31, 2025
9d10136
fix: rebase from main branch
zimirza Aug 31, 2025
6592c1f
fix: rebase from main branch
zimirza Aug 31, 2025
6cc6dff
fix: rebase from main branch
zimirza Aug 31, 2025
1d3071f
use `WITH_SIGNAL` in `nullptr` tests for localtime and localtime_r
zimirza Aug 31, 2025
edc30fc
format code with clang-format
zimirza Aug 31, 2025
dd7e206
add time_utils as dependency to localtime and localtime_r
zimirza Sep 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libc/config/baremetal/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.time.difftime
libc.src.time.gmtime
libc.src.time.gmtime_r
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
libc.src.time.strftime
libc.src.time.strftime_l
Expand Down
2 changes: 2 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,8 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.gettimeofday
libc.src.time.gmtime
libc.src.time.gmtime_r
libc.src.time.localtime
libc.src.time.localtime_r
libc.src.time.mktime
libc.src.time.nanosleep
libc.src.time.strftime
Expand Down
6 changes: 3 additions & 3 deletions libc/docs/headers/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ Implementation Status
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| gmtime_r | |check| | |check| | | |check| | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| localtime | | | | | | | | | | | | | |
| localtime | |check| | |check| | | |check| | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| localtime_r | | | | | | | | | | | | | |
| localtime_r | |check| | |check| | | |check| | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| mktime | |check| | |check| | | |check| | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
Expand All @@ -112,4 +112,4 @@ Implementation Status
| timer_settime | | | | | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| tzset | | | | | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
45 changes: 45 additions & 0 deletions libc/hdr/localtime_overlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===-- Including localtime.h in overlay mode -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_LOCALTIME_OVERLAY_H
#define LLVM_LIBC_HDR_LOCALTIME_OVERLAY_H

#ifdef LIBC_FULL_BUILD
#error "This header should only be included in overlay mode"
#endif

// Overlay mode

// glibc <unistd.h> header might provide extern inline definitions for few
// functions, causing external alias errors. They are guarded by
// `__USE_EXTERN_INLINES` macro.

#ifdef __USE_EXTERN_INLINES
#define LIBC_OLD_USE_EXTERN_INLINES
#undef __USE_EXTERN_INLINES
#endif

#ifndef __NO_INLINE__
#define __NO_INLINE__ 1
#define LIBC_SET_NO_INLINE
#endif

#include <localtime.h>
#include <localtime_r.h>

#ifdef LIBC_SET_NO_INLINE
#undef __NO_INLINE__
#undef LIBC_SET_NO_INLINE
#endif

#ifdef LIBC_OLD_USE_EXTERN_INLINES
#define __USE_EXTERN_INLINES
#undef LIBC_OLD_USE_EXTERN_INLINES
#endif

#endif // LLVM_LIBC_HDR_LOCALTIME_OVERLAY_H
13 changes: 13 additions & 0 deletions libc/include/time.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ functions:
arguments:
- type: const time_t *
- type: char *
- name: localtime
standard:
- stdc
return_type: struct tm *
arguments:
- type: const time_t *
- name: localtime_r
standard:
- stdc
return_type: struct tm *
arguments:
- type: const time_t *
- type: struct tm *
- name: clock
standard:
- stdc
Expand Down
24 changes: 24 additions & 0 deletions libc/src/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ add_entrypoint_object(
libc.include.time
)

add_entrypoint_object(
localtime
SRCS
localtime.cpp
HDRS
localtime.h
DEPENDS
.time_utils
libc.hdr.types.time_t
libc.hdr.types.struct_tm
)

add_entrypoint_object(
localtime_r
SRCS
localtime_r.cpp
HDRS
localtime_r.h
DEPENDS
.time_utils
libc.hdr.types.time_t
libc.hdr.types.struct_tm
)

add_entrypoint_object(
difftime
SRCS
Expand Down
26 changes: 26 additions & 0 deletions libc/src/time/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,29 @@ add_entrypoint_object(
libc.hdr.time_macros
libc.hdr.types.struct_timespec
)

add_entrypoint_object(
localtime
SRCS
localtime.cpp
HDRS
../localtime.h
time_utils.h
DEPENDS
.time_utils
libc.hdr.types.struct_tm
libc.hdr.types.time_t
)

add_entrypoint_object(
localtime_r
SRCS
localtime_r.cpp
HDRS
../localtime.h
time_utils.h
DEPENDS
.time_utils
libc.hdr.types.struct_tm
libc.hdr.types.time_t
)
22 changes: 22 additions & 0 deletions libc/src/time/baremetal/localtime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation of localtime for baremetal -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/time/localtime.h"
#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(struct tm *, localtime, (time_t *timer)) {
static struct tm tm_out;

return time_utils::localtime_internal(timer, &tm_out);
}

} // namespace LIBC_NAMESPACE_DECL
24 changes: 24 additions & 0 deletions libc/src/time/baremetal/localtime_r.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation of localtime_r for baremetal -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/time/localtime_r.h"
#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/macros/null_check.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(struct tm *, localtime_r,
(const time_t *timer, struct tm *buf)) {
LIBC_CRASH_ON_NULLPTR(timer);

return time_utils::localtime_internal(timer, buf);
}

} // namespace LIBC_NAMESPACE_DECL
24 changes: 24 additions & 0 deletions libc/src/time/localtime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Linux implementation of the localtime function --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/time/localtime.h"
#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/macros/null_check.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(struct tm *, localtime, (const time_t *timer)) {
LIBC_CRASH_ON_NULLPTR(timer);

static struct tm tm_out;
return time_utils::localtime_internal(timer, &tm_out);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/time/localtime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header of localtime ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_TIME_LOCALTIME_H
#define LLVM_LIBC_SRC_TIME_LOCALTIME_H

#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE_DECL {

struct tm *localtime(const time_t *timer);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_TIME_LOCALTIME_H
25 changes: 25 additions & 0 deletions libc/src/time/localtime_r.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Linux implementation of localtime_r function ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/time/localtime_r.h"
#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/macros/null_check.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(struct tm *, localtime_r,
(const time_t *timer, struct tm *buf)) {
LIBC_CRASH_ON_NULLPTR(timer);
LIBC_CRASH_ON_NULLPTR(buf);

return time_utils::localtime_internal(timer, buf);
}

} // namespace LIBC_NAMESPACE_DECL
22 changes: 22 additions & 0 deletions libc/src/time/localtime_r.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header of localtime_r --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_TIME_LOCALTIME_R_H
#define LLVM_LIBC_SRC_TIME_LOCALTIME_R_H

#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE_DECL {

struct tm *localtime_r(const time_t *timer, struct tm *buf);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_TIME_LOCALTIME_R_H
17 changes: 14 additions & 3 deletions libc/src/time/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,22 @@ LIBC_INLINE tm *gmtime_internal(const time_t *timer, tm *result) {
return result;
}

// TODO: localtime is not yet implemented and a temporary solution is to
// use gmtime, https://github.com/llvm/llvm-project/issues/107597
LIBC_INLINE tm *localtime_internal(const time_t *timer, tm *result) {
time_t seconds = *timer;
// Update the tm structure's year, month, day, etc. from seconds.
if (update_from_seconds(seconds, result) < 0) {
out_of_range();
return nullptr;
}

// TODO(zimirza): implement timezone database

return result;
}

LIBC_INLINE tm *localtime(const time_t *t_ptr) {
static tm result;
return time_utils::gmtime_internal(t_ptr, &result);
return time_utils::localtime_internal(t_ptr, &result);
}

// Returns number of years from (1, year).
Expand Down
23 changes: 23 additions & 0 deletions libc/test/src/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ add_libc_unittest(
libc.hdr.types.struct_tm
)

add_libc_unittest(
localtime_test
SUITE
libc_time_unittests
SRCS
localtime_test.cpp
DEPENDS
libc.hdr.types.time_t
libc.src.time.localtime
)

add_libc_unittest(
localtime_r_test
SUITE
libc_time_unittests
SRCS
localtime_r_test.cpp
DEPENDS
libc.hdr.types.struct_tm
libc.hdr.types.time_t
libc.src.time.localtime_r
)

add_libc_test(
clock_gettime_test
SUITE
Expand Down
Loading
Loading