Skip to content

[libc] change the return value type of mktime_internal to time_t #132231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions libc/src/time/mktime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
int64_t seconds = time_utils::mktime_internal(tm_out);
auto mktime_result = time_utils::mktime_internal(tm_out);
if (!mktime_result)
return time_utils::out_of_range();

time_t seconds = *mktime_result;

// Update the tm structure's year, month, day, etc. from seconds.
if (time_utils::update_from_seconds(seconds, tm_out) < 0)
return time_utils::out_of_range();

return static_cast<time_t>(seconds);
return seconds;
}

} // namespace LIBC_NAMESPACE_DECL
27 changes: 13 additions & 14 deletions libc/src/time/time_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace time_utils {

// TODO: clean this up in a followup patch
int64_t mktime_internal(const tm *tm_out) {
cpp::optional<time_t> mktime_internal(const tm *tm_out) {
// Unlike most C Library functions, mktime doesn't just die on bad input.
// TODO(rtenneti); Handle leap seconds.
int64_t tm_year_from_base = tm_out->tm_year + time_constants::TIME_YEAR_BASE;
Expand All @@ -27,20 +27,20 @@ int64_t mktime_internal(const tm *tm_out) {
if (sizeof(time_t) == 4 &&
tm_year_from_base >= time_constants::END_OF32_BIT_EPOCH_YEAR) {
if (tm_year_from_base > time_constants::END_OF32_BIT_EPOCH_YEAR)
return time_utils::out_of_range();
return cpp::nullopt;
if (tm_out->tm_mon > 0)
return time_utils::out_of_range();
return cpp::nullopt;
if (tm_out->tm_mday > 19)
return time_utils::out_of_range();
return cpp::nullopt;
else if (tm_out->tm_mday == 19) {
if (tm_out->tm_hour > 3)
return time_utils::out_of_range();
return cpp::nullopt;
else if (tm_out->tm_hour == 3) {
if (tm_out->tm_min > 14)
return time_utils::out_of_range();
return cpp::nullopt;
else if (tm_out->tm_min == 14) {
if (tm_out->tm_sec > 7)
return time_utils::out_of_range();
return cpp::nullopt;
}
}
}
Expand Down Expand Up @@ -102,10 +102,10 @@ int64_t mktime_internal(const tm *tm_out) {

// TODO: https://github.com/llvm/llvm-project/issues/121962
// Need to handle timezone and update of tm_isdst.
int64_t seconds = tm_out->tm_sec +
tm_out->tm_min * time_constants::SECONDS_PER_MIN +
tm_out->tm_hour * time_constants::SECONDS_PER_HOUR +
total_days * time_constants::SECONDS_PER_DAY;
time_t seconds = tm_out->tm_sec +
tm_out->tm_min * time_constants::SECONDS_PER_MIN +
tm_out->tm_hour * time_constants::SECONDS_PER_HOUR +
total_days * time_constants::SECONDS_PER_DAY;
return seconds;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ static int64_t computeRemainingYears(int64_t daysPerYears,
//
// Compute the number of months from the remaining days. Finally, adjust years
// to be 1900 and months to be from January.
int64_t update_from_seconds(int64_t total_seconds, tm *tm) {
int64_t update_from_seconds(time_t total_seconds, tm *tm) {
// Days in month starting from March in the year 2000.
static const char daysInMonth[] = {31 /* Mar */, 30, 31, 30, 31, 31,
30, 31, 30, 31, 31, 29};
Expand All @@ -152,8 +152,7 @@ int64_t update_from_seconds(int64_t total_seconds, tm *tm) {
: INT_MAX * static_cast<int64_t>(
time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR);

time_t ts = static_cast<time_t>(total_seconds);
if (ts < time_min || ts > time_max)
if (total_seconds < time_min || total_seconds > time_max)
return time_utils::out_of_range();

int64_t seconds =
Expand Down
9 changes: 5 additions & 4 deletions libc/src/time/time_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ namespace time_utils {

// calculates the seconds from the epoch for tm_in. Does not update the struct,
// you must call update_from_seconds for that.
int64_t mktime_internal(const tm *tm_out);
cpp::optional<time_t> mktime_internal(const tm *tm_out);

// Update the "tm" structure's year, month, etc. members from seconds.
// "total_seconds" is the number of seconds since January 1st, 1970.
int64_t update_from_seconds(int64_t total_seconds, tm *tm);
int64_t update_from_seconds(time_t total_seconds, 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
Expand Down Expand Up @@ -84,7 +84,7 @@ LIBC_INLINE char *asctime(const tm *timeptr, char *buffer,
}

LIBC_INLINE tm *gmtime_internal(const time_t *timer, tm *result) {
int64_t seconds = *timer;
time_t seconds = *timer;
// Update the tm structure's year, month, day, etc. from seconds.
if (update_from_seconds(seconds, result) < 0) {
out_of_range();
Expand Down Expand Up @@ -329,7 +329,8 @@ class TMReader final {
}

LIBC_INLINE time_t get_epoch() const {
return static_cast<time_t>(mktime_internal(timeptr));
auto seconds = mktime_internal(timeptr);
return seconds ? *seconds : time_utils::out_of_range();
}

// returns the timezone offset in microwave time:
Expand Down
Loading