static void test_ts_format(void) { char buf[128]; const char* expect; timestampt ts; int status = ts_format(&ts, NULL, sizeof(buf)); CU_ASSERT_EQUAL(status, -1); ts = TS_NONE; expect = "TS_NONE"; status = ts_format(&ts, buf, sizeof(buf)); CU_ASSERT_EQUAL(status, strlen(expect)); CU_ASSERT_STRING_EQUAL(buf, expect); ts = TS_ZERO; expect = "TS_ZERO"; status = ts_format(&ts, buf, sizeof(buf)); CU_ASSERT_EQUAL(status, strlen(expect)); CU_ASSERT_STRING_EQUAL(buf, expect); ts = TS_ENDT; expect = "TS_ENDT"; status = ts_format(&ts, buf, sizeof(buf)); CU_ASSERT_EQUAL(status, strlen(expect)); CU_ASSERT_STRING_EQUAL(buf, expect); ts.tv_sec = 123456789; ts.tv_usec = 123456; expect = "19731129213309.123456"; status = ts_format(&ts, buf, sizeof(buf)); CU_ASSERT_EQUAL(status, strlen(expect)); CU_ASSERT_STRING_EQUAL(buf, expect); }
/* * Print the timestamp */ void ts_print(register const struct timeval *tvp) { register int s; struct tm *tm; time_t Time; static unsigned b_sec; static unsigned b_usec; int d_usec; int d_sec; switch (tflag) { case 0: /* Default */ s = (tvp->tv_sec + thiszone) % 86400; (void)printf("%s ", ts_format(s, tvp->tv_usec)); break; case 1: /* No time stamp */ break; case 2: /* Unix timeval style */ (void)printf("%u.%06u ", (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec); break; case 3: /* Microseconds since previous packet */ case 5: /* Microseconds since first packet */ if (b_sec == 0) { /* init timestamp for first packet */ b_usec = tvp->tv_usec; b_sec = tvp->tv_sec; } d_usec = tvp->tv_usec - b_usec; d_sec = tvp->tv_sec - b_sec; while (d_usec < 0) { d_usec += 1000000; d_sec--; } (void)printf("%s ", ts_format(d_sec, d_usec)); if (tflag == 3) { /* set timestamp for last packet */ b_sec = tvp->tv_sec; b_usec = tvp->tv_usec; } break; case 4: /* Default + Date*/ s = (tvp->tv_sec + thiszone) % 86400; Time = (tvp->tv_sec + thiszone) - s; tm = gmtime (&Time); if (!tm) printf("Date fail "); else printf("%04d-%02d-%02d %s ", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, ts_format(s, tvp->tv_usec)); break; } }
void ts_print(netdissect_options *ndo, register const struct timeval *tvp) { register int s; struct tm *tm; time_t Time; char buf[TS_BUF_SIZE]; static struct timeval tv_ref; struct timeval tv_result; int negative_offset; int nano_prec; switch (ndo->ndo_tflag) { case 0: /* Default */ s = (tvp->tv_sec + thiszone) % 86400; ND_PRINT((ndo, "%s ", ts_format(ndo, s, tvp->tv_usec, buf))); break; case 1: /* No time stamp */ break; case 2: /* Unix timeval style */ ND_PRINT((ndo, "%s ", ts_unix_format(ndo, tvp->tv_sec, tvp->tv_usec, buf))); break; case 3: /* Microseconds/nanoseconds since previous packet */ case 5: /* Microseconds/nanoseconds since first packet */ #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION switch (ndo->ndo_tstamp_precision) { case PCAP_TSTAMP_PRECISION_MICRO: nano_prec = 0; break; case PCAP_TSTAMP_PRECISION_NANO: nano_prec = 1; break; default: nano_prec = 0; break; } #else nano_prec = 0; #endif if (!(netdissect_timevalisset(&tv_ref))) tv_ref = *tvp; /* set timestamp for first packet */ negative_offset = netdissect_timevalcmp(tvp, &tv_ref, <); if (negative_offset) netdissect_timevalsub(&tv_ref, tvp, &tv_result, nano_prec); else netdissect_timevalsub(tvp, &tv_ref, &tv_result, nano_prec); ND_PRINT((ndo, (negative_offset ? "-" : " "))); ND_PRINT((ndo, "%s ", ts_format(ndo, tv_result.tv_sec, tv_result.tv_usec, buf))); 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((ndo, "Date fail ")); else ND_PRINT((ndo, "%04d-%02d-%02d %s ", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, ts_format(ndo, s, tvp->tv_usec, buf))); break; } }
/* * Print the timestamp */ void ts_print(register const struct timeval *tvp) { register int s; struct tm *tm; time_t Time; int d_usec; long d_sec; /* Default */ if (tflag == 0 || t0flag) { s = (tvp->tv_sec + thiszone) % 86400; (void)printf("%s ", ts_format(s, tvp->tv_usec)); } /* Unix timeval style */ if (tflag == 2 || t2flag) { (void)printf("%u.%06u ", (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec); } /* Microseconds since previous packet */ if (tflag == 3 || t3flag) { static unsigned long p_sec = 0; static unsigned p_usec = 0; if (p_sec == 0) { /* init timestamp for first packet */ p_usec = tvp->tv_usec; p_sec = tvp->tv_sec; } d_usec = tvp->tv_usec - p_usec; d_sec = tvp->tv_sec - p_sec; while (d_usec < 0) { d_usec += 1000000; d_sec--; } (void)printf("%s ", ts_format((int)d_sec, d_usec)); /* set timestamp for last packet */ p_sec = tvp->tv_sec; p_usec = tvp->tv_usec; } /* Default + Date */ if (tflag == 4 || t4flag) { s = (tvp->tv_sec + thiszone) % 86400; Time = (tvp->tv_sec + thiszone) - s; tm = gmtime (&Time); if (!tm) printf("Date fail "); else printf("%04d-%02d-%02d %s ", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, ts_format(s, tvp->tv_usec)); } /* Microseconds since first packet */ if (tflag == 5 || t5flag) { static unsigned long b_sec = 0; static unsigned b_usec = 0; /* init timestamp for first packet */ if (b_sec == 0 && b_usec == 0) { b_usec = tvp->tv_usec; b_sec = tvp->tv_sec; } d_usec = tvp->tv_usec - b_usec; d_sec = tvp->tv_sec - b_sec; while (d_usec < 0) { d_usec += 1000000; d_sec--; } (void)printf("%s ", ts_format((int)d_sec, d_usec)); } }