]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Fix local time printing
authorFrancois-Xavier Le Bail <[email protected]>
Tue, 7 Aug 2018 15:11:43 +0000 (17:11 +0200)
committerFrancois-Xavier Le Bail <[email protected]>
Tue, 7 Aug 2018 15:37:36 +0000 (17:37 +0200)
Using seconds offset from GMT to local time to compute local time give
errors when printing times outside the same daylight saving time period.

Use the localtime() function for default and -tttt cases.
Use the gmtime() function for -ttt and -ttttt cases.

Rename ts_hmsfrac_print() to ts_date_hmsfrac_print().
Remove some variables now useless.
Update some comments.

netdissect.h
print.c
print.h
tcpdump.c
util-print.c

index 93fa8be6514405333709df7b6bf5e814181ba5e3..7ebbfab86a32dabb243f573dde12ab231990e997 100644 (file)
@@ -119,7 +119,6 @@ typedef unsigned char nd_byte;
 #include "ip.h" /* struct ip for nextproto4_cksum() */
 #include "ip6.h" /* struct ip6 for nextproto6_cksum() */
 
-extern int32_t thiszone;       /* seconds offset from gmt to local time */
 /* invalid string to print '(invalid)' for malformed or corrupted packets */
 extern const char istr[];
 
diff --git a/print.c b/print.c
index 7460b777035e5efe79111f9e2234c1621622e9fb..c6e354309ea8a4fa175c9e4dac273e0a1681d294 100644 (file)
--- a/print.c
+++ b/print.c
@@ -243,11 +243,9 @@ static int ndo_printf(netdissect_options *ndo,
                     PRINTFLIKE(2, 3);
 
 void
-init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask,
-          uint32_t timezone_offset)
+init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
 {
 
-       thiszone = timezone_offset;
        init_addrtoname(ndo, localnet, mask);
        init_checksum();
 }
diff --git a/print.h b/print.h
index 9632694e8612e51158b87b2046b69165500b6db1..70ea9f8e821e781abe64aec2fd609908fa245294 100644 (file)
--- a/print.h
+++ b/print.h
@@ -28,8 +28,7 @@
 #ifndef print_h
 #define print_h
 
-void   init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask,
-           uint32_t timezone_offset);
+void   init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask);
 
 int    has_printer(int type);
 
index 21ead64c33c781a7e93299150def29bc4a631b96..65defd0d652c1b58e8f4ccf465ec29cb021851f9 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -132,7 +132,6 @@ The Regents of the University of California.  All rights reserved.\n";
 #include "interface.h"
 #include "addrtoname.h"
 #include "machdep.h"
-#include "gmt2local.h"
 #include "pcap-missing.h"
 #include "ascii_strcasecmp.h"
 
