diff options
author | Alejandro Colomar <alx@kernel.org> | 2024-01-29 15:34:20 +0100 |
---|---|---|
committer | Serge Hallyn <serge@hallyn.com> | 2024-03-14 16:30:46 -0500 |
commit | 8fcf6cccff64cf270b8d4596dca3e6fd763f846c (patch) | |
tree | 65f40f199d36dd4280610a73e4bdb47cea356c76 | |
parent | 8fee869e9ad8530f79f51eabec9eac87cb982196 (diff) |
lib/time/day_to_str.[ch]: day_to_str(): Accept a day instead of a date, and rename function
It was always being called with 'day * DAY', so do that internally and
simplify. This grabs some code from print_day_as_date().
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r-- | lib/time/day_to_str.c | 3 | ||||
-rw-r--r-- | lib/time/day_to_str.h | 31 |
2 files changed, 19 insertions, 15 deletions
diff --git a/lib/time/day_to_str.c b/lib/time/day_to_str.c index 4acfe907..e3e42210 100644 --- a/lib/time/day_to_str.c +++ b/lib/time/day_to_str.c @@ -1,4 +1,5 @@ // SPDX-FileCopyrightText: 2021-2024, Alejandro Colomar <alx@kernel.org> +// SPDX-FileCopyrightText: 2024, Tobias Stoeckmann <tobias@stoeckmann.org> // SPDX-License-Identifier: BSD-3-Clause @@ -7,4 +8,4 @@ #include "time/day_to_str.h" -extern inline void date_to_str(size_t size, char buf[size], long date); +extern inline void day_to_str(size_t size, char buf[size], long day); diff --git a/lib/time/day_to_str.h b/lib/time/day_to_str.h index 2689794b..6aa855ac 100644 --- a/lib/time/day_to_str.h +++ b/lib/time/day_to_str.h @@ -1,7 +1,6 @@ -/* - * SPDX-FileCopyrightText: 2021-2023, Alejandro Colomar <alx@kernel.org> - * SPDX-License-Identifier: BSD-3-Clause - */ +// SPDX-FileCopyrightText: 2021-2024, Alejandro Colomar <alx@kernel.org> +// SPDX-FileCopyrightText: 2024, Tobias Stoeckmann <tobias@stoeckmann.org> +// SPDX-License-Identifier: BSD-3-Clause #ifndef SHADOW_INCLUDE_LIB_TIME_DAY_TO_STR_H_ @@ -17,32 +16,36 @@ #include "string/strtcpy.h" -#define DAY_TO_STR(str, day) date_to_str(NITEMS(str), str, day * DAY) +#define DAY_TO_STR(str, day) day_to_str(NITEMS(str), str, day) -inline void date_to_str(size_t size, char buf[size], long date); +inline void day_to_str(size_t size, char buf[size], long day); inline void -date_to_str(size_t size, char buf[size], long date) +day_to_str(size_t size, char buf[size], long day) { - time_t t; + time_t date; const struct tm *tm; - t = date; - if (date < 0) { - (void) strtcpy(buf, "never", size); + if (day < 0) { + strtcpy(buf, "never", size); return; } - tm = gmtime(&t); + if (__builtin_mul_overflow(day, DAY, &date)) { + strtcpy(buf, "future", size); + return; + } + + tm = gmtime(&date); if (tm == NULL) { - (void) strtcpy(buf, "future", size); + strtcpy(buf, "future", size); return; } if (strftime(buf, size, "%Y-%m-%d", tm) == 0) - (void) strtcpy(buf, "future", size); + strtcpy(buf, "future", size); } |