Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
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
2 changes: 1 addition & 1 deletion 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 "hdr/time_macros.h"
#include <pthread.h> // pthread_condattr_t, PTHREAD_PROCESS_PRIVATE
#include <time.h> // CLOCK_REALTIME

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion 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 "hdr/time_macros.h"
#include <pthread.h> // pthread_condattr_t
#include <sys/types.h> // clockid_t
#include <time.h> // CLOCK_MONOTONIC, CLOCK_REALTIME

namespace LIBC_NAMESPACE_DECL {

Expand Down
25 changes: 25 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 @@ -23,6 +38,7 @@ add_entrypoint_object(
DEPENDS
.time_utils
libc.include.time
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -34,6 +50,7 @@ add_entrypoint_object(
DEPENDS
.time_utils
libc.include.time
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand Down Expand Up @@ -68,6 +85,7 @@ add_entrypoint_object(
difftime.h
DEPENDS
libc.include.time
libc.hdr.types.time_t
)

add_entrypoint_object(
Expand All @@ -79,6 +97,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 +110,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 @@ -102,6 +124,8 @@ add_entrypoint_object(
.time_utils
libc.include.time
libc.src.errno.errno
libc.hdr.types.time_t
libc.hdr.types.struct_tm
)

add_entrypoint_object(
Expand All @@ -115,6 +139,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
7 changes: 3 additions & 4 deletions libc/src/time/asctime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@

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
6 changes: 2 additions & 4 deletions libc/src/time/ctime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@

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
4 changes: 1 addition & 3 deletions libc/src/time/ctime_r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@

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
33 changes: 14 additions & 19 deletions libc/src/time/mktime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

namespace LIBC_NAMESPACE_DECL {

using LIBC_NAMESPACE::time_utils::TimeConstants;

static constexpr int NON_LEAP_YEAR_DAYS_IN_MONTH[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};

// Returns number of years from (1, year).
static constexpr int64_t get_num_of_leap_years_before(int64_t year) {
return (year / 4) - (year / 100) + (year / 400);
Expand All @@ -31,12 +26,12 @@ static constexpr bool is_leap_year(const int64_t year) {
LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
// Unlike most C Library functions, mktime doesn't just die on bad input.
// TODO(rtenneti); Handle leap seconds.
int64_t tm_year_from_base = tm_out->tm_year + TimeConstants::TIME_YEAR_BASE;
int64_t tm_year_from_base = tm_out->tm_year + time_constants::TIME_YEAR_BASE;

// 32-bit end-of-the-world is 03:14:07 UTC on 19 January 2038.
if (sizeof(time_t) == 4 &&
tm_year_from_base >= TimeConstants::END_OF32_BIT_EPOCH_YEAR) {
if (tm_year_from_base > TimeConstants::END_OF32_BIT_EPOCH_YEAR)
tm_year_from_base >= time_constants::END_OF32_BIT_EPOCH_YEAR) {
if (tm_year_from_base > time_constants::END_OF32_BIT_EPOCH_YEAR)
return time_utils::out_of_range();
if (tm_out->tm_mon > 0)
return time_utils::out_of_range();
Expand Down Expand Up @@ -64,7 +59,7 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {

// Calculate number of months and years from tm_mon.
int64_t month = tm_out->tm_mon;
if (month < 0 || month >= TimeConstants::MONTHS_PER_YEAR - 1) {
if (month < 0 || month >= time_constants::MONTHS_PER_YEAR - 1) {
int64_t years = month / 12;
month %= 12;
if (month < 0) {
Expand All @@ -78,23 +73,23 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
// Calculate total number of days based on the month and the day (tm_mday).
int64_t total_days = tm_out->tm_mday - 1;
for (int64_t i = 0; i < month; ++i)
total_days += NON_LEAP_YEAR_DAYS_IN_MONTH[i];
total_days += time_constants::NON_LEAP_YEAR_DAYS_IN_MONTH[i];
// Add one day if it is a leap year and the month is after February.
if (tm_year_is_leap && month > 1)
total_days++;

// Calculate total numbers of days based on the year.
total_days += (tm_year_from_base - TimeConstants::EPOCH_YEAR) *
TimeConstants::DAYS_PER_NON_LEAP_YEAR;
if (tm_year_from_base >= TimeConstants::EPOCH_YEAR) {
total_days += (tm_year_from_base - time_constants::EPOCH_YEAR) *
time_constants::DAYS_PER_NON_LEAP_YEAR;
if (tm_year_from_base >= time_constants::EPOCH_YEAR) {
total_days += get_num_of_leap_years_before(tm_year_from_base - 1) -
get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR);
get_num_of_leap_years_before(time_constants::EPOCH_YEAR);
} else if (tm_year_from_base >= 1) {
total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) -
total_days -= get_num_of_leap_years_before(time_constants::EPOCH_YEAR) -
get_num_of_leap_years_before(tm_year_from_base - 1);
} else {
// Calculate number of leap years until 0th year.
total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) -
total_days -= get_num_of_leap_years_before(time_constants::EPOCH_YEAR) -
get_num_of_leap_years_before(0);
if (tm_year_from_base <= 0) {
total_days -= 1; // Subtract 1 for 0th year.
Expand All @@ -109,9 +104,9 @@ LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
// TODO: https://github.com/llvm/llvm-project/issues/121962
// Need to handle timezone and update of tm_isdst.
int64_t seconds = tm_out->tm_sec +
tm_out->tm_min * TimeConstants::SECONDS_PER_MIN +
tm_out->tm_hour * TimeConstants::SECONDS_PER_HOUR +
total_days * TimeConstants::SECONDS_PER_DAY;
tm_out->tm_min * time_constants::SECONDS_PER_MIN +
tm_out->tm_hour * time_constants::SECONDS_PER_HOUR +
total_days * time_constants::SECONDS_PER_DAY;

// Update the tm structure's year, month, day, etc. from seconds.
if (time_utils::update_from_seconds(seconds, tm_out) < 0)
Expand Down
3 changes: 2 additions & 1 deletion libc/src/time/mktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#ifndef LLVM_LIBC_SRC_TIME_MKTIME_H
#define LLVM_LIBC_SRC_TIME_MKTIME_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
Loading