[libc][NFC] Start cleanup of time functions

The time functions have not yet been updated to match our new coding
patterns. This patch removes some unnecessary includes, adjusts the
names of the test targets, and adds several TODO comments. It is my
intention to follow this patch up with additional cleanup.

Reviewed By: sivachandra, rtenneti

Differential Revision: https://reviews.llvm.org/D149487
This commit is contained in:
Michael Jones 2023-04-28 14:43:04 -07:00
parent 42e79d9771
commit 2dc97921af
15 changed files with 37 additions and 38 deletions

View File

@ -18,5 +18,3 @@ char *asctime(const struct tm *timeptr);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_ASCTIME_H
#include "include/time.h"

View File

@ -18,5 +18,3 @@ char *asctime_r(const struct tm *timeptr, char *buffer);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_ASCTIME_R_H
#include "include/time.h"

View File

@ -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 =

View File

@ -18,5 +18,3 @@ double difftime(time_t end, time_t beginning);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_DIFFTIME_H
#include "include/time.h"

View File

@ -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)

View File

@ -18,5 +18,3 @@ int gettimeofday(struct timeval *tv, void *tz);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_GETTIMEOFDAY_H
#include "include/time.h"

View File

@ -18,5 +18,3 @@ struct tm *gmtime(const time_t *timer);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_GMTIME_H
#include "include/time.h"

View File

@ -18,5 +18,3 @@ struct tm *gmtime_r(const time_t *timer, struct tm *result);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_GMTIME_R_H
#include "include/time.h"

View File

@ -27,8 +27,7 @@ LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
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 =

View File

@ -18,5 +18,3 @@ time_t mktime(struct tm *t1);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_MKTIME_H
#include "include/time.h"

View File

@ -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);

View File

@ -18,5 +18,3 @@ int nanosleep(const struct timespec *req, struct timespec *rem);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_NANOSLEEP_H
#include "include/time.h"

View File

@ -85,19 +85,19 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
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 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
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;

View File

@ -37,18 +37,21 @@ enum Month : int {
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 <time.h>, or if struct tm's
@ -64,10 +67,10 @@ struct TimeConstants {
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 @@ struct TimeConstants {
// "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 @@ LIBC_INLINE char *asctime(const struct tm *timeptr, char *buffer,
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],

View File

@ -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(
)
add_libc_unittest(
asctime_r
asctime_r_test
SUITE
libc_time_unittests
SRCS
@ -31,7 +31,7 @@ add_libc_unittest(
)
add_libc_unittest(
clock_gettime
clock_gettime_test
SUITE
libc_time_unittests
SRCS
@ -41,7 +41,7 @@ add_libc_unittest(
)
add_libc_unittest(
difftime
difftime_test
SUITE
libc_time_unittests
SRCS
@ -51,7 +51,7 @@ add_libc_unittest(
)
add_libc_unittest(
gettimeofday
gettimeofday_test
SUITE
libc_time_unittests
SRCS
@ -69,7 +69,7 @@ add_libc_unittest(
)
add_libc_unittest(
gmtime
gmtime_test
SUITE
libc_time_unittests
SRCS
@ -81,7 +81,7 @@ add_libc_unittest(
)
add_libc_unittest(
gmtime_r
gmtime_r_test
SUITE
libc_time_unittests
SRCS
@ -93,7 +93,7 @@ add_libc_unittest(
)
add_libc_unittest(
mktime
mktime_test
SUITE
libc_time_unittests
SRCS
@ -108,7 +108,7 @@ add_libc_unittest(
)
add_libc_unittest(
nanosleep
nanosleep_test
SUITE
libc_time_unittests
SRCS