changeset 1690:7b6437d0e754

One more cleanup pass on hwclock factoring out common code.
author Rob Landley <rob@landley.net>
date Mon, 09 Feb 2015 17:47:29 -0600
parents bb4232e883d5
children 9b1cbc13dfdc
files toys/pending/hwclock.c
diffstat 1 files changed, 7 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/hwclock.c	Mon Feb 09 17:33:59 2015 -0600
+++ b/toys/pending/hwclock.c	Mon Feb 09 17:47:29 2015 -0600
@@ -98,10 +98,11 @@
     }
   }
 
-  if (toys.optflags & FLAG_w) {
+  if (toys.optflags & (FLAG_w|FLAG_t))
     if (gettimeofday(&timeval, 0)
         || (TT.utc ? gmtime_r : localtime_r)(&timeval.tv_sec, &tm)) goto bad;
 
+  if (toys.optflags & FLAG_w) {
     /* The value of tm_isdst will positive if daylight saving time is in effect,
      * zero if it is not and negative if the information is not available. 
      * todo: so why isn't this negative...? */
@@ -110,20 +111,13 @@
   } else if (toys.optflags & FLAG_s) {
     tzone.tz_minuteswest = timezone / 60 - 60 * daylight;
     timeval.tv_sec = time;
-    timeval.tv_usec = 0;
-    tzone.tz_dsttime = 0;
-    if (settimeofday(&timeval, &tzone)) goto bad;
+    timeval.tv_usec = 0; // todo: fixit
   } else if (toys.optflags & FLAG_t) {
-    if (gettimeofday(&timeval, NULL) || !localtime_r(&timeval.tv_sec, &tm))
-      goto bad;
     // Adjust seconds for timezone and daylight saving time
     // extern long timezone is defined in header sys/time.h
     tzone.tz_minuteswest = timezone / 60;
     if (tm.tm_isdst) tzone.tz_minuteswest -= 60;
-    if (gettimeofday(&timeval, NULL)) goto bad;
     if (!TT.utc) timeval.tv_sec += tzone.tz_minuteswest * 60;
-    tzone.tz_dsttime = 0;
-    if (settimeofday(&timeval, &tzone)) goto bad;
   } else {
     char *c = ctime(&time), *s = strrchr(c, '\n');
 
@@ -131,6 +125,10 @@
     // TODO: implement this.
     xprintf("%s  0.000000 seconds\n", c);
   }
+  if (toys.optflags & (FLAG_t|FLAG_s)) {
+    tzone.tz_dsttime = 0;
+    if (settimeofday(&timeval, &tzone)) goto bad;
+  }
 
   if (fd != -1) close(fd);