48#include <sys/kernel.h>
51#include <sys/limits.h>
52#include <sys/sysctl.h>
53#include <sys/timetc.h>
65 if (!error && req->newptr)
71 "Local offset from UTC in seconds");
75 &
ct_debug, 0,
"Enable printing of clocktime debugging");
88#define days_in_year(y) (leapyear(y) ? 366 : 365)
89#define days_in_month(y, m) \
90 (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
92#define day_of_week(days) (((days) + 4) % 7)
95 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
111 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
127 if ((year & 3) == 0) {
129 if ((year % 100) == 0) {
131 if ((year % 400) == 0)
161 if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
163 ct->hour > 23 || ct->min > 59 || ct->sec > 59 || year < 1970 ||
164 (
sizeof(time_t) == 4 && year > 2037)) {
181 for (; i < year; i++)
185 for (i = 1; i < ct->mon; i++)
187 days += (ct->day - 1);
189 ts->tv_sec = (((time_t)
days * 24 + ct->hour) * 60 + ct->min) * 60 +
191 ts->tv_nsec = ct->nsec;
194 printf(
" = %jd.%09ld\n", (intmax_t)
ts->tv_sec,
ts->tv_nsec);
208 bcent = bct->year >> 8;
209 byear = bct->year & 0xff;
216 if (!validbcd(bcent) || !validbcd(byear) || !validbcd(bct->mon) ||
217 !validbcd(bct->day) || !validbcd(bct->hour) ||
218 !validbcd(bct->min) || !validbcd(bct->sec)) {
220 printf(
"clock_bcd_to_ts: bad BCD: "
221 "[%04x-%02x-%02x %02x:%02x:%02x]\n",
222 bct->year, bct->mon, bct->day,
223 bct->hour, bct->min, bct->sec);
227 ct.year = FROMBCD(byear) + FROMBCD(bcent) * 100;
228 ct.mon = FROMBCD(bct->mon);
229 ct.day = FROMBCD(bct->day);
230 ct.hour = FROMBCD(bct->hour);
231 ct.min = FROMBCD(bct->min);
232 ct.sec = FROMBCD(bct->sec);
250 time_t i, year,
days;
255 days = secs / SECDAY;
256 rsec = secs % SECDAY;
265 year = POSIX_BASE_YEAR;
280 ct->hour = rsec / 3600;
285 ct->nsec =
ts->tv_nsec;
287 printf(
"ts_to_ct(%jd.%09ld) = [",
288 (intmax_t)
ts->tv_sec,
ts->tv_nsec);
293 KASSERT(ct->year >= 0 && ct->year < 10000,
294 (
"year %d isn't a 4 digit year", ct->year));
295 KASSERT(ct->mon >= 1 && ct->mon <= 12,
296 (
"month %d not in 1-12", ct->mon));
297 KASSERT(ct->day >= 1 && ct->day <= 31,
298 (
"day %d not in 1-31", ct->day));
299 KASSERT(ct->hour >= 0 && ct->hour <= 23,
300 (
"hour %d not in 0-23", ct->hour));
301 KASSERT(ct->min >= 0 && ct->min <= 59,
302 (
"minute %d not in 0-59", ct->min));
304 KASSERT(ct->sec >= 0 && ct->sec <= 60,
305 (
"seconds %d not in 0-60", ct->sec));
326 bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8);
327 bct->mon = TOBCD(ct.mon);
328 bct->day = TOBCD(ct.day);
329 bct->hour = TOBCD(ct.hour);
330 bct->min = TOBCD(ct.min);
331 bct->sec = TOBCD(ct.sec);
340 KASSERT(nsdigits >= 0 && nsdigits <= 9, (
"bad nsdigits %d", nsdigits));
343 printf(
"%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
344 bct->year, bct->mon, bct->day,
345 bct->hour, bct->min, bct->sec,
346 nsdigits, nsdigits, bct->nsec /
nsdivisors[nsdigits]);
348 printf(
"%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
349 bct->year, bct->mon, bct->day,
350 bct->hour, bct->min, bct->sec);
358 KASSERT(nsdigits >= 0 && nsdigits <= 9, (
"bad nsdigits %d", nsdigits));
361 printf(
"%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
362 ct->year, ct->mon, ct->day,
363 ct->hour, ct->min, ct->sec,
364 nsdigits, nsdigits, ct->nsec /
nsdivisors[nsdigits]);
366 printf(
"%04d-%02d-%02d %02d:%02d:%02d",
367 ct->year, ct->mon, ct->day,
368 ct->hour, ct->min, ct->sec);
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN, &ct_debug, 0, "Enable printing of clocktime debugging")
static int wall_cmos_clock
static const int recent_base_year
void clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
static u_int nsdivisors[]
static const int recent_base_days
SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "Local offset from UTC in seconds")
void clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct)
int clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
#define days_in_month(y, m)
static const int month_days[12]
int clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm)
static int leapyear(int year)
void clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
#define day_of_week(days)
void clock_print_ts(const struct timespec *ts, int nsdigits)
void clock_print_ct(const struct clocktime *ct, int nsdigits)
static int sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
int printf(const char *fmt,...)