*/
vint64
time_to_vint64(
- const time_t * tv
+ const time_t * ptt
)
{
vint64 res;
- time_t tmp;
+ time_t tt;
- /*
- * shifting negative signed quantities is compiler-dependent, so
- * we better avoid it and do it all manually. And shifting more
- * than the width of a quantity is undefined. Also a don't do!
- */
+ tt = *ptt;
#if SIZEOF_TIME_T <= 4
res.D_s.hi = 0;
- if ((tmp = *tv) < 0) {
- res.D_s.lo = (u_int32) -tmp;
+ if (tt < 0) {
+ res.D_s.lo = (u_int32)-tt;
M_NEG(res.D_s.hi, res.D_s.lo);
} else {
- res.D_s.lo = (u_int32) tmp;
+ res.D_s.lo = (u_int32)tt;
}
-#elif defined (HAVE_INT64)
+#elif defined(HAVE_INT64)
- (void)tmp; /* touch it to avoid warnings... */
- res.q_s = *tv;
+ res.q_s = tt;
#else
- time_t tmp = *tv;
- if ((tmp = *tv) < 0) {
- tmp = -tmp;
- res.D_s.lo = (u_int32)tmp;
- res.D_s.hi = (u_int32)(tmp >> 32);
+ /*
+ * shifting negative signed quantities is compiler-dependent, so
+ * we better avoid it and do it all manually. And shifting more
+ * than the width of a quantity is undefined. Also a don't do!
+ */
+ if (tt < 0) {
+ tt = -tt;
+ res.D_s.lo = (u_int32)tt;
+ res.D_s.hi = (u_int32)(tt >> 32);
M_NEG(res.D_s.hi, res.D_s.lo);
} else {
- res.D_s.lo = (u_int32)tmp;
- res.D_s.hi = (u_int32)(tmp >> 32);
+ res.D_s.lo = (u_int32)tt;
+ res.D_s.hi = (u_int32)(tt >> 32);
}
-
+
#endif
- return res;
+ return res;
}
res = (time_t)tv->D_s.lo;
-#elif defined (HAVE_INT64)
+#elif defined(HAVE_INT64)
res = (time_t)tv->q_s;
#else
res = ((time_t)tv->d_s.hi << 32) | tv->D_s.lo;
-
+
#endif
return res;
*/
static const char build[] = __TIME__ "/" __DATE__;
static const char mlist[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
-
- char month[4];
- const char *cp;
- u_short hour, minute, second, year, day;
-
- memset(jd, 0, sizeof(struct calendar));
+ char monstr[4];
+ const char * cp;
+ u_short hour, minute, second, month, day, year;
+
+ ZERO(jd);
jd->year = 1970;
jd->month = 1;
jd->monthday = 1;
if (6 == sscanf(build, "%hu:%hu:%hu/%3s %hu %hu",
- &hour, &minute, &second, month, &day, &year)) {
- cp = strstr(mlist, month);
+ &hour, &minute, &second, monstr, &day, &year)) {
+ cp = strstr(mlist, monstr);
if (NULL != cp) {
+ month = (u_short)(cp - mlist) / 3 + 1;
jd->year = year;
- jd->month = (cp - mlist) / 3 + 1;
- jd->monthday = day;
- jd->hour = hour;
- jd->minute = minute;
- jd->second = second;
- return 1;
+ jd->month = (u_char)month;
+ jd->monthday = (u_char)day;
+ jd->hour = (u_char)hour;
+ jd->minute = (u_char)minute;
+ jd->second = (u_char)second;
+
+ return TRUE;
}
- }
+ }
- return 0;
+ return FALSE;
}
ntpcal_periodic_extend(
int32 pivot,
int32 value,
- int32 cycle)
+ int32 cycle
+ )
{
u_int32 diff;
char cpl = 0; /* modulo complement flag */
*/
vint64
ntpcal_ntp_to_time(
- u_int32 ntp ,
- const time_t *pivot)
+ u_int32 ntp,
+ const time_t * pivot
+ )
{
vint64 res;
#ifdef HAVE_INT64
- res.q_s = pivot ? *pivot : now();
-
- res.Q_s -= 0x80000000u; /* unshift of half range */
+ res.q_s = (pivot != NULL)
+ ? *pivot
+ : now();
+ res.Q_s -= 0x80000000; /* unshift of half range */
ntp -= (u_int32)JAN_1970; /* warp into UN*X domain */
ntp -= res.D_s.lo; /* cycle difference */
res.Q_s += (u_int64)ntp; /* get expanded time */
#else /* no 64bit scalars */
- time_t tmp = pivot ? *pivot : now();
-
+ time_t tmp;
+
+ tmp = (pivot != NULL)
+ ? *pivot
+ : now();
res = time_to_vint64(&tmp);
- M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000u);
+ M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000);
ntp -= (u_int32)JAN_1970; /* warp into UN*X domain */
ntp -= res.D_s.lo; /* cycle difference */
M_ADD(res.D_s.hi, res.D_s.lo, 0, ntp);
*/
vint64
ntpcal_ntp_to_ntp(
- u_int32 ntp ,
- const time_t *pivot)
+ u_int32 ntp,
+ const time_t *pivot
+ )
{
vint64 res;
#ifdef HAVE_INT64
- res.q_s = pivot ? *pivot : now();
-
- res.Q_s -= 0x80000000u; /* unshift of half range */
+ res.q_s = (pivot)
+ ? *pivot
+ : now();
+ res.Q_s -= 0x80000000; /* unshift of half range */
res.Q_s += (u_int32)JAN_1970; /* warp into NTP domain */
ntp -= res.D_s.lo; /* cycle difference */
res.Q_s += (u_int64)ntp; /* get expanded time */
#else /* no 64bit scalars */
- time_t tmp = pivot ? *pivot : now();
-
+ time_t tmp;
+
+ tmp = (pivot)
+ ? *pivot
+ : now();
res = time_to_vint64(&tmp);
M_SUB(res.D_s.hi, res.D_s.lo, 0, 0x80000000u);
M_ADD(res.D_s.hi, res.D_s.lo, 0, (u_int32)JAN_1970);/*into NTP */
*/
ntpcal_split
ntpcal_daysplit(
- const vint64 *ts)
+ const vint64 *ts
+ )
{
ntpcal_split res;
memcpy(&op, ts, sizeof(op));
/* fix sign */
- if ((isneg = M_ISNEG(op.D_s.hi, op.D_s.lo)) != 0)
+ isneg = M_ISNEG(op.D_s.hi, op.D_s.lo);
+ if (isneg)
M_NEG(op.D_s.hi, op.D_s.lo);
/* save remainder of DIV 128, shift for divide */
* even if we *know* the divider fits into unsigned 16 bits the
* exra code pathes should pay off.
*/
- if ((a = op.D_s.hi) > 675u)
+ a = op.D_s.hi;
+ if (a > 675u)
a = a % 675u;
if (a) {
a = (a << 16) | op.W_s.lh;
static int32
priv_timesplit(
int split[3],
- int32 ts )
+ int32 ts
+ )
{
int32 days = 0;
*/
ntpcal_split
ntpcal_split_eradays(
- int32 days ,
- int *isleapyear)
+ int32 days,
+ int *isleapyear
+ )
{
ntpcal_split res;
int32 n400, n100, n004, n001, yday; /* calendar year cycles */
*/
ntpcal_split
ntpcal_split_yeardays(
- int32 eyd ,
- int isleapyear)
+ int32 eyd,
+ int isleapyear
+ )
{
ntpcal_split res;
const u_short *lt; /* month length table */
if (lt[res.hi + 1] <= eyd) /* fixup approximative month value */
res.hi += 1;
res.lo = eyd - lt[res.hi];
- } else
+ } else {
res.lo = res.hi = -1;
+ }
return res;
}
int
ntpcal_rd_to_date(
struct calendar *jd,
- int32 rd)
+ int32 rd
+ )
{
ntpcal_split split;
- int leaps = 0;
- int retv = 0;
+ int leaps;
+ int retv;
+ leaps = 0;
+ retv = 0;
/* get day-of-week first */
jd->weekday = rd % 7;
if (jd->weekday >= 7) /* unsigned! */
int
ntpcal_rd_to_tm(
struct tm *utm,
- int32 rd )
+ int32 rd
+ )
{
ntpcal_split split;
- int leaps = 0;
+ int leaps;
+ leaps = 0;
/* get day-of-week first */
utm->tm_wday = rd % 7;
if (utm->tm_wday < 0)
int32
ntpcal_daysec_to_date(
struct calendar *jd,
- int32 sec)
+ int32 sec
+ )
{
int32 days;
int ts[3];
int32
ntpcal_daysec_to_tm(
struct tm *utm,
- int32 sec)
+ int32 sec
+ )
{
int32 days;
int ts[3];
*/
int
ntpcal_daysplit_to_date(
- struct calendar *jd ,
- const ntpcal_split *ds ,
- int32 dof)
+ struct calendar *jd,
+ const ntpcal_split *ds,
+ int32 dof
+ )
{
dof += ntpcal_daysec_to_date(jd, ds->lo);
return ntpcal_rd_to_date(jd, ds->hi + dof);
ntpcal_daysplit_to_tm(
struct tm *utm,
const ntpcal_split *ds ,
- int32 dof)
+ int32 dof
+ )
{
dof += ntpcal_daysec_to_tm(utm, ds->lo);
+
return ntpcal_rd_to_tm(utm, ds->hi + dof);
}
int
ntpcal_time_to_date(
struct calendar *jd,
- const vint64 *ts)
+ const vint64 *ts
+ )
{
ntpcal_split ds;
ds = ntpcal_daysplit(ts);
ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
ds.hi += DAY_UNIX_STARTS;
+
return ntpcal_rd_to_date(jd, ds.hi);
}
vint64
ntpcal_dayjoin(
int32 days,
- int32 secs)
+ int32 secs
+ )
{
vint64 res;
u_int32 p1, p2;
int isneg;
- /* res = days *86400 + secs, using manual 16/32 bit
+ /*
+ * res = days *86400 + secs, using manual 16/32 bit
* multiplications and shifts.
*/
- if ((isneg = (days < 0)) != 0)
+ isneg = (days < 0);
+ if (isneg)
days = -days;
/* assemble days * 675 */
/* properly add seconds */
p2 = 0;
if (secs < 0) {
- p1 = (u_int32) -secs;
+ p1 = (u_int32)-secs;
M_NEG(p2, p1);
} else {
- p1 = (u_int32) secs;
+ p1 = (u_int32)secs;
}
M_ADD(res.D_s.hi, res.D_s.lo, p2, p1);
*/
int32
ntpcal_days_in_years(
- int32 years)
+ int32 years
+ )
{
int32 cycle; /* full gregorian cycle */
*/
ntpcal_split
ntpcal_days_in_months(
- int32 m)
+ int32 m
+ )
{
ntpcal_split res;
*/
int32
ntpcal_edate_to_eradays(
- int32 years ,
- int32 mons ,
- int32 mdays )
+ int32 years,
+ int32 mons,
+ int32 mdays
+ )
{
ntpcal_split tmp;
int32 res;
*/
int32
ntpcal_edate_to_yeardays(
- int32 years ,
+ int32 years,
int32 mons,
- int32 mdays )
+ int32 mdays
+ )
{
ntpcal_split tmp;
+ ntpcal_days_in_years(years + tmp.hi)
- ntpcal_days_in_years(years);
}
+
return mdays;
}
*/
int32
ntpcal_etime_to_seconds(
- int32 hours ,
+ int32 hours,
int32 minutes,
- int32 seconds)
+ int32 seconds
+ )
{
int32 res;
*/
int32
ntpcal_tm_to_rd(
- const struct tm *utm)
+ const struct tm *utm
+ )
{
return ntpcal_edate_to_eradays(utm->tm_year + 1899,
utm->tm_mon,
*/
int32
ntpcal_date_to_rd(
- const struct calendar *jd)
+ const struct calendar *jd
+ )
{
return ntpcal_edate_to_eradays((int32)jd->year - 1,
(int32)jd->month - 1,
*/
int32
ntpcal_year_to_ystart(
- int32 year)
+ int32 year
+ )
{
return ntpcal_days_in_years(year - 1) + 1;
}
*/
int32
ntpcal_rd_to_ystart(
- int32 rd)
+ int32 rd
+ )
{
/*
* Rather simple exercise: split the day number into elapsed
*/
int32
ntpcal_rd_to_mstart(
- int32 rd)
+ int32 rd
+ )
{
ntpcal_split split;
int leaps;
- split = ntpcal_split_eradays( rd - 1 , &leaps);
- split = ntpcal_split_yeardays(split.lo, leaps);
+ split = ntpcal_split_eradays(rd - 1, &leaps);
+ split = ntpcal_split_yeardays(split.lo, leaps);
+
return rd - split.lo;
}
*/
int32
ntpcal_date_to_daysec(
- const struct calendar *jd)
+ const struct calendar *jd
+ )
{
- return ntpcal_etime_to_seconds(jd->hour, jd->minute, jd->second);
+ return ntpcal_etime_to_seconds(jd->hour, jd->minute,
+ jd->second);
}
/*
*/
int32
ntpcal_tm_to_daysec(
- const struct tm *utm)
+ const struct tm *utm
+ )
{
- return ntpcal_etime_to_seconds(utm->tm_hour, utm->tm_min, utm->tm_sec);
+ return ntpcal_etime_to_seconds(utm->tm_hour, utm->tm_min,
+ utm->tm_sec);
}
/*
*/
time_t
ntpcal_date_to_time(
- const struct calendar *jd)
+ const struct calendar *jd
+ )
{
vint64 join;
int32 days, secs;
days = ntpcal_date_to_rd(jd) - DAY_UNIX_STARTS;
secs = ntpcal_date_to_daysec(jd);
join = ntpcal_dayjoin(days, secs);
+
return vint64_to_time(&join);
}
*/
int
ntpcal_ntp_to_date(
- struct calendar *jd ,
+ struct calendar *jd,
u_int32 ntp,
- const time_t *piv)
+ const time_t *piv
+ )
{
vint64 vl;
ntpcal_split ds;
vl = ntpcal_ntp_to_ntp(ntp, piv);
ds = ntpcal_daysplit(&vl);
ds.hi += ntpcal_daysec_to_date(jd, ds.lo);
+
return ntpcal_rd_to_date(jd, ds.hi + DAY_NTP_STARTS);
}
u_int32
ntpcal_date_to_ntp(
- const struct calendar *jd)
+ const struct calendar *jd
+ )
{
/*
* Convert date to NTP. Ignore yearday, use d/m/y only.
int32
ntpcal_weekday_gt(
int32 rdn,
- int32 dow)
+ int32 dow
+ )
{
return ntpcal_periodic_extend(rdn+1, dow, 7);
}
int32
ntpcal_weekday_ge(
int32 rdn,
- int32 dow)
+ int32 dow
+ )
{
return ntpcal_periodic_extend(rdn, dow, 7);
}
int32
ntpcal_weekday_close(
int32 rdn,
- int32 dow)
+ int32 dow
+ )
{
return ntpcal_periodic_extend(rdn-3, dow, 7);
}
int32
ntpcal_weekday_le(
int32 rdn,
- int32 dow)
+ int32 dow
+ )
{
return ntpcal_periodic_extend(rdn, dow, -7);
}
int32
ntpcal_weekday_lt(
int32 rdn,
- int32 dow)
+ int32 dow
+ )
{
return ntpcal_periodic_extend(rdn-1, dow, -7);
}
*/
int32
isocal_weeks_in_years(
- int32 years)
+ int32 years
+ )
{
/*
* use: w = (y * 53431 + b[c]) / 1024 as interpolation
*/
static const int32 bctab[4] = { 449, 157, 889, 597 };
-
int32 cycle; /* full gregorian cycle */
int32 cents; /* full centuries */
int32 weeks; /* accumulated weeks */
*/
ntpcal_split
isocal_split_eraweeks(
- int32 weeks)
+ int32 weeks
+ )
{
/*
* use: y = (w * 157 + b[c]) / 8192 as interpolation
*/
-
static const int32 bctab[4] = { 85, 131, 17, 62 };
-
ntpcal_split res;
int32 cents;
*/
int
isocal_ntp_to_date(
- struct isodate *id ,
+ struct isodate *id,
u_int32 ntp,
- const time_t *piv)
+ const time_t *piv
+ )
{
vint64 vl;
ntpcal_split ds;
*/
u_int32
isocal_date_to_ntp(
- const struct isodate *id)
+ const struct isodate *id
+ )
{
int32 weeks, days, secs;
days = weeks * 7 + (int32)id->weekday;
/* days is RDN of ISO date now */
secs = ntpcal_etime_to_seconds(id->hour, id->minute, id->second);
+
return ntpcal_dayjoin(days - DAY_NTP_STARTS, secs).d_s.lo;
}
{
time_t pivot; /* for ntp era unfolding */
struct timeval timetv, tvlast, tvdiff;
+ struct timespec timets;
+ struct calendar jd;
l_fp fp_ofs, fp_sys; /* offset and target system time in FP */
- /* Get pivot time for NTP era unfolding. Since we don't step
+ /*
+ * Get pivot time for NTP era unfolding. Since we don't step
* very often, we can afford to do the whole calculation from
* scratch. And we're not in the time-critical path yet.
*/
* system time is in the 2^32 seconds starting at 1970-01-01,
* 00:00:00 UTC.
*/
- pivot = 0x80000000U;
+ pivot = 0x80000000;
#if USE_COMPILETIME_PIVOT
/*
* Add the compile time minus 10 years to get a possible target
* years). This should be sufficient for a given binary of
* NTPD.
*/
- {
- struct calendar jd;
-
- if (ntpcal_get_build_date(&jd)) {
- jd.year -= 10;
- pivot += ntpcal_date_to_time(&jd);
- } else {
- msyslog(LOG_ERR,
- "step-systime: assume 1970-01-01 as build date");
- }
+ if (ntpcal_get_build_date(&jd)) {
+ jd.year -= 10;
+ pivot += ntpcal_date_to_time(&jd);
+ } else {
+ msyslog(LOG_ERR,
+ "step-systime: assume 1970-01-01 as build date");
}
+#else
+ UNUSED_LOCAL(jd);
#endif /* USE_COMPILETIME_PIVOT */
#else
+ UNUSED_LOCAL(jd);
/* This makes sure the resulting time stamp is on or after
* 1969-12-31/23:59:59 UTC and gives us additional two years,
* from the change of NTP era in 2036 to the UNIX rollover in
* need a longer 'time_t' after that! Or a different baseline,
* but that would cause other serious trouble, too.
*/
- pivot = 0x7FFFFFFFU;
+ pivot = 0x7FFFFFFF;
#endif
/* get the complete jump distance as l_fp */
/* get the current time as l_fp (without fuzz) and as struct timeval */
#if defined(GET_SYSTIME_AS_TIMESPEC)
- {
- struct timespec timets;
- (void) GET_SYSTIME_AS_TIMESPEC(&timets);
- timespec_abstolfp(&fp_sys, &timets);
- tvlast.tv_sec = timets.tv_sec;
- tvlast.tv_usec = (timets.tv_nsec + 500) / 1000;
- }
+ GET_SYSTIME_AS_TIMESPEC(&timets);
+ timespec_abstolfp(&fp_sys, &timets);
+ tvlast.tv_sec = timets.tv_sec;
+ tvlast.tv_usec = (timets.tv_nsec + 500) / 1000;
#else /* have GETTIMEOFDAY */
- {
- (void) GETTIMEOFDAY(&tvlast, NULL);
- timeval_abstolfp(&fp_sys, &tvlast);
- }
+ UNUSED_LOCAL(timets);
+ GETTIMEOFDAY(&tvlast, NULL);
+ timeval_abstolfp(&fp_sys, &tvlast);
#endif
/* get the target time as l_fp */
/* now set new system time */
if (ntp_set_tod(&timetv, NULL) != 0) {
msyslog(LOG_ERR, "step-systime: %m");
- return (0);
+ return FALSE;
}
/* <--- time-critical path ended with 'ntp_set_tod()' <--- */
*/
timeval_sub(&tvdiff, &timetv, &tvlast);
timeval_abs(&tvdiff, &tvdiff);
- if (tvdiff.tv_sec > 0)
- {
+ if (tvdiff.tv_sec > 0) {
#ifdef HAVE_UTMP_H
struct utmp ut;
#endif
#endif /* UPDATE_WTMPX */
}
- return (1);
+ return TRUE;
}
#endif /* !SIM */