void test_NtpToTime(void) { # if SIZEOF_TIME_T <= 4 TEST_IGNORE_MESSAGE("test only useful for sizeof(time_t) > 4, skipped"); # else static const uint32_t ntp_vals[6] = { UINT32_C(0x00000000), UINT32_C(0x00000001), UINT32_C(0x7FFFFFFF), UINT32_C(0x80000000), UINT32_C(0x80000001), UINT32_C(0xFFFFFFFF) }; static char lbuf[128]; vint64 hold; time_t pivot, texp, diff; uint32_t back; int loops, iloop; pivot = 0; for (loops = 0; loops < 16; ++loops) { for (iloop = 0; iloop < 6; ++iloop) { hold = ntpcal_ntp_to_time( ntp_vals[iloop], &pivot); texp = vint64_to_time(&hold); /* constraint 1: texp must be in the * (right-open) intervall [p-(2^31), p+(2^31)[ */ diff = texp - pivot; snprintf(lbuf, sizeof(lbuf), "bounds check: piv=%lld exp=%lld dif=%lld", (long long)pivot, (long long)texp, (long long)diff); TEST_ASSERT_MESSAGE((diff >= INT32_MIN) && (diff <= INT32_MAX), lbuf); /* constraint 2: conversion from full time back * to truncated NTP time must yield same result * as input. */ back = (uint32_t)texp + JAN_1970; snprintf(lbuf, sizeof(lbuf), "modulo check: ntp(in)=$%08lu ntp(out)=$%08lu", (unsigned long)ntp_vals[iloop], (unsigned long)back); TEST_ASSERT_EQUAL_MESSAGE(ntp_vals[iloop], back, lbuf); } pivot += 0x20000000; } # endif }
struct tm * ntp2unix_tm( u_int32 ntp, int local ) { vint64 vl; vl = ntpcal_ntp_to_time(ntp, NULL); return get_struct_tm(&vl, local); }
static char * common_prettydate( l_fp *ts, int local ) { static const char pfmt0[] = "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03u"; static const char pfmt1[] = "%08lx.%08lx [%s, %s %2d %4d %2d:%02d:%02d.%03u UTC]"; char *bp; struct tm *tm; u_int msec; u_int32 ntps; vint64 sec; LIB_GETBUF(bp); /* get & fix milliseconds */ ntps = ts->l_ui; msec = ts->l_uf / 4294967; /* fract / (2 ** 32 / 1000) */ if (msec >= 1000u) { msec -= 1000u; ntps++; } sec = ntpcal_ntp_to_time(ntps, NULL); tm = get_struct_tm(&sec, local); if (!tm) { /* * get a replacement, but always in UTC, using * ntpcal_time_to_date() */ struct calendar jd; ntpcal_time_to_date(&jd, &sec); snprintf(bp, LIB_BUFLENGTH, local ? pfmt1 : pfmt0, (u_long)ts->l_ui, (u_long)ts->l_uf, daynames[jd.weekday], months[jd.month-1], jd.monthday, jd.year, jd.hour, jd.minute, jd.second, msec); } else snprintf(bp, LIB_BUFLENGTH, pfmt0, (u_long)ts->l_ui, (u_long)ts->l_uf, daynames[tm->tm_wday], months[tm->tm_mon], tm->tm_mday, 1900 + tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec, msec); return bp; }