[libc-commits] [libc] 2dc9792 - [libc][NFC] Start cleanup of time functions

Michael Jones via libc-commits libc-commits at lists.llvm.org
Fri Apr 28 15:43:27 PDT 2023


Author: Michael Jones
Date: 2023-04-28T15:43:23-07:00
New Revision: 2dc97921afba741586887037763784fc9b19c304

URL: https://github.com/llvm/llvm-project/commit/2dc97921afba741586887037763784fc9b19c304
DIFF: https://github.com/llvm/llvm-project/commit/2dc97921afba741586887037763784fc9b19c304.diff

LOG: [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

Added: 
    

Modified: 
    libc/src/time/asctime.h
    libc/src/time/asctime_r.h
    libc/src/time/clock_gettime.cpp
    libc/src/time/difftime.h
    libc/src/time/gettimeofday.cpp
    libc/src/time/gettimeofday.h
    libc/src/time/gmtime.h
    libc/src/time/gmtime_r.h
    libc/src/time/linux/clock.cpp
    libc/src/time/mktime.h
    libc/src/time/nanosleep.cpp
    libc/src/time/nanosleep.h
    libc/src/time/time_utils.cpp
    libc/src/time/time_utils.h
    libc/test/src/time/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/src/time/asctime.h b/libc/src/time/asctime.h
index 9d75b40148afd..f053ad23eb4e3 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 8521e782a509d..21bba7ebaa3f5 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 e8f9994ae696e..0bd14a1149363 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/
diff time.h b/libc/src/time/
diff time.h
index feadef135933a..249dfed4fe819 100644
--- a/libc/src/time/
diff time.h
+++ b/libc/src/time/
diff time.h
@@ -18,5 +18,3 @@ double 
diff time(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 7c16c01d1dd91..914b5826e9cfb 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 42182ee28e2a9..ee08efba8381b 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 8891a8c917ac5..950ba4f5673ab 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 8e9fc94b5ceed..619af603c98db 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 ff91e95488088..f72e4fcf37296 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 05dced8b6077c..efe9cf44565a4 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 de2503953f98b..f0458de2d1a85 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 0abe73948be4a..d8ecfb758f93c 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 ca5f16cb7c9a8..b25b737d1f8d0 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 362b4a9085a9f..8075bfe32027a 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],

diff  --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index 85ddd42db2122..6f15bdbc07d4a 100644
--- 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(
 )
 
 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(
-  
diff time
+  
diff time_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


        


More information about the libc-commits mailing list