summaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
authorMichael Jones <michaelrj@google.com>2023-04-28 14:43:04 -0700
committerMichael Jones <michaelrj@google.com>2023-04-28 15:43:23 -0700
commit2dc97921afba741586887037763784fc9b19c304 (patch)
tree371cc338f90914925e5365b3baa3b34c6871db67 /libc/src
parent42e79d9771f96110c7f51ee5f39a76517ae44234 (diff)
downloadllvm-2dc97921afba741586887037763784fc9b19c304.tar.gz
[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
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/time/asctime.h2
-rw-r--r--libc/src/time/asctime_r.h2
-rw-r--r--libc/src/time/clock_gettime.cpp1
-rw-r--r--libc/src/time/difftime.h2
-rw-r--r--libc/src/time/gettimeofday.cpp1
-rw-r--r--libc/src/time/gettimeofday.h2
-rw-r--r--libc/src/time/gmtime.h2
-rw-r--r--libc/src/time/gmtime_r.h2
-rw-r--r--libc/src/time/linux/clock.cpp3
-rw-r--r--libc/src/time/mktime.h2
-rw-r--r--libc/src/time/nanosleep.cpp1
-rw-r--r--libc/src/time/nanosleep.h2
-rw-r--r--libc/src/time/time_utils.cpp10
-rw-r--r--libc/src/time/time_utils.h25
14 files changed, 28 insertions, 29 deletions
diff --git a/libc/src/time/asctime.h b/libc/src/time/asctime.h
index 9d75b40148af..f053ad23eb4e 100644
--- a/libc/src/time/asctime.h
+++ b/libc/src/time/asctime.h
@@ -18,5 +18,3 @@ char *asctime(const struct tm *timeptr);
} // 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
index 8521e782a509..21bba7ebaa3f 100644
--- a/libc/src/time/asctime_r.h
+++ b/libc/src/time/asctime_r.h
@@ -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"
diff --git a/libc/src/time/clock_gettime.cpp b/libc/src/time/clock_gettime.cpp
index e8f9994ae696..0bd14a114936 100644
--- 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
index feadef135933..249dfed4fe81 100644
--- a/libc/src/time/difftime.h
+++ b/libc/src/time/difftime.h
@@ -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"
diff --git a/libc/src/time/gettimeofday.cpp b/libc/src/time/gettimeofday.cpp
index 7c16c01d1dd9..914b5826e9cf 100644
--- 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/gettimeofday.h b/libc/src/time/gettimeofday.h
index 42182ee28e2a..ee08efba8381 100644
--- a/libc/src/time/gettimeofday.h
+++ b/libc/src/time/gettimeofday.h
@@ -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"
diff --git a/libc/src/time/gmtime.h b/libc/src/time/gmtime.h
index 8891a8c917ac..950ba4f5673a 100644
--- a/libc/src/time/gmtime.h
+++ b/libc/src/time/gmtime.h
@@ -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"
diff --git a/libc/src/time/gmtime_r.h b/libc/src/time/gmtime_r.h
index 8e9fc94b5cee..619af603c98d 100644
--- a/libc/src/time/gmtime_r.h
+++ b/libc/src/time/gmtime_r.h
@@ -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"
diff --git a/libc/src/time/linux/clock.cpp b/libc/src/time/linux/clock.cpp
index ff91e9548808..f72e4fcf3729 100644
--- a/libc/src/time/linux/clock.cpp
+++ b/libc/src/time/linux/clock.cpp
@@ -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 =
diff --git a/libc/src/time/mktime.h b/libc/src/time/mktime.h
index 05dced8b6077..efe9cf44565a 100644
--- a/libc/src/time/mktime.h
+++ b/libc/src/time/mktime.h
@@ -18,5 +18,3 @@ time_t mktime(struct tm *t1);
} // namespace __llvm_libc
#endif // LLVM_LIBC_SRC_TIME_MKTIME_H
-
-#include "include/time.h"
diff --git a/libc/src/time/nanosleep.cpp b/libc/src/time/nanosleep.cpp
index de2503953f98..f0458de2d1a8 100644
--- 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/nanosleep.h b/libc/src/time/nanosleep.h
index 0abe73948be4..d8ecfb758f93 100644
--- a/libc/src/time/nanosleep.h
+++ b/libc/src/time/nanosleep.h
@@ -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"
diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp
index ca5f16cb7c9a..b25b737d1f8d 100644
--- a/libc/src/time/time_utils.cpp
+++ b/libc/src/time/time_utils.cpp
@@ -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;
diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index 362b4a9085a9..8075bfe32027 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -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],