2 * prettydate - convert a time stamp to something readable
8 #include "ntp_unixtime.h" /* includes <sys/time.h> */
9 #include "lib_strbuf.h"
10 #include "ntp_stdlib.h"
11 #include "ntp_assert.h"
12 #include "ntp_calendar.h"
15 # error sizeof(time_t) < 4 -- this will not work!
18 static char *common_prettydate(l_fp *, int);
20 const char * const months[12] = {
21 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
22 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
25 const char * const daynames[7] = {
26 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
29 /* Helper function to handle possible wraparound of the ntp epoch.
31 * Works by periodic extension of the ntp time stamp in the UN*X epoch.
32 * If the 'time_t' is 32 bit, use solar cycle warping to get the value
33 * in a suitable range. Also uses solar cycle warping to work around
34 * really buggy implementations of 'gmtime()' / 'localtime()' that
35 * cannot work with a negative time value, that is, times before
36 * 1970-01-01. (MSVCRT...)
38 * Apart from that we're assuming that the localtime/gmtime library
39 * functions have been updated so that they work...
41 * An explanation: The julian calendar repeats ever 28 years, because
42 * it's the LCM of 7 and 1461, the week and leap year cycles. This is
43 * called a 'solar cycle'. The gregorian calendar does the same as
44 * long as no centennial year (divisible by 100, but not 400) goes in
45 * the way. So between 1901 and 2099 (inclusive) we can warp time
46 * stamps by 28 years to make them suitable for localtime() and
47 * gmtime() if we have trouble. Of course this will play hubbubb with
48 * the DST zone switches, so we should do it only if necessary; but as
49 * we NEED a proper conversion to dates via gmtime() we should try to
50 * cope with as many idiosyncrasies as possible.
55 * solar cycle in unsigned secs and years, and the cycle limits.
57 #define SOLAR_CYCLE_SECS 0x34AADC80UL /* 7*1461*86400*/
58 #define SOLAR_CYCLE_YEARS 28
77 * If there is chance of truncation, try to fix it. Let the
78 * compiler find out if this can happen at all.
80 while (ts != tl) { /* truncation? */
82 if (--folds < MINFOLD)
84 tl += SOLAR_CYCLE_SECS;
86 if (++folds > MAXFOLD)
88 tl -= SOLAR_CYCLE_SECS;
90 ts = tl; /* next try... */
95 * since we do not have 64-bit scalars, it's not likely we have
96 * 64-bit time_t. Assume 32 bits and properly reduce the value.
103 while ((hi && ~hi) || ((hi ^ lo) & 0x80000000u)) {
104 if (M_ISNEG(hi, lo)) {
105 if (--folds < MINFOLD)
107 M_ADD(hi, lo, 0, SOLAR_CYCLE_SECS);
109 if (++folds > MAXFOLD)
111 M_SUB(hi, lo, 0, SOLAR_CYCLE_SECS);
119 * 'ts' should be a suitable value by now. Just go ahead, but
122 * There are some pathological implementations of 'gmtime()'
123 * and 'localtime()' out there. No matter if we have 32-bit or
124 * 64-bit 'time_t', try to fix this by solar cycle warping
127 * At least the MSDN says that the (Microsoft) Windoze
128 * versions of 'gmtime()' and 'localtime()' will bark on time
131 while ((tm = (*(local ? localtime : gmtime))(&ts)) == NULL)
133 if (--folds < MINFOLD)
135 ts += SOLAR_CYCLE_SECS;
136 } else if (ts >= (time_t)SOLAR_CYCLE_SECS) {
137 if (++folds > MAXFOLD)
139 ts -= SOLAR_CYCLE_SECS;
141 return NULL; /* That's truly pathological! */
143 /* 'tm' surely not NULL here! */
144 NTP_INSIST(tm != NULL);
146 tm->tm_year += folds * SOLAR_CYCLE_YEARS;
147 if (tm->tm_year <= 0 || tm->tm_year >= 200)
148 return NULL; /* left warp range... can't help here! */
160 static const char pfmt0[] =
161 "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03u";
162 static const char pfmt1[] =
163 "%08lx.%08lx [%s, %s %2d %4d %2d:%02d:%02d.%03u UTC]";
173 /* get & fix milliseconds */
175 msec = ts->l_uf / 4294967; /* fract / (2 ** 32 / 1000) */
180 sec = ntpcal_ntp_to_time(ntps, NULL);
181 tm = get_struct_tm(&sec, local);
184 * get a replacement, but always in UTC, using
185 * ntpcal_time_to_date()
188 ntpcal_time_to_date(&jd, &sec);
189 snprintf(bp, LIB_BUFLENGTH, local ? pfmt1 : pfmt0,
190 (u_long)ts->l_ui, (u_long)ts->l_uf,
191 daynames[jd.weekday], months[jd.month-1],
192 jd.monthday, jd.year, jd.hour,
193 jd.minute, jd.second, msec);
195 snprintf(bp, LIB_BUFLENGTH, pfmt0,
196 (u_long)ts->l_ui, (u_long)ts->l_uf,
197 daynames[tm->tm_wday], months[tm->tm_mon],
198 tm->tm_mday, 1900 + tm->tm_year, tm->tm_hour,
199 tm->tm_min, tm->tm_sec, msec);
209 return common_prettydate(ts, 1);
218 return common_prettydate(ts, 0);
224 u_int32 ntp, int local
228 vl = ntpcal_ntp_to_time(ntp, NULL);
229 return get_struct_tm(&vl, local);