[libc-commits] [libc] [libc] implement localtime (PR #110363)
Petr Hosek via libc-commits
libc-commits at lists.llvm.org
Mon Oct 14 16:59:03 PDT 2024
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/110363 at github.com>
================
@@ -129,6 +154,28 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
if (years > INT_MAX || years < INT_MIN)
return time_utils::out_of_range();
+ char timezone[TimeConstants::TIMEZONE_SIZE];
+ char *env_tz = getenv("TZ");
+ FILE *fp = NULL;
+ if (env_tz) {
+ strncpy(timezone, env_tz, sizeof(timezone));
+ timezone[sizeof(timezone) - 1] = '\0';
+ } else {
+ fp = fopen("/etc/timezone", "rb");
+ if (fp == NULL) {
+ return time_utils::out_of_range();
+ }
+
+ acquire_file(fp, timezone, TimeConstants::TIMEZONE_SIZE);
+ }
+
+ if (fp != NULL && file_usage == 0) {
+ release_file(fp, timezone);
+ return time_utils::out_of_range();
+ }
+
+ int offset = timezone::get_timezone_offset(timezone);
----------------
petrhosek wrote:
This logic should be moved into an internal utlity function that could be implemented differently for each platform. For example, on baremetal we don't have any filesystem and `fopen` isn't defined at all. Ideally, this function should return a struct type rather than a string so on platforms like baremetal we can return statically initialized global and avoid having to parse the string on every call (even in the Linux implementation you probably want to memoize it for performance reasons).
https://github.com/llvm/llvm-project/pull/110363
More information about the libc-commits
mailing list