@@ -1412,7 +1411,6 @@ main(int argc, char **argv)
 {
        int cnt, op, i;
        bpf_u_int32 localnet = 0, netmask = 0;
-       int timezone_offset = 0;
        char *cp, *infile, *cmdbuf, *device, *RFileName, *VFileName, *WFileName;
        char *endp;
        pcap_handler callback;
@@ -1869,14 +1867,11 @@ main(int argc, char **argv)
        switch (ndo->ndo_tflag) {
 
        case 0: /* Default */
-       case 4: /* Default + Date*/
-               timezone_offset = gmt2local(0);
-               break;
-
        case 1: /* No time stamp */
        case 2: /* Unix timeval style */
-       case 3: /* Microseconds since previous packet */
-        case 5: /* Microseconds since first packet */
+       case 3: /* Microseconds/nanoseconds since previous packet */
+       case 4: /* Date + Default */
+       case 5: /* Microseconds/nanoseconds since first packet */
                break;
 
        default: /* Not supported */
@@ -2138,7 +2133,7 @@ main(int argc, char **argv)
                capdns = capdns_setup();
 #endif /* HAVE_CASPER */
 
-       init_print(ndo, localnet, netmask, timezone_offset);
+       init_print(ndo, localnet, netmask);
 
 #ifndef _WIN32
        (void)setsignal(SIGPIPE, cleanup);
index 7f23a4b037b68f0081e41be537d56ba57107d887..06844898d9caa28a4e592689fe91ce2fcdfd89b9 100644 (file)
 #include "ascii_strcasecmp.h"
 #include "timeval-operations.h"
 
-int32_t thiszone;              /* seconds offset from gmt to local time */
 /* invalid string to print '(invalid)' for malformed or corrupted packets */
 const char istr[] = " (invalid)";
 
 #define TOKBUFSIZE 128
 
+
+enum date_flag { WITHOUT_DATE = 0, WITH_DATE = 1 };
+enum time_flag { UTC_TIME = 0, LOCAL_TIME = 1 };
+
 /*
  * Print out a character, filtering out the non-printable ones
  */
@@ -229,32 +232,50 @@ nd_printzp(netdissect_options *ndo,
 }
 
 /*
- * Print the timestamp as HH:MM:SS.FRAC.
+ * Print the timestamp as [YY:MM:DD] HH:MM:SS.FRAC.
+ *   if time_flag == LOCAL_TIME print local time else UTC/GMT time
+ *   if date_flag == WITH_DATE print YY:MM:DD before HH:MM:SS.FRAC
  */
 static void
-ts_hmsfrac_print(netdissect_options *ndo, int sec, int usec)
+ts_date_hmsfrac_print(netdissect_options *ndo, int sec, int usec,
+                     enum date_flag date_flag, enum time_flag time_flag)
 {
+       time_t Time = sec;
+       struct tm *tm;
+       char timestr[32];
+
+       if (time_flag == LOCAL_TIME)
+               tm = localtime(&Time);
+       else
+               tm = gmtime(&Time);
+
+       if (!tm) {
+               ND_PRINT("[Error converting time]");
+               return;
+       }
+       if (date_flag == WITH_DATE)
+               strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tm);
+       else
+               strftime(timestr, sizeof(timestr), "%H:%M:%S", tm);
+       ND_PRINT("%s", timestr);
+
 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
        switch (ndo->ndo_tstamp_precision) {
 
        case PCAP_TSTAMP_PRECISION_MICRO:
-               ND_PRINT("%02d:%02d:%02d.%06u", sec / 3600, (sec % 3600) / 60,
-                   sec % 60, usec);
+               ND_PRINT(".%06u", usec);
                break;
 
        case PCAP_TSTAMP_PRECISION_NANO:
-               ND_PRINT("%02d:%02d:%02d.%09u", sec / 3600, (sec % 3600) / 60,
-                   sec % 60, usec);
+               ND_PRINT(".%09u", usec);
                break;
 
        default:
-               ND_PRINT("%02d:%02d:%02d.{unknown}", sec / 3600, (sec % 3600) / 60,
-                   sec % 60);
+               ND_PRINT(".{unknown}");
                break;
        }
 #else
-       ND_PRINT("%02d:%02d:%02d.%06u", sec / 3600, (sec % 3600) / 60,
-           sec % 60, usec);
+       ND_PRINT(".%06u", usec);
 #endif
 }
 
@@ -291,9 +312,6 @@ void
 ts_print(netdissect_options *ndo,
          const struct timeval *tvp)
 {
-       int s;
-       struct tm *tm;
-       time_t Time;
        static struct timeval tv_ref;
        struct timeval tv_result;
        int negative_offset;
@@ -302,8 +320,8 @@ ts_print(netdissect_options *ndo,
        switch (ndo->ndo_tflag) {
 
        case 0: /* Default */
-               s = (tvp->tv_sec + thiszone) % 86400;
-               ts_hmsfrac_print(ndo, s, tvp->tv_usec);
+               ts_date_hmsfrac_print(ndo, tvp->tv_sec, tvp->tv_usec,
+                                     WITHOUT_DATE, LOCAL_TIME);
                ND_PRINT(" ");
                break;
 
@@ -342,25 +360,18 @@ ts_print(netdissect_options *ndo,
                        netdissect_timevalsub(tvp, &tv_ref, &tv_result, nano_prec);
 
                ND_PRINT((negative_offset ? "-" : " "));
-               ts_hmsfrac_print(ndo, tv_result.tv_sec, tv_result.tv_usec);
+               ts_date_hmsfrac_print(ndo, tv_result.tv_sec, tv_result.tv_usec,
+                                     WITHOUT_DATE, UTC_TIME);
                ND_PRINT(" ");
 
                 if (ndo->ndo_tflag == 3)
                        tv_ref = *tvp; /* set timestamp for previous packet */
                break;
 
-       case 4: /* Default + Date */
-               s = (tvp->tv_sec + thiszone) % 86400;
-               Time = (tvp->tv_sec + thiszone) - s;
-               tm = gmtime (&Time);
-               if (!tm)
-                       ND_PRINT("Date fail ");
-               else {
-                       ND_PRINT("%04d-%02d-%02d ",
-                           tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
-                       ts_hmsfrac_print(ndo, s, tvp->tv_usec);
-                       ND_PRINT(" ");
-               }
+       case 4: /* Date + Default */
+               ts_date_hmsfrac_print(ndo, tvp->tv_sec, tvp->tv_usec,
+                                     WITH_DATE, LOCAL_TIME);
+               ND_PRINT(" ");
                break;
        }
 }