[libc-commits] [libc] [libc] implement localtime (PR #110363)

Зишан Мирза via libc-commits libc-commits at lists.llvm.org
Mon Oct 14 22:28:47 PDT 2024


================
@@ -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);
----------------
zimirza wrote:

Sure, I am currently refactoring the logic by parsing `/etc/localtime` by using the `TZif` file format.

https://github.com/llvm/llvm-project/pull/110363


More information about the libc-commits mailing list