1 /* $NetBSD: prettydate.c,v 1.2 2003/12/04 16:23:37 drochner Exp $ */
4 * prettydate - convert a time stamp to something readable
9 #include "ntp_unixtime.h" /* includes <sys/time.h> */
10 #include "lib_strbuf.h"
11 #include "ntp_stdlib.h"
13 static const char *months
[] = {
14 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
15 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
18 static const char *days
[] = {
19 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
22 /* Helper function to handle possible wraparound of the ntp epoch.
24 Works by assuming that the localtime/gmtime library functions
25 have been updated so that they work
28 #define MAX_EPOCH_NR 1000
37 int curr_year
, epoch_nr
;
39 /* First get the current year: */
41 tm
= local
? localtime(&curr
) : gmtime(&curr
);
44 curr_year
= 1900 + tm
->tm_year
;
46 /* Convert the ntp timestamp to a unix utc seconds count: */
47 t
= (time_t) ntp
- JAN_1970
;
49 /* Check that the ntp timestamp is not before a 136 year window centered
50 around the current year:
52 Failsafe in case of an infinite loop:
53 Allow up to 1000 epochs of 136 years each!
55 for (epoch_nr
= 0; epoch_nr
< MAX_EPOCH_NR
; epoch_nr
++) {
56 tm
= local
? localtime(&t
) : gmtime(&t
);
59 # include "Bletch: sizeof(time_t) < 4!"
62 #if SIZEOF_TIME_T == 4
63 /* If 32 bits, then year is 1970-2038, so no sense looking */
64 epoch_nr
= MAX_EPOCH_NR
;
65 #else /* SIZEOF_TIME_T > 4 */
66 /* Check that the resulting year is in the correct epoch: */
67 if (1900 + tm
->tm_year
> curr_year
- 68) break;
69 /* Epoch wraparound: Add 2^32 seconds! */
70 t
+= (time_t) 65536 << 16;
71 #endif /* SIZEOF_TIME_T > 4 */
89 msec
= ts
->l_uf
/ 4294967; /* fract / (2 ** 32 / 1000) */
91 tm
= ntp2unix_tm(sec
, 1);
93 (void) sprintf(bp
, "%08lx.%08lx --- --- -- ---- --:--:--",
94 (u_long
)ts
->l_ui
, (u_long
)ts
->l_uf
);
97 (void) sprintf(bp
, "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03lu",
98 (u_long
)ts
->l_ui
, (u_long
)ts
->l_uf
, days
[tm
->tm_wday
],
99 months
[tm
->tm_mon
], tm
->tm_mday
, 1900 + tm
->tm_year
,
100 tm
->tm_hour
,tm
->tm_min
, tm
->tm_sec
, msec
);
119 msec
= ts
->l_uf
/ 4294967; /* fract / (2 ** 32 / 1000) */
121 tm
= ntp2unix_tm(sec
, 0);
123 (void) sprintf(bp
, "%08lx.%08lx --- --- -- ---- --:--:--",
124 (u_long
)ts
->l_ui
, (u_long
)ts
->l_uf
);
127 (void) sprintf(bp
, "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03lu",
128 (u_long
)ts
->l_ui
, (u_long
)ts
->l_uf
, days
[tm
->tm_wday
],
129 months
[tm
->tm_mon
], tm
->tm_mday
, 1900 + tm
->tm_year
,
130 tm
->tm_hour
,tm
->tm_min
, tm
->tm_sec
, msec
);