Changeset View
Changeset View
Standalone View
Standalone View
sys/kern/subr_clock.c
Show First 20 Lines • Show All 238 Lines • ▼ Show 20 Lines | if (ct.hour == 12) | ||||||||
ct.hour = 0; | ct.hour = 0; | ||||||||
if (bct->ispm) | if (bct->ispm) | ||||||||
ct.hour += 12; | ct.hour += 12; | ||||||||
} | } | ||||||||
return (clock_ct_to_ts(&ct, ts)); | return (clock_ct_to_ts(&ct, ts)); | ||||||||
} | } | ||||||||
void | int | ||||||||
clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct) | clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct) | ||||||||
{ | { | ||||||||
time_t i, year, days; | time_t mon, year, days; | ||||||||
time_t rsec; /* remainder seconds */ | time_t rsec; /* remainder seconds */ | ||||||||
time_t secs; | time_t secs; | ||||||||
int hour, day, min; | |||||||||
secs = ts->tv_sec; | secs = ts->tv_sec; | ||||||||
days = secs / SECDAY; | days = secs / SECDAY; | ||||||||
rsec = secs % SECDAY; | rsec = secs % SECDAY; | ||||||||
ct->dow = day_of_week(days); | ct->dow = day_of_week(days); | ||||||||
/* Subtract out whole years. */ | /* Subtract out whole years. */ | ||||||||
if (days >= recent_base_days) { | if (days >= recent_base_days) { | ||||||||
year = recent_base_year; | year = recent_base_year; | ||||||||
days -= recent_base_days; | days -= recent_base_days; | ||||||||
} else { | } else { | ||||||||
year = POSIX_BASE_YEAR; | year = POSIX_BASE_YEAR; | ||||||||
} | } | ||||||||
for (; days >= days_in_year(year); year++) | for (; days >= days_in_year(year); year++) | ||||||||
days -= days_in_year(year); | days -= days_in_year(year); | ||||||||
if (year < 0 || year >= 10000) | |||||||||
corvinkUnsubmitted Not Done Inline Actions
corvink: | |||||||||
Done Inline ActionsGood catch. asomers: Good catch. | |||||||||
return (EINVAL); | |||||||||
ct->year = year; | ct->year = year; | ||||||||
/* Subtract out whole months, counting them in i. */ | /* Subtract out whole months, counting them in mon. */ | ||||||||
for (i = 1; days >= days_in_month(year, i); i++) | for (mon = 1; days >= days_in_month(year, mon); mon++) | ||||||||
days -= days_in_month(year, i); | days -= days_in_month(year, mon); | ||||||||
ct->mon = i; | ct->mon = mon; | ||||||||
/* Days are what is left over (+1) from all that. */ | /* Days are what is left over (+1) from all that. */ | ||||||||
ct->day = days + 1; | day = days + 1; | ||||||||
ct->day = day; | |||||||||
/* Hours, minutes, seconds are easy */ | /* Hours, minutes, seconds are easy */ | ||||||||
ct->hour = rsec / 3600; | hour = rsec / 3600; | ||||||||
ct->hour = hour; | |||||||||
rsec = rsec % 3600; | rsec = rsec % 3600; | ||||||||
ct->min = rsec / 60; | min = rsec / 60; | ||||||||
ct->min = min; | |||||||||
rsec = rsec % 60; | rsec = rsec % 60; | ||||||||
ct->sec = rsec; | ct->sec = rsec; | ||||||||
Not Done Inline ActionsNo. It does not. In fact, it cannot because time_t doesn't encode leap seconds. There's no defined time_t encoding for a leap second. imp: No. It does not. In fact, it cannot because time_t doesn't encode leap seconds. There's no… | |||||||||
ct->nsec = ts->tv_nsec; | ct->nsec = ts->tv_nsec; | ||||||||
Not Done Inline Actions
imp: 59. Though this can never fire after the rsec = rsec % 60 | |||||||||
Done Inline ActionsAs a matter of fact, it should be impossible for any of the checks to fail except for the year one. I'll remove them all, but restore the KASSERTs. asomers: As a matter of fact, it should be impossible for any of the checks to fail except for the year… | |||||||||
if (ct_debug) { | if (ct_debug) { | ||||||||
printf("ts_to_ct(%jd.%09ld) = [", | printf("ts_to_ct(%jd.%09ld) = [", | ||||||||
(intmax_t)ts->tv_sec, ts->tv_nsec); | (intmax_t)ts->tv_sec, ts->tv_nsec); | ||||||||
clock_print_ct(ct, 9); | clock_print_ct(ct, 9); | ||||||||
printf("]\n"); | printf("]\n"); | ||||||||
} | } | ||||||||
KASSERT(ct->year >= 0 && ct->year < 10000, | KASSERT(ct->year >= 0 && ct->year < 10000, | ||||||||
("year %d isn't a 4 digit year", ct->year)); | ("year %d isn't a 4 digit year", ct->year)); | ||||||||
KASSERT(ct->mon >= 1 && ct->mon <= 12, | KASSERT(ct->mon >= 1 && ct->mon <= 12, | ||||||||
("month %d not in 1-12", ct->mon)); | ("month %d not in 1-12", ct->mon)); | ||||||||
KASSERT(ct->day >= 1 && ct->day <= 31, | KASSERT(ct->day >= 1 && ct->day <= 31, | ||||||||
("day %d not in 1-31", ct->day)); | ("day %d not in 1-31", ct->day)); | ||||||||
KASSERT(ct->hour >= 0 && ct->hour <= 23, | KASSERT(ct->hour >= 0 && ct->hour <= 23, | ||||||||
("hour %d not in 0-23", ct->hour)); | ("hour %d not in 0-23", ct->hour)); | ||||||||
KASSERT(ct->min >= 0 && ct->min <= 59, | KASSERT(ct->min >= 0 && ct->min <= 59, | ||||||||
("minute %d not in 0-59", ct->min)); | ("minute %d not in 0-59", ct->min)); | ||||||||
/* Not sure if this interface needs to handle leapseconds or not. */ | KASSERT(ct->sec >= 0 && ct->sec <= 59, | ||||||||
KASSERT(ct->sec >= 0 && ct->sec <= 60, | ("seconds %d not in 0-59", ct->sec)); | ||||||||
("seconds %d not in 0-60", ct->sec)); | |||||||||
return (0); | |||||||||
} | } | ||||||||
void | int | ||||||||
clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm) | clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm) | ||||||||
{ | { | ||||||||
struct clocktime ct; | struct clocktime ct; | ||||||||
int r; | |||||||||
clock_ts_to_ct(ts, &ct); | r = clock_ts_to_ct(ts, &ct); | ||||||||
if (r) | |||||||||
return (r); | |||||||||
/* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */ | /* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */ | ||||||||
bct->ispm = false; | bct->ispm = false; | ||||||||
if (ampm) { | if (ampm) { | ||||||||
if (ct.hour >= 12) { | if (ct.hour >= 12) { | ||||||||
ct.hour -= 12; | ct.hour -= 12; | ||||||||
bct->ispm = true; | bct->ispm = true; | ||||||||
} | } | ||||||||
if (ct.hour == 0) | if (ct.hour == 0) | ||||||||
ct.hour = 12; | ct.hour = 12; | ||||||||
} | } | ||||||||
bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8); | bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8); | ||||||||
bct->mon = TOBCD(ct.mon); | bct->mon = TOBCD(ct.mon); | ||||||||
bct->day = TOBCD(ct.day); | bct->day = TOBCD(ct.day); | ||||||||
bct->hour = TOBCD(ct.hour); | bct->hour = TOBCD(ct.hour); | ||||||||
bct->min = TOBCD(ct.min); | bct->min = TOBCD(ct.min); | ||||||||
bct->sec = TOBCD(ct.sec); | bct->sec = TOBCD(ct.sec); | ||||||||
bct->dow = ct.dow; | bct->dow = ct.dow; | ||||||||
bct->nsec = ct.nsec; | bct->nsec = ct.nsec; | ||||||||
return (0); | |||||||||
} | } | ||||||||
void | void | ||||||||
clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits) | clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits) | ||||||||
{ | { | ||||||||
KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits)); | KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits)); | ||||||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |