Skip to content

Commit

Permalink
[libc][NFC] Cleanup time.h (llvm#122027)
Browse files Browse the repository at this point in the history
While working on strftime I noticed some constants were being defined in
unexpected places. One thing led to another, and I ended up doing a
major cleanup of the time functions.

What's included:
All uses of <time.h> in /src and /test removed (except for LibcTest.cpp)
The various time constants have been moved to time_constants.h, and the
`time_constants` namespace.
struct tm gets its own type indirection header now.
  • Loading branch information
michaelrj-google authored Jan 8, 2025
1 parent 75781fa commit f9c2377
Show file tree
Hide file tree
Showing 33 changed files with 790 additions and 556 deletions.
8 changes: 8 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_timespec
)

add_proxy_header_library(
struct_tm
HDRS
struct_tm.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_tm
)

add_proxy_header_library(
size_t
HDRS
Expand Down
21 changes: 21 additions & 0 deletions libc/hdr/types/struct_tm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Proxy for struct tm ----------------------------------------===//
//
// 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_TYPES_STRUCT_TM_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_TM_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/struct_tm.h"

#else

#include <time.h>

#endif // LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_STRUCT_TM_H
4 changes: 2 additions & 2 deletions libc/src/pthread/pthread_condattr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

#include <pthread.h> // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE
#include <time.h> // CLOCK_REALTIME
#include "hdr/time_macros.h" // CLOCK_REALTIME
#include <pthread.h> // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE

namespace LIBC_NAMESPACE_DECL {

Expand Down
6 changes: 3 additions & 3 deletions libc/src/pthread/pthread_condattr_setclock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <pthread.h> // pthread_condattr_t
#include <sys/types.h> // clockid_t
#include <time.h> // CLOCK_MONOTONIC, CLOCK_REALTIME
#include "hdr/time_macros.h" // CLOCK_MONOTONIC, CLOCK_REALTIME
#include <pthread.h> // pthread_condattr_t
#include <sys/types.h> // clockid_t

namespace LIBC_NAMESPACE_DECL {

Expand Down
30 changes: 30 additions & 0 deletions libc/src/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()

add_header_library(
time_constants
HDRS
time_constants.h
DEPENDS
libc.include.time
libc.src.__support.CPP.array
libc.src.__support.CPP.string_view
libc.hdr.types.time_t
)

add_object_library(
time_utils
SRCS
Expand All @@ -12,6 +23,10 @@ add_object_library(
libc.include.time
libc.src.__support.CPP.limits
libc.src.errno.errno
.time_constants
libc.hdr.types.time_t
libc.hdr.types.size_t
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -22,7 +37,9 @@ add_entrypoint_object(
asctime.h
DEPENDS
.time_utils
.time_constants
libc.include.time
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -33,7 +50,9 @@ add_entrypoint_object(
asctime_r.h
DEPENDS
.time_utils
.time_constants
libc.include.time
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -44,6 +63,7 @@ add_entrypoint_object(
ctime.h
DEPENDS
.time_utils
.time_constants
libc.hdr.types.time_t
libc.include.time
)
Expand All @@ -56,6 +76,7 @@ add_entrypoint_object(
ctime_r.h
DEPENDS
.time_utils
.time_constants
libc.hdr.types.time_t
libc.include.time
)
Expand All @@ -68,6 +89,7 @@ add_entrypoint_object(
difftime.h
DEPENDS
libc.include.time
libc.hdr.types.time_t
)

add_entrypoint_object(
Expand All @@ -79,6 +101,8 @@ add_entrypoint_object(
DEPENDS
.time_utils
libc.include.time
libc.hdr.types.time_t
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -90,6 +114,8 @@ add_entrypoint_object(
DEPENDS
.time_utils
libc.include.time
libc.hdr.types.time_t
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -100,8 +126,11 @@ add_entrypoint_object(
mktime.h
DEPENDS
.time_utils
.time_constants
libc.include.time
libc.src.errno.errno
libc.hdr.types.time_t
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -115,6 +144,7 @@ add_entrypoint_object(
libc.hdr.types.time_t
libc.src.__support.time.clock_gettime
libc.src.errno.errno
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand Down
8 changes: 4 additions & 4 deletions libc/src/time/asctime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
#include "src/time/asctime.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/time/time_constants.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

using LIBC_NAMESPACE::time_utils::TimeConstants;

LLVM_LIBC_FUNCTION(char *, asctime, (const struct tm *timeptr)) {
static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
return time_utils::asctime(timeptr, buffer, TimeConstants::ASCTIME_MAX_BYTES);
static char buffer[time_constants::ASCTIME_BUFFER_SIZE];
return time_utils::asctime(timeptr, buffer,
time_constants::ASCTIME_MAX_BYTES);
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 1 addition & 1 deletion libc/src/time/asctime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_TIME_ASCTIME_H
#define LLVM_LIBC_SRC_TIME_ASCTIME_H

#include "hdr/types/struct_tm.h"
#include "src/__support/macros/config.h"
#include <time.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
6 changes: 3 additions & 3 deletions libc/src/time/asctime_r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
#include "src/time/asctime_r.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/time/time_constants.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

using LIBC_NAMESPACE::time_utils::TimeConstants;

LLVM_LIBC_FUNCTION(char *, asctime_r,
(const struct tm *timeptr, char *buffer)) {
return time_utils::asctime(timeptr, buffer, TimeConstants::ASCTIME_MAX_BYTES);
return time_utils::asctime(timeptr, buffer,
time_constants::ASCTIME_MAX_BYTES);
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 1 addition & 1 deletion libc/src/time/asctime_r.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_TIME_ASCTIME_R_H
#define LLVM_LIBC_SRC_TIME_ASCTIME_R_H

#include "hdr/types/struct_tm.h"
#include "src/__support/macros/config.h"
#include <time.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
11 changes: 5 additions & 6 deletions libc/src/time/ctime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
//
//===----------------------------------------------------------------------===//

#include "ctime.h"
#include "src/time/ctime.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "time_utils.h"
#include "src/time/time_constants.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

using LIBC_NAMESPACE::time_utils::TimeConstants;

LLVM_LIBC_FUNCTION(char *, ctime, (const time_t *t_ptr)) {
if (t_ptr == nullptr || *t_ptr > cpp::numeric_limits<int32_t>::max()) {
return nullptr;
}
static char buffer[TimeConstants::ASCTIME_BUFFER_SIZE];
static char buffer[time_constants::ASCTIME_BUFFER_SIZE];
return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
TimeConstants::ASCTIME_MAX_BYTES);
time_constants::ASCTIME_MAX_BYTES);
}

} // namespace LIBC_NAMESPACE_DECL
9 changes: 4 additions & 5 deletions libc/src/time/ctime_r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
//
//===----------------------------------------------------------------------===//

#include "ctime_r.h"
#include "src/time/ctime_r.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "time_utils.h"
#include "src/time/time_constants.h"
#include "src/time/time_utils.h"

namespace LIBC_NAMESPACE_DECL {

using LIBC_NAMESPACE::time_utils::TimeConstants;

LLVM_LIBC_FUNCTION(char *, ctime_r, (const time_t *t_ptr, char *buffer)) {
if (t_ptr == nullptr || buffer == nullptr ||
*t_ptr > cpp::numeric_limits<int32_t>::max()) {
return nullptr;
}

return time_utils::asctime(time_utils::localtime(t_ptr), buffer,
TimeConstants::ASCTIME_MAX_BYTES);
time_constants::ASCTIME_MAX_BYTES);
}

} // namespace LIBC_NAMESPACE_DECL
2 changes: 1 addition & 1 deletion libc/src/time/difftime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef LLVM_LIBC_SRC_TIME_DIFFTIME_H
#define LLVM_LIBC_SRC_TIME_DIFFTIME_H

#include "hdr/types/time_t.h"
#include "src/__support/macros/config.h"
#include <time.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
3 changes: 2 additions & 1 deletion libc/src/time/gmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_TIME_GMTIME_H
#define LLVM_LIBC_SRC_TIME_GMTIME_H

#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/macros/config.h"
#include <time.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
3 changes: 2 additions & 1 deletion libc/src/time/gmtime_r.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_TIME_GMTIME_R_H
#define LLVM_LIBC_SRC_TIME_GMTIME_R_H

#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/macros/config.h"
#include <time.h>

namespace LIBC_NAMESPACE_DECL {

Expand Down
Loading

0 comments on commit f9c2377

Please sign in to comment.