#ifndef LLVM_LIBC_SRC_TIME_TIME_UTILS_H
#define LLVM_LIBC_SRC_TIME_TIME_UTILS_H
#include "hdr/stdint_proxy.h"
#include "hdr/types/size_t.h"
#include "hdr/types/struct_tm.h"
#include "hdr/types/time_t.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "time_constants.h"
namespace LIBC_NAMESPACE_DECL {
namespace time_utils {
cpp::optional<time_t> mktime_internal(const tm *tm_out);
int64_t update_from_seconds(time_t total_seconds, tm *tm);
LIBC_INLINE time_t out_of_range() {
#ifdef EOVERFLOW
libc_errno = EOVERFLOW;
#endif
return time_constants::OUT_OF_RANGE_RETURN_VALUE;
}
LIBC_INLINE void invalid_value() { libc_errno = EINVAL; }
LIBC_INLINE char *asctime(const tm *timeptr, char *buffer,
size_t bufferLength) {
if (timeptr == nullptr || buffer == nullptr) {
invalid_value();
return nullptr;
}
if (timeptr->tm_wday < 0 ||
timeptr->tm_wday > (time_constants::DAYS_PER_WEEK - 1)) {
invalid_value();
return nullptr;
}
if (timeptr->tm_mon < 0 ||
timeptr->tm_mon > (time_constants::MONTHS_PER_YEAR - 1)) {
invalid_value();
return nullptr;
}
int written_size = __builtin_snprintf(
buffer, bufferLength, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
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) {
out_of_range();
return nullptr;
}
return buffer;
}
LIBC_INLINE tm *gmtime_internal(const time_t *timer, tm *result) {
time_t seconds = *timer;
if (update_from_seconds(seconds, result) < 0) {
out_of_range();
return nullptr;
}
return result;
}
LIBC_INLINE tm *localtime_internal(const time_t *timer, tm *result) {
time_t seconds = *timer;
if (update_from_seconds(seconds, result) < 0) {
out_of_range();
return nullptr;
}
return result;
}
LIBC_INLINE tm *localtime(const time_t *t_ptr) {
static tm result;
return time_utils::localtime_internal(t_ptr, &result);
}
LIBC_INLINE constexpr int64_t get_num_of_leap_years_before(int64_t year) {
return (year / 4) - (year / 100) + (year / 400);
}
LIBC_INLINE constexpr bool is_leap_year(const int64_t year) {
return (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0));
}
LIBC_INLINE constexpr int get_days_in_year(const int year) {
return is_leap_year(year) ? time_constants::DAYS_PER_LEAP_YEAR
: time_constants::DAYS_PER_NON_LEAP_YEAR;
}
class TMReader final {
const tm *timeptr;
template <size_t N>
LIBC_INLINE constexpr cpp::optional<cpp::string_view>
bounds_check(const cpp::array<cpp::string_view, N> &arr, int index) const {
if (index >= 0 && index < static_cast<int>(arr.size()))
return arr[index];
return cpp::nullopt;
}
public:
LIBC_INLINE constexpr explicit TMReader(const tm *tmptr) : timeptr(tmptr) {}
LIBC_INLINE constexpr cpp::optional<cpp::string_view>
get_weekday_short_name() const {
return bounds_check(time_constants::WEEK_DAY_NAMES, timeptr->tm_wday);
}
LIBC_INLINE constexpr cpp::optional<cpp::string_view>
get_weekday_full_name() const {
return bounds_check(time_constants::WEEK_DAY_FULL_NAMES, timeptr->tm_wday);
}
LIBC_INLINE constexpr cpp::optional<cpp::string_view>
get_month_short_name() const {
return bounds_check(time_constants::MONTH_NAMES, timeptr->tm_mon);
}
LIBC_INLINE constexpr cpp::optional<cpp::string_view>
get_month_full_name() const {
return bounds_check(time_constants::MONTH_FULL_NAMES, timeptr->tm_mon);
}
LIBC_INLINE constexpr cpp::string_view get_am_pm() const {
if (timeptr->tm_hour < 12)
return "AM";
return "PM";
}
LIBC_INLINE constexpr cpp::string_view get_timezone_name() const {
return "UTC";
}
LIBC_INLINE constexpr int get_sec() const { return timeptr->tm_sec; }
LIBC_INLINE constexpr int get_min() const { return timeptr->tm_min; }
LIBC_INLINE constexpr int get_hour() const { return timeptr->tm_hour; }
LIBC_INLINE constexpr int get_mday() const { return timeptr->tm_mday; }
LIBC_INLINE constexpr int get_mon() const { return timeptr->tm_mon; }
LIBC_INLINE constexpr int get_yday() const { return timeptr->tm_yday; }
LIBC_INLINE constexpr int get_wday() const { return timeptr->tm_wday; }
LIBC_INLINE constexpr int get_isdst() const { return timeptr->tm_isdst; }
LIBC_INLINE constexpr int get_year_raw() const { return timeptr->tm_year; }
LIBC_INLINE constexpr int get_year() const {
return timeptr->tm_year + time_constants::TIME_YEAR_BASE;
}
LIBC_INLINE constexpr int is_leap_year() const {
return time_utils::is_leap_year(get_year());
}
LIBC_INLINE constexpr int get_iso_wday() const {
using time_constants::DAYS_PER_WEEK;
using time_constants::MONDAY;
const int NORMALIZED_WDAY = timeptr->tm_wday % DAYS_PER_WEEK;
return (NORMALIZED_WDAY + (DAYS_PER_WEEK - MONDAY)) % DAYS_PER_WEEK;
}
LIBC_INLINE constexpr int get_week(time_constants::WeekDay start_day) const {
using time_constants::DAYS_PER_WEEK;
const int start_of_cur_week =
timeptr->tm_yday -
((timeptr->tm_wday + DAYS_PER_WEEK - start_day) % DAYS_PER_WEEK);
const int ceil_weeks_since_start =
(start_of_cur_week + DAYS_PER_WEEK) / DAYS_PER_WEEK;
return ceil_weeks_since_start;
}
LIBC_INLINE constexpr int get_iso_week() const {
using time_constants::DAYS_PER_WEEK;
using time_constants::ISO_FIRST_DAY_OF_YEAR;
using time_constants::MONDAY;
using time_constants::WeekDay;
using time_constants::WEEKS_PER_YEAR;
constexpr WeekDay START_DAY = MONDAY;
const int start_of_cur_week =
timeptr->tm_yday -
((timeptr->tm_wday + DAYS_PER_WEEK - START_DAY) % DAYS_PER_WEEK);
if (start_of_cur_week < -3) {
const int days_into_prev_year =
get_days_in_year(get_year() - 1) + start_of_cur_week;
return WEEKS_PER_YEAR +
((days_into_prev_year % DAYS_PER_WEEK) > ISO_FIRST_DAY_OF_YEAR);
}
const int days_until_end_of_year =
get_days_in_year(get_year()) - start_of_cur_week - 1;
if (days_until_end_of_year < 3)
return 1;
const int ceil_weeks_since_start =
(start_of_cur_week + DAYS_PER_WEEK) / DAYS_PER_WEEK;
const int WEEK_STARTS_IN_PREV_YEAR =
((start_of_cur_week + time_constants::DAYS_PER_WEEK) %
time_constants::DAYS_PER_WEEK) > time_constants::ISO_FIRST_DAY_OF_YEAR;
return ceil_weeks_since_start + WEEK_STARTS_IN_PREV_YEAR;
}
LIBC_INLINE constexpr int get_iso_year() const {
const int BASE_YEAR = get_year();
if (timeptr->tm_yday >= time_constants::ISO_FIRST_DAY_OF_YEAR &&
timeptr->tm_yday < time_constants::DAYS_PER_NON_LEAP_YEAR -
time_constants::DAYS_PER_WEEK)
return BASE_YEAR;
const int ISO_WDAY = get_iso_wday();
if (timeptr->tm_yday < time_constants::ISO_FIRST_DAY_OF_YEAR) {
If jan 4 is in this week, then we're in BASE_YEAR, else we're in the
previous year. The formula's been rearranged so here's the derivation:
+--------+-- days until jan 4
| |
wday + (4 - yday) < 7
| |
+---------------+-- weekday of jan 4
rearranged to get all the constants on one side:
wday - yday < 7 - 4
*/
const int IS_CUR_YEAR = (ISO_WDAY - timeptr->tm_yday <
time_constants::DAYS_PER_WEEK -
time_constants::ISO_FIRST_DAY_OF_YEAR);
return BASE_YEAR - !IS_CUR_YEAR;
}
const int DAYS_LEFT_IN_YEAR =
get_days_in_year(get_year()) - timeptr->tm_yday;
Similar to above, we're checking if jan 4 (of next year) is in this week. If
it is, this is in the next year. Note that this also handles the case of
yday > days in year gracefully.
+------------------+-- days until jan 4 (of next year)
| |
wday + (4 + remaining days) < 7
| |
+-------------------------+-- weekday of jan 4
rearranging we get:
wday + remaining days < 7 - 4
*/
const int IS_NEXT_YEAR =
(ISO_WDAY + DAYS_LEFT_IN_YEAR <
time_constants::DAYS_PER_WEEK - time_constants::ISO_FIRST_DAY_OF_YEAR);
return BASE_YEAR + IS_NEXT_YEAR;
}
LIBC_INLINE time_t get_epoch() const {
auto seconds = mktime_internal(timeptr);
return seconds ? *seconds : time_utils::out_of_range();
}
LIBC_INLINE constexpr int get_timezone_offset() const {
return 0;
}
};
}
}
#endif