Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / prettydate.c
blobd6b49a18eba9bd852528bee66ef0b5df890f7f46
1 /* $NetBSD: prettydate.c,v 1.2 2003/12/04 16:23:37 drochner Exp $ */
3 /*
4 * prettydate - convert a time stamp to something readable
5 */
6 #include <stdio.h>
8 #include "ntp_fp.h"
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
30 struct tm *
31 ntp2unix_tm(
32 u_long ntp, int local
35 time_t t, curr;
36 struct tm *tm;
37 int curr_year, epoch_nr;
39 /* First get the current year: */
40 curr = time(NULL);
41 tm = local ? localtime(&curr) : gmtime(&curr);
42 if (!tm) return NULL;
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);
58 #if SIZEOF_TIME_T < 4
59 # include "Bletch: sizeof(time_t) < 4!"
60 #endif
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 */
73 return tm;
76 char *
77 prettydate(
78 l_fp *ts
81 char *bp;
82 struct tm *tm;
83 time_t sec;
84 u_long msec;
86 LIB_GETBUF(bp);
88 sec = ts->l_ui;
89 msec = ts->l_uf / 4294967; /* fract / (2 ** 32 / 1000) */
91 tm = ntp2unix_tm(sec, 1);
92 if (!tm) {
93 (void) sprintf(bp, "%08lx.%08lx --- --- -- ---- --:--:--",
94 (u_long)ts->l_ui, (u_long)ts->l_uf);
96 else {
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);
103 return bp;
106 char *
107 gmprettydate(
108 l_fp *ts
111 char *bp;
112 struct tm *tm;
113 time_t sec;
114 u_long msec;
116 LIB_GETBUF(bp);
118 sec = ts->l_ui;
119 msec = ts->l_uf / 4294967; /* fract / (2 ** 32 / 1000) */
121 tm = ntp2unix_tm(sec, 0);
122 if (!tm) {
123 (void) sprintf(bp, "%08lx.%08lx --- --- -- ---- --:--:--",
124 (u_long)ts->l_ui, (u_long)ts->l_uf);
126 else {
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);
133 return bp;