This is an automated email from the ASF dual-hosted git repository.

michallenc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit dc7a13df6406d8b7c4cd1b26946036beb87ed339
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 20 17:44:09 2026 +0200

    libs/libc/time: Read the local time zone file when TZ is not set.
    
    Without the environment variable TZ, tzset() returned at once, so
    localtime() always gave UTC: the TZDEFAULT name "localtime" of the
    tzcode was never used.  POSIX leaves the time zone without TZ to the
    implementation, and the systems with the IANA TZ database read the file
    /etc/localtime, a link to the file of the local zone.
    
    Add CONFIG_LIBC_TZDEFAULT: the file that tzset() loads when TZ is not
    set, an absolute path or a name in CONFIG_LIBC_TZDIR.  It is empty by
    default, which keeps UTC without TZ, so existing configurations do not
    change.  When the file cannot be read, the local time is UTC.  As in the
    tzcode, g_lcl_isset is -1 while the default zone is in use, so that a
    later tzset() without TZ does not load the file again, and a TZ that is
    set later still takes effect.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/time/Kconfig         | 10 ++++++++++
 libs/libc/time/lib_localtime.c | 35 ++++++++++++++++++++++++++++-------
 2 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/libs/libc/time/Kconfig b/libs/libc/time/Kconfig
index 7b233f8cfd2..df3e8c14a27 100644
--- a/libs/libc/time/Kconfig
+++ b/libs/libc/time/Kconfig
@@ -86,6 +86,16 @@ config LIBC_TZDIR
                This is the full path to the location where the TZ database is 
expected
                to be found.
 
+config LIBC_TZDEFAULT
+       string "Local time zone file"
+       default ""
+       ---help---
+               The time zone file that localtime() and tzset() use when the
+               environment variable TZ is not set: an absolute path, or a name 
in
+               LIBC_TZDIR.  With the IANA layout of most systems this is
+               "/etc/localtime", a link to a file in LIBC_TZDIR.  Empty: 
without
+               TZ the local time is UTC.
+
 config LIBC_ZONEINFO
        bool "TZ database"
        default n
diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c
index 8953e6571d8..7a172d337ed 100644
--- a/libs/libc/time/lib_localtime.c
+++ b/libs/libc/time/lib_localtime.c
@@ -80,7 +80,11 @@
 /* Time zone files */
 
 #define TZ_MAGIC            "TZif"
-#define TZDEFAULT           "localtime"
+#ifdef CONFIG_LIBC_TZDEFAULT
+#  define TZDEFAULT         CONFIG_LIBC_TZDEFAULT
+#else
+#  define TZDEFAULT         ""
+#endif
 #define TZDEFRULES          "posixrules"
 
 /* In the current implementation, "tzset()" refuses to deal with files that
@@ -2786,16 +2790,30 @@ static int zoneinit(FAR const char *name)
 void tzset(void)
 {
   FAR const char *name;
+  int lcl;
+
+  /* Without TZ the local time zone is the file TZDEFAULT, if there is one
+   * (g_lcl_isset is -1 then), else UTC.
+   */
 
   name = getenv("TZ");
   if (name == NULL)
     {
-      return;
-    }
+      if (TZDEFAULT[0] == '\0' || g_lcl_isset < 0)
+        {
+          return;
+        }
 
-  if (g_lcl_isset > 0 && strcmp(g_lcl_tzname, name) == 0)
+      lcl = -1;
+    }
+  else
     {
-      return;
+      if (g_lcl_isset > 0 && strcmp(g_lcl_tzname, name) == 0)
+        {
+          return;
+        }
+
+      lcl = 1;
     }
 
   if (nxrmutex_is_hold(&g_lcl_lock))
@@ -2819,11 +2837,14 @@ void tzset(void)
       zoneinit("");
     }
 
-  strlcpy(g_lcl_tzname, name, sizeof(g_lcl_tzname));
+  if (lcl > 0)
+    {
+      strlcpy(g_lcl_tzname, name, sizeof(g_lcl_tzname));
+    }
 
 tzname:
   settzname();
-  g_lcl_isset = 1;
+  g_lcl_isset = lcl;
   tz_unlock(&g_lcl_lock);
 }
 

Reply via email to