---
* [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.
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;
}
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 */ ;
printf("\n");
free(ts_str);
+ if (p_SNTP_PRETEND_TIME)
+ return 0;
+
if (ENABLED_OPT(SETTOD) || ENABLED_OPT(ADJTIME))
return set_time(offset);
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);
#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
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;
}