I promised I'd clean this up months ago. Better late than never.
---
 toys/posix/date.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)
From 8d1314c31de2235a5b44eb48bf240b65b5cf28a2 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Thu, 11 Apr 2019 17:47:47 -0700
Subject: [PATCH] date: use xtzset.

I promised I'd clean this up months ago. Better late than never.
---
 toys/posix/date.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)

diff --git a/toys/posix/date.c b/toys/posix/date.c
index a1762854..f50b717d 100644
--- a/toys/posix/date.c
+++ b/toys/posix/date.c
@@ -72,16 +72,11 @@ static void parse_date(char *str, time_t *t)
     while (isspace(*str)) str++;
 
     // Switch $TZ.
-    old_tz = getenv("TZ");
-    setenv("TZ", new_tz, 1);
-    tzset();
+    old_tz = xtzset(new_tz);
   }
   time(t);
   xparsedate(str, t, &TT.nano, 1);
-  if (new_tz) {
-    if (old_tz) setenv("TZ", old_tz, 1);
-    else unsetenv("TZ");
-  }
+  if (new_tz) xtzset(old_tz);
 }
 
 // Print strftime plus %N escape(s). note: modifies fmt for %N
@@ -116,14 +111,10 @@ static void puts_time(char *fmt, struct tm *tm)
 void date_main(void)
 {
   char *setdate = *toys.optargs, *format_string = "%a %b %e %H:%M:%S %Z %Y",
-    *tz = NULL;
+    *old_tz = NULL;
   time_t t;
 
-  if (FLAG(u)) {
-    tz = getenv("TZ");
-    setenv("TZ", "UTC", 1);
-    tzset();
-  }
+  if (FLAG(u)) old_tz = xtzset("UTC");
 
   if (TT.d) {
     if (TT.D) {
@@ -164,11 +155,5 @@ void date_main(void)
 
   puts_time(format_string, localtime(&t));
 
-  if (FLAG(u)) {
-    if (tz) setenv("TZ", tz, 1);
-    else unsetenv("TZ");
-    tzset();
-  }
-
-  return;
+  if (FLAG(u)) xtzset(old_tz);
 }
-- 
2.21.0.392.gf8f6787159e-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to