]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1743] Display timezone offset when showing time for sntp in the local timezone.
authorHarlan Stenn <stenn@ntp.org>
Wed, 8 Dec 2010 07:34:17 +0000 (02:34 -0500)
committerHarlan Stenn <stenn@ntp.org>
Wed, 8 Dec 2010 07:34:17 +0000 (02:34 -0500)
bk: 4cff34f9pmtl-zCnAmh_mnil7vEJCA

ChangeLog
sntp/configure.ac
sntp/main.c
sntp/utilities.c
sntp/utilities.h

index f7cffd2c272b0a33dc37f6665ce3c842e396d5ac..ae8d367f7bd0511a12b5989b8b7bd16a84c39724 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,8 @@
 ---
 
 * [Bug 1742] Fix a typo in an error message in the "build" script.
+* [Bug 1743] Display timezone offset when showing time for sntp in the
+  local timezone.
 * Clean up m4 quoting in configure.ac, *.m4 files, resolving
   intermittent AC_LANG_PROGRAM possibly undefined errors.
 * Clean up the SNTP documentation.
index 0b34eed7c5a71936fe2c8642c58b7396779890d0..d05d536001a518ff9065502e202e066afe5ce846 100644 (file)
@@ -237,6 +237,7 @@ AC_HEADER_TIME
 AC_HEADER_STDBOOL
 AC_C_CONST
 AC_TYPE_SIZE_T
+AC_CHECK_SIZEOF([time_t])
 
 AC_C_INLINE
 
index ef7f11457de7a0cbf8856ce92a60e5aaa643b6b2..6ce262536bf1640a4810617526dbed32b7b4a757 100644 (file)
@@ -199,6 +199,8 @@ handle_pkt (
        char *hostname = NULL, *ref, *ts_str = NULL;
        double offset, precision, root_dispersion;
        char addr_buf[INET6_ADDRSTRLEN];
+       char *p_SNTP_PRETEND_TIME;
+       time_t pretend_time;
 
        if(rpktl > 0)
                sw_case = 1;
@@ -243,9 +245,21 @@ handle_pkt (
                }
 
                GETTIMEOFDAY(&tv_dst, (struct timezone *)NULL);
-               tv_dst.tv_sec += JAN_1970;
 
-               offset_calculation(rpkt, rpktl, &tv_dst, &offset, &precision, &root_dispersion);
+               p_SNTP_PRETEND_TIME = getenv("SNTP_PRETEND_TIME");
+               if (p_SNTP_PRETEND_TIME) {
+#if SIZEOF_TIME_T == 4
+                       sscanf(p_SNTP_PRETEND_TIME, "%ld", &pretend_time);
+#elif SIZEOF_TIME_T == 8
+                       sscanf(p_SNTP_PRETEND_TIME, "%lld", &pretend_time);
+#else
+# include "GRONK: unexpected value for SIZEOF_TIME_T"
+#endif
+                       tv_dst.tv_sec = pretend_time;
+               }
+
+               offset_calculation(rpkt, rpktl, &tv_dst, &offset,
+                                  &precision, &root_dispersion);
 
                for (digits = 0; (precision *= 10.) < 1.; ++digits)
                        /* empty */ ;
@@ -262,6 +276,9 @@ handle_pkt (
                printf("\n");
                free(ts_str);
 
+               if (p_SNTP_PRETEND_TIME)
+                       return 0;
+
                if (ENABLED_OPT(SETTOD) || ENABLED_OPT(ADJTIME))
                        return set_time(offset); 
 
@@ -325,6 +342,7 @@ offset_calculation (
        L_SUB(&tmp, &p_org);
        LFPTOD(&tmp, t21);
        TVTOTS(tv_dst, &dst);
+       dst.l_ui += JAN_1970;
        tmp = p_xmt;
        L_SUB(&tmp, &dst);
        LFPTOD(&tmp, t34);
index 6180ecb7ac90f9eb549747b26a03fff2d4bd6edc..e19ee1c354ef8f0a05e011970354ea497869c781 100644 (file)
@@ -1,5 +1,6 @@
 #include <config.h>
 #include "utilities.h"
+#include <assert.h>
 
 /* Display a NTP packet in hex with leading address offset 
  * e.g. offset: value, 0: ff 1: fe ... 255: 00
@@ -139,34 +140,47 @@ ss_to_str (
 
        return buf;
 }
-
-/* Converts a struct tv to a date string
+/*
+ * Converts a struct tv to a date string
  */
 char *
-tv_to_str (
-               struct timeval *tv
-         )
+tv_to_str(
+       const struct timeval *tv
+       )
 {
-       static const char *month_names[] = {
-               "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-       };
-
-       char *buf = (char *) emalloc(sizeof(char) * 48);
-       time_t cur_time = time(NULL);
-       struct tm *tm_ptr;
-
-       tm_ptr = localtime(&cur_time);
-
-
-       snprintf(buf, 48, "%i %s %.2d %.2d:%.2d:%.2d.%.6d", 
-                       tm_ptr->tm_year + 1900,
-                       month_names[tm_ptr->tm_mon],
-                       tm_ptr->tm_mday,
-                       tm_ptr->tm_hour,
-                       tm_ptr->tm_min,
-                       tm_ptr->tm_sec,
-                       (int)tv->tv_usec);
+       const size_t bufsize = 48;
+       char *buf;
+       time_t gmt_time, local_time;
+       struct tm *p_tm_local;
+       int hh, mm, lto;
+
+       /*
+        * convert to struct tm in UTC, then intentionally feed
+        * that tm to mktime() which expects local time input, to
+        * derive the offset from UTC to local time.
+        */
+       gmt_time = tv->tv_sec;
+       local_time = mktime(gmtime(&gmt_time));
+       p_tm_local = localtime(&gmt_time);
+
+       /* Local timezone offsets should never cause an overflow.  Yeah. */
+       lto = difftime(local_time, gmt_time);
+       lto /= 60;
+       hh = lto / 60;
+       mm = abs(lto % 60);
+
+       buf = emalloc(bufsize);
+       snprintf(buf, bufsize,
+                "%d-%.2d-%.2d %.2d:%.2d:%.2d.%.6d (%+03d%02d)",
+                p_tm_local->tm_year + 1900,
+                p_tm_local->tm_mon + 1,
+                p_tm_local->tm_mday,
+                p_tm_local->tm_hour,
+                p_tm_local->tm_min,
+                p_tm_local->tm_sec,
+                (int)tv->tv_usec,
+                hh,
+                mm);
 
        return buf;
 }
index 79fd77477758578acb779c84c9ab44d687e93ef4..a122f868d96198b9ce11fe51059c61f9adbdb546 100644 (file)
@@ -20,6 +20,6 @@ void l_fp_output_dec (l_fp *ts, FILE *output);
 
 char *addrinfo_to_str (struct addrinfo *addr);
 char *ss_to_str (sockaddr_u *saddr);
-char *tv_to_str (struct timeval *tv);
+char *tv_to_str (const struct timeval *tv);
 
 #endif