diff --git a/libc/src/time/asctime.h b/libc/src/time/asctime.h --- a/libc/src/time/asctime.h +++ b/libc/src/time/asctime.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_ASCTIME_H - -#include "include/time.h" diff --git a/libc/src/time/asctime_r.h b/libc/src/time/asctime_r.h --- a/libc/src/time/asctime_r.h +++ b/libc/src/time/asctime_r.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_ASCTIME_R_H - -#include "include/time.h" diff --git a/libc/src/time/clock_gettime.cpp b/libc/src/time/clock_gettime.cpp --- a/libc/src/time/clock_gettime.cpp +++ b/libc/src/time/clock_gettime.cpp @@ -17,6 +17,7 @@ namespace __llvm_libc { +// TODO(michaelrj): Move this into time/linux with the other syscalls. LLVM_LIBC_FUNCTION(int, clock_gettime, (clockid_t clockid, struct timespec *tp)) { long ret_val = diff --git a/libc/src/time/difftime.h b/libc/src/time/difftime.h --- a/libc/src/time/difftime.h +++ b/libc/src/time/difftime.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_DIFFTIME_H - -#include "include/time.h" diff --git a/libc/src/time/gettimeofday.h b/libc/src/time/gettimeofday.h --- a/libc/src/time/gettimeofday.h +++ b/libc/src/time/gettimeofday.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_GETTIMEOFDAY_H - -#include "include/time.h" diff --git a/libc/src/time/gettimeofday.cpp b/libc/src/time/gettimeofday.cpp --- a/libc/src/time/gettimeofday.cpp +++ b/libc/src/time/gettimeofday.cpp @@ -16,6 +16,7 @@ namespace __llvm_libc { +// TODO(michaelrj): Move this into time/linux with the other syscalls. LLVM_LIBC_FUNCTION(int, gettimeofday, (struct timeval * tv, [[maybe_unused]] void *unused)) { if (tv == nullptr) diff --git a/libc/src/time/gmtime.h b/libc/src/time/gmtime.h --- a/libc/src/time/gmtime.h +++ b/libc/src/time/gmtime.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_GMTIME_H - -#include "include/time.h" diff --git a/libc/src/time/gmtime_r.h b/libc/src/time/gmtime_r.h --- a/libc/src/time/gmtime_r.h +++ b/libc/src/time/gmtime_r.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_GMTIME_R_H - -#include "include/time.h" diff --git a/libc/src/time/linux/clock.cpp b/libc/src/time/linux/clock.cpp --- a/libc/src/time/linux/clock.cpp +++ b/libc/src/time/linux/clock.cpp @@ -27,8 +27,7 @@ return clock_t(-1); } - // The above syscall gets the CPU time in seconds plus nanoseconds. We should - // make sure that corresponding clocks can actually be represented by clock-t. + // The above syscall gets the CPU time in seconds plus nanoseconds. // The standard requires that we return clock_t(-1) if we cannot represent // clocks as a clock_t value. constexpr clock_t CLOCK_SECS_MAX = diff --git a/libc/src/time/mktime.h b/libc/src/time/mktime.h --- a/libc/src/time/mktime.h +++ b/libc/src/time/mktime.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_MKTIME_H - -#include "include/time.h" diff --git a/libc/src/time/nanosleep.h b/libc/src/time/nanosleep.h --- a/libc/src/time/nanosleep.h +++ b/libc/src/time/nanosleep.h @@ -18,5 +18,3 @@ } // namespace __llvm_libc #endif // LLVM_LIBC_SRC_TIME_NANOSLEEP_H - -#include "include/time.h" diff --git a/libc/src/time/nanosleep.cpp b/libc/src/time/nanosleep.cpp --- a/libc/src/time/nanosleep.cpp +++ b/libc/src/time/nanosleep.cpp @@ -15,6 +15,7 @@ namespace __llvm_libc { +// TODO(michaelrj): Move this into time/linux with the other syscalls. LLVM_LIBC_FUNCTION(int, nanosleep, (const struct timespec *req, struct timespec *rem)) { int ret = __llvm_libc::syscall_impl(SYS_nanosleep, req, rem); diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h --- a/libc/src/time/time_utils.h +++ b/libc/src/time/time_utils.h @@ -37,18 +37,21 @@ struct TimeConstants { static constexpr int SECONDS_PER_MIN = 60; - static constexpr int SECONDS_PER_HOUR = 3600; - static constexpr int SECONDS_PER_DAY = 86400; static constexpr int MINUTES_PER_HOUR = 60; + static constexpr int HOURS_PER_DAY = 24; static constexpr int DAYS_PER_WEEK = 7; static constexpr int MONTHS_PER_YEAR = 12; static constexpr int DAYS_PER_NON_LEAP_YEAR = 365; static constexpr int DAYS_PER_LEAP_YEAR = 366; + + static constexpr int SECONDS_PER_HOUR = SECONDS_PER_MIN * MINUTES_PER_HOUR; + static constexpr int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; + static constexpr int NUMBER_OF_SECONDS_IN_LEAP_YEAR = + DAYS_PER_LEAP_YEAR * SECONDS_PER_DAY; + static constexpr int TIME_YEAR_BASE = 1900; static constexpr int EPOCH_YEAR = 1970; static constexpr int EPOCH_WEEK_DAY = 4; - static constexpr int NUMBER_OF_SECONDS_IN_LEAP_YEAR = - (DAYS_PER_NON_LEAP_YEAR + 1) * SECONDS_PER_DAY; // For asctime the behavior is undefined if struct tm's tm_wday or tm_mon are // not within the normal ranges as defined in , or if struct tm's @@ -64,10 +67,10 @@ static constexpr int WEEK_DAY_OF2000_MARCH_FIRST = 3; static constexpr int DAYS_PER400_YEARS = - (DAYS_PER_NON_LEAP_YEAR * 400 + (400 / 4) - 3); + (DAYS_PER_NON_LEAP_YEAR * 400) + (400 / 4) - 3; static constexpr int DAYS_PER100_YEARS = - (DAYS_PER_NON_LEAP_YEAR * 100 + (100 / 4) - 1); - static constexpr int DAYS_PER4_YEARS = (DAYS_PER_NON_LEAP_YEAR * 4 + 1); + (DAYS_PER_NON_LEAP_YEAR * 100) + (100 / 4) - 1; + static constexpr int DAYS_PER4_YEARS = (DAYS_PER_NON_LEAP_YEAR * 4) + 1; // The latest time that can be represented in this form is 03:14:07 UTC on // Tuesday, 19 January 2038 (corresponding to 2,147,483,647 seconds since the @@ -82,6 +85,10 @@ // "total_seconds" is the number of seconds since January 1st, 1970. extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm); +// TODO(michaelrj): move these functions to use ErrorOr instead of setting +// errno. They always accompany a specific return value so we only need the one +// variable. + // POSIX.1-2017 requires this. LIBC_INLINE time_t out_of_range() { libc_errno = EOVERFLOW; @@ -114,6 +121,10 @@ static const char *months_name[TimeConstants::MONTHS_PER_YEAR] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + + // TODO(michaelr): look into removing this call to __builtin_snprintf that may + // be emitted as a call to snprintf. Alternatively, look into using our + // internal printf machinery. int written_size = __builtin_snprintf( buffer, bufferLength, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", week_days_name[timeptr->tm_wday], months_name[timeptr->tm_mon], diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp --- a/libc/src/time/time_utils.cpp +++ b/libc/src/time/time_utils.cpp @@ -85,19 +85,19 @@ numOfFourHundredYearCycles--; } - // The reminder number of years after computing number of + // The remaining number of years after computing the number of // "four hundred year cycles" will be 4 hundred year cycles or less in 400 // years. int64_t numOfHundredYearCycles = computeRemainingYears( TimeConstants::DAYS_PER100_YEARS, 4, &remainingDays); - // The reminder number of years after computing number of + // The remaining number of years after computing the number of // "hundred year cycles" will be 25 four year cycles or less in 100 years. int64_t numOfFourYearCycles = computeRemainingYears(TimeConstants::DAYS_PER4_YEARS, 25, &remainingDays); - // The reminder number of years after computing number of "four year cycles" - // will be 4 one year cycles or less in 4 years. + // The remaining number of years after computing the number of + // "four year cycles" will be 4 one year cycles or less in 4 years. int64_t remainingYears = computeRemainingYears( TimeConstants::DAYS_PER_NON_LEAP_YEAR, 4, &remainingDays); @@ -109,6 +109,8 @@ int leapDay = !remainingYears && (numOfFourYearCycles || !numOfHundredYearCycles); + // We add 31 and 28 for the number of days in January and February, since our + // starting point was March 1st. int64_t yday = remainingDays + 31 + 28 + leapDay; if (yday >= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay) yday -= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay; diff --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt --- a/libc/test/src/time/CMakeLists.txt +++ b/libc/test/src/time/CMakeLists.txt @@ -1,7 +1,7 @@ add_custom_target(libc_time_unittests) add_libc_unittest( - asctime + asctime_test SUITE libc_time_unittests SRCS @@ -16,7 +16,7 @@ ) add_libc_unittest( - asctime_r + asctime_r_test SUITE libc_time_unittests SRCS @@ -31,7 +31,7 @@ ) add_libc_unittest( - clock_gettime + clock_gettime_test SUITE libc_time_unittests SRCS @@ -41,7 +41,7 @@ ) add_libc_unittest( - difftime + difftime_test SUITE libc_time_unittests SRCS @@ -51,7 +51,7 @@ ) add_libc_unittest( - gettimeofday + gettimeofday_test SUITE libc_time_unittests SRCS @@ -69,7 +69,7 @@ ) add_libc_unittest( - gmtime + gmtime_test SUITE libc_time_unittests SRCS @@ -81,7 +81,7 @@ ) add_libc_unittest( - gmtime_r + gmtime_r_test SUITE libc_time_unittests SRCS @@ -93,7 +93,7 @@ ) add_libc_unittest( - mktime + mktime_test SUITE libc_time_unittests SRCS @@ -108,7 +108,7 @@ ) add_libc_unittest( - nanosleep + nanosleep_test SUITE libc_time_unittests SRCS