aboutsummaryrefslogtreecommitdiff
path: root/libc/src/time/time_utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'libc/src/time/time_utils.h')
-rw-r--r--libc/src/time/time_utils.h93
1 files changed, 12 insertions, 81 deletions
diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index 552ea92..5e0a692 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -9,79 +9,19 @@
#ifndef LLVM_LIBC_SRC_TIME_TIME_UTILS_H
#define LLVM_LIBC_SRC_TIME_TIME_UTILS_H
-#include <stddef.h> // For size_t.
-
+#include "hdr/types/size_t.h"
+#include "hdr/types/struct_tm.h"
+#include "hdr/types/time_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"
-#include "src/time/mktime.h"
+#include "time_constants.h"
#include <stdint.h>
namespace LIBC_NAMESPACE_DECL {
namespace time_utils {
-enum Month : int {
- JANUARY,
- FEBRUARY,
- MARCH,
- APRIL,
- MAY,
- JUNE,
- JULY,
- AUGUST,
- SEPTEMBER,
- OCTOBER,
- NOVEMBER,
- DECEMBER
-};
-
-struct TimeConstants {
- static constexpr int SECONDS_PER_MIN = 60;
- 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;
-
- // 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
- // tm_year exceeds {INT_MAX}-1990, or if the below asctime_internal algorithm
- // would attempt to generate more than 26 bytes of output (including the
- // terminating null).
- static constexpr int ASCTIME_BUFFER_SIZE = 256;
- static constexpr int ASCTIME_MAX_BYTES = 26;
-
- /* 2000-03-01 (mod 400 year, immediately after feb29 */
- static constexpr int64_t SECONDS_UNTIL2000_MARCH_FIRST =
- (946684800LL + SECONDS_PER_DAY * (31 + 29));
- static constexpr int WEEK_DAY_OF2000_MARCH_FIRST = 3;
-
- static constexpr int DAYS_PER400_YEARS =
- (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;
-
- // 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
- // start of the epoch). This means that systems using a 32-bit time_t type are
- // susceptible to the Year 2038 problem.
- static constexpr int END_OF32_BIT_EPOCH_YEAR = 2038;
-
- static constexpr time_t OUT_OF_RANGE_RETURN_VALUE = -1;
-};
-
// Update the "tm" structure's year, month, etc. members from seconds.
// "total_seconds" is the number of seconds since January 1st, 1970.
extern int64_t update_from_seconds(int64_t total_seconds, struct tm *tm);
@@ -98,7 +38,7 @@ LIBC_INLINE time_t out_of_range() {
// require it.
libc_errno = EOVERFLOW;
#endif
- return TimeConstants::OUT_OF_RANGE_RETURN_VALUE;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
}
LIBC_INLINE void invalid_value() { libc_errno = EINVAL; }
@@ -110,32 +50,23 @@ LIBC_INLINE char *asctime(const struct tm *timeptr, char *buffer,
return nullptr;
}
if (timeptr->tm_wday < 0 ||
- timeptr->tm_wday > (TimeConstants::DAYS_PER_WEEK - 1)) {
+ timeptr->tm_wday > (time_constants::DAYS_PER_WEEK - 1)) {
invalid_value();
return nullptr;
}
if (timeptr->tm_mon < 0 ||
- timeptr->tm_mon > (TimeConstants::MONTHS_PER_YEAR - 1)) {
+ timeptr->tm_mon > (time_constants::MONTHS_PER_YEAR - 1)) {
invalid_value();
return nullptr;
}
- // TODO(rtenneti): i18n the following strings.
- static const char *week_days_name[TimeConstants::DAYS_PER_WEEK] = {
- "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
-
- 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.
+ // TODO(michaelr): move this to use the strftime 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],
- timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec,
- TimeConstants::TIME_YEAR_BASE + timeptr->tm_year);
+ time_constants::WEEK_DAY_NAMES[timeptr->tm_wday].data(),
+ time_constants::MONTH_NAMES[timeptr->tm_mon].data(), timeptr->tm_mday,
+ timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec,
+ time_constants::TIME_YEAR_BASE + timeptr->tm_year);
if (written_size < 0)
return nullptr;
if (static_cast<size_t>(written_size) >= bufferLength) {