From: Harlan Stenn Date: Wed, 8 Dec 2010 07:34:17 +0000 (-0500) Subject: [Bug 1743] Display timezone offset when showing time for sntp in the local timezone. X-Git-Tag: NTP_4_2_7P92~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d65be755460867d58008f7c73bd5a334890aa942;p=thirdparty%2Fntp.git [Bug 1743] Display timezone offset when showing time for sntp in the local timezone. bk: 4cff34f9pmtl-zCnAmh_mnil7vEJCA --- diff --git a/ChangeLog b/ChangeLog index f7cffd2c2..ae8d367f7 100644 --- 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. diff --git a/sntp/configure.ac b/sntp/configure.ac index 0b34eed7c..d05d53600 100644 --- a/sntp/configure.ac +++ b/sntp/configure.ac @@ -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 diff --git a/sntp/main.c b/sntp/main.c index ef7f11457..6ce262536 100644 --- a/sntp/main.c +++ b/sntp/main.c @@ -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); diff --git a/sntp/utilities.c b/sntp/utilities.c index 6180ecb7a..e19ee1c35 100644 --- a/sntp/utilities.c +++ b/sntp/utilities.c @@ -1,5 +1,6 @@ #include #include "utilities.h" +#include /* 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; } diff --git a/sntp/utilities.h b/sntp/utilities.h index 79fd77477..a122f868d 100644 --- a/sntp/utilities.h +++ b/sntp/utilities.h @@ -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