-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-libc Author: Leslie (wsehjk) ChangesThis pr is to resovle #126948 Full diff: https://github.com/llvm/llvm-project/pull/132231.diff 3 Files Affected:
diff --git a/libc/src/time/mktime.cpp b/libc/src/time/mktime.cpp
index fc05ff2930434..4c035a15bfe23 100644
--- a/libc/src/time/mktime.cpp
+++ b/libc/src/time/mktime.cpp
@@ -15,13 +15,14 @@
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
- int64_t seconds = time_utils::mktime_internal(tm_out);
+ bool out_of_range_flag = true;
+ time_t seconds = time_utils::mktime_internal(tm_out, &out_of_range_flag);
// Update the tm structure's year, month, day, etc. from seconds.
- if (time_utils::update_from_seconds(seconds, tm_out) < 0)
+ if (out_of_range_flag || 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
diff --git a/libc/src/time/time_utils.cpp b/libc/src/time/time_utils.cpp
index 3ccb2dd934967..f4f06e276abde 100644
--- a/libc/src/time/time_utils.cpp
+++ b/libc/src/time/time_utils.cpp
@@ -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) {
+time_t mktime_internal(const tm *tm_out, bool* out_of_range_flag) {
// 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;
@@ -26,21 +26,33 @@ int64_t mktime_internal(const tm *tm_out) {
// 32-bit end-of-the-world is 03:14:07 UTC on 19 January 2038.
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();
- if (tm_out->tm_mon > 0)
- return time_utils::out_of_range();
- if (tm_out->tm_mday > 19)
- return time_utils::out_of_range();
+ if (tm_year_from_base > time_constants::END_OF32_BIT_EPOCH_YEAR) {
+ *out_of_range_flag = true;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
+ if (tm_out->tm_mon > 0) {
+ *out_of_range_flag = true;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
+ if (tm_out->tm_mday > 19) {
+ *out_of_range_flag = true;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
else if (tm_out->tm_mday == 19) {
- if (tm_out->tm_hour > 3)
- return time_utils::out_of_range();
+ if (tm_out->tm_hour > 3) {
+ *out_of_range_flag = true;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
else if (tm_out->tm_hour == 3) {
- if (tm_out->tm_min > 14)
- return time_utils::out_of_range();
+ if (tm_out->tm_min > 14) {
+ *out_of_range_flag = true;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
else if (tm_out->tm_min == 14) {
- if (tm_out->tm_sec > 7)
- return time_utils::out_of_range();
+ if (tm_out->tm_sec > 7) {
+ *out_of_range_flag = true;
+ return time_constants::OUT_OF_RANGE_RETURN_VALUE;
+ }
}
}
}
@@ -66,7 +78,7 @@ int64_t mktime_internal(const tm *tm_out) {
bool tm_year_is_leap = time_utils::is_leap_year(tm_year_from_base);
// Calculate total number of days based on the month and the day (tm_mday).
- int64_t total_days = tm_out->tm_mday - 1;
+ int total_days = tm_out->tm_mday - 1;
for (int64_t i = 0; i < month; ++i)
total_days += time_constants::NON_LEAP_YEAR_DAYS_IN_MONTH[i];
// Add one day if it is a leap year and the month is after February.
@@ -102,7 +114,7 @@ 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 +
+ 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;
@@ -136,7 +148,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};
@@ -152,7 +164,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);
+ time_t ts = total_seconds;
if (ts < time_min || ts > time_max)
return time_utils::out_of_range();
diff --git a/libc/src/time/time_utils.h b/libc/src/time/time_utils.h
index 68eaac8c04f11..ac5d48815e698 100644
--- a/libc/src/time/time_utils.h
+++ b/libc/src/time/time_utils.h
@@ -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);
+time_t mktime_internal(const tm *tm_out, bool* out_of_range_flag);
// 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
@@ -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();
@@ -329,7 +329,8 @@ class TMReader final {
}
LIBC_INLINE time_t get_epoch() const {
- return static_cast<time_t>(mktime_internal(timeptr));
+ bool out_of_range_flag = true;
+ return mktime_internal(timeptr, &out_of_range_flag);
}
// returns the timezone offset in microwave time:
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git clang-format HEAD~1
should fix whitespace automatically. One nit.
Co-authored-by: Joseph Huber <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just add the [libc] tag to the commit title
@wsehjk Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
We're seeing this patch fail to build w/ ToT clang. Looks like its missing a static_cast, so #132947 should fix it. Posting details so others don't duplicate work. Error:
|
Thanks! |
This pr is to close #126948