1. gmt2local.c:
2. int32_t
3. gmt2local(time_t t)
4. {
5. register int dt, dir;
6. register struct tm *gmt, *loc;
7. struct tm sgmt;
8.
9. if (t == 0)
10. t = time(NULL);
11. gmt = &sgmt;
12. *gmt = *gmtime(&t);
13. loc = localtime(&t);
14. dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
15. (loc->tm_min - gmt->tm_min) * 60;
16.
17. /*
18. * If the year or julian day is different, we span 00:00 GMT
19. * and must add or subtract a day. Check the year first to
20. * avoid problems when the julian day wraps.
21. */
22. dir = loc->tm_year - gmt->tm_year;
23. if (dir == 0)
24. dir = loc->tm_yday - gmt->tm_yday;
25. dt += dir * 24 * 60 * 60;
26.
27. return (dt);
28. }
29.
30.
31. tcpdump.c
32. int tflag=1;
33.
34. cast 't' //如果在参数中有指定
35. --tflag;
36. break;
37.
38. if (tflag > 0)
39. thiszone = gmt2local(0);
40.
41. util.c
42. void
43. ts_print(register const struct timeval *tvp)
44. {
45. register int s;
46. struct tm *tm;
47. time_t Time;
48. static unsigned b_sec;
49. static unsigned b_usec;
50.
51. switch(tflag) {
52. case 1: /* Default */
53. s = (tvp->tv_sec + thiszone) % 86400;
54. (void)printf("%02d:%02d:%02d.%06u ",
55. s / 3600, (s % 3600) / 60, s % 60,
56. (unsigned)tvp->tv_usec);
57. break;
58. case -1: /* Unix timeval style */
59. (void)printf("%u.%06u ",
60. (unsigned)tvp->tv_sec,
61. (unsigned)tvp->tv_usec);
62. break;
63. case -2:
64. if (b_sec == 0) {
65. printf("000000 ");
66. } else {
67. int d_usec = tvp->tv_usec - b_usec;
68. int d_sec = tvp->tv_sec - b_sec;
69.
70. while (d_usec tv_sec;
79. b_usec = tvp->tv_usec;
80. break;
81. case -3: /* Default + Date*/
82. s = (tvp->tv_sec + thiszone) % 86400;
83. Time = (tvp->tv_sec + thiszone) - s;
84. tm = gmtime (&Time);
85. if (!tm)
86. printf("Date fail ");
87. else
88. printf("%04d-%02d-%02d ",
89. tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
90. printf("%02d:%02d:%02d.%06u ",
91. s / 3600, (s % 3600) / 60, s % 60, (unsigned)tvp->tv_usec);
92. break;
93. }
94. }