Deleted Added
full compact
subr_clock.c (331496) subr_clock.c (331503)
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by

--- 27 unchanged lines hidden (view full) ---

36 * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
38 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
39 * and
40 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
41 */
42
43#include <sys/cdefs.h>
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by

--- 27 unchanged lines hidden (view full) ---

36 * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
38 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
39 * and
40 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: stable/11/sys/kern/subr_clock.c 331496 2018-03-24 20:40:16Z ian $");
44__FBSDID("$FreeBSD: stable/11/sys/kern/subr_clock.c 331503 2018-03-24 23:01:10Z ian $");
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/bus.h>
50#include <sys/clock.h>
51#include <sys/limits.h>
52#include <sys/sysctl.h>

--- 50 unchanged lines hidden (view full) ---

103 * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and
104 * some recent year avoids lots of unnecessary loop iterations in conversion.
105 * recent_base_days is the number of days before the start of recent_base_year.
106 */
107static const int recent_base_year = 2017;
108static const int recent_base_days = 17167;
109
110/*
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/bus.h>
50#include <sys/clock.h>
51#include <sys/limits.h>
52#include <sys/sysctl.h>

--- 50 unchanged lines hidden (view full) ---

103 * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and
104 * some recent year avoids lots of unnecessary loop iterations in conversion.
105 * recent_base_days is the number of days before the start of recent_base_year.
106 */
107static const int recent_base_year = 2017;
108static const int recent_base_days = 17167;
109
110/*
111 * Table to 'calculate' pow(10, 9 - nsdigits) via lookup of nsdigits.
112 * Before doing the lookup, the code asserts 0 <= nsdigits <= 9.
113 */
114static u_int nsdivisors[] = {
115 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
116};
117
118/*
111 * This inline avoids some unnecessary modulo operations
112 * as compared with the usual macro:
113 * ( ((year % 4) == 0 &&
114 * (year % 100) != 0) ||
115 * ((year % 400) == 0) )
116 * It is otherwise equivalent.
117 */
118static int

--- 7 unchanged lines hidden (view full) ---

126 rv = 0;
127 if ((year % 400) == 0)
128 rv = 1;
129 }
130 }
131 return (rv);
132}
133
119 * This inline avoids some unnecessary modulo operations
120 * as compared with the usual macro:
121 * ( ((year % 4) == 0 &&
122 * (year % 100) != 0) ||
123 * ((year % 400) == 0) )
124 * It is otherwise equivalent.
125 */
126static int

--- 7 unchanged lines hidden (view full) ---

134 rv = 0;
135 if ((year % 400) == 0)
136 rv = 1;
137 }
138 }
139 return (rv);
140}
141
134static void
135print_ct(struct clocktime *ct)
136{
137 printf("[%04d-%02d-%02d %02d:%02d:%02d]",
138 ct->year, ct->mon, ct->day,
139 ct->hour, ct->min, ct->sec);
140}
141
142int
142int
143clock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
143clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
144{
145 int i, year, days;
146
147 if (ct_debug) {
144{
145 int i, year, days;
146
147 if (ct_debug) {
148 printf("ct_to_ts(");
149 print_ct(ct);
150 printf(")");
148 printf("ct_to_ts([");
149 clock_print_ct(ct, 9);
150 printf("])");
151 }
152
153 /*
154 * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to
155 * determine century. Some clocks have a "century bit" and drivers do
156 * year += 100, so interpret values between 70-199 as relative to 1900.
157 */
158 year = ct->year;

--- 36 unchanged lines hidden (view full) ---

195 ts->tv_nsec = ct->nsec;
196
197 if (ct_debug)
198 printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec);
199 return (0);
200}
201
202int
151 }
152
153 /*
154 * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to
155 * determine century. Some clocks have a "century bit" and drivers do
156 * year += 100, so interpret values between 70-199 as relative to 1900.
157 */
158 year = ct->year;

--- 36 unchanged lines hidden (view full) ---

195 ts->tv_nsec = ct->nsec;
196
197 if (ct_debug)
198 printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec);
199 return (0);
200}
201
202int
203clock_bcd_to_ts(struct bcd_clocktime *bct, struct timespec *ts, bool ampm)
203clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm)
204{
205 struct clocktime ct;
206 int bcent, byear;
207
208 /*
209 * Year may come in as 2-digit or 4-digit BCD. Split the value into
210 * separate BCD century and year values for validation and conversion.
211 */

--- 32 unchanged lines hidden (view full) ---

244 if (bct->ispm)
245 ct.hour += 12;
246 }
247
248 return (clock_ct_to_ts(&ct, ts));
249}
250
251void
204{
205 struct clocktime ct;
206 int bcent, byear;
207
208 /*
209 * Year may come in as 2-digit or 4-digit BCD. Split the value into
210 * separate BCD century and year values for validation and conversion.
211 */

--- 32 unchanged lines hidden (view full) ---

244 if (bct->ispm)
245 ct.hour += 12;
246 }
247
248 return (clock_ct_to_ts(&ct, ts));
249}
250
251void
252clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
252clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct)
253{
254 int i, year, days;
255 time_t rsec; /* remainder seconds */
256 time_t secs;
257
258 secs = ts->tv_sec;
259 days = secs / SECDAY;
260 rsec = secs % SECDAY;

--- 22 unchanged lines hidden (view full) ---

283 /* Hours, minutes, seconds are easy */
284 ct->hour = rsec / 3600;
285 rsec = rsec % 3600;
286 ct->min = rsec / 60;
287 rsec = rsec % 60;
288 ct->sec = rsec;
289 ct->nsec = ts->tv_nsec;
290 if (ct_debug) {
253{
254 int i, year, days;
255 time_t rsec; /* remainder seconds */
256 time_t secs;
257
258 secs = ts->tv_sec;
259 days = secs / SECDAY;
260 rsec = secs % SECDAY;

--- 22 unchanged lines hidden (view full) ---

283 /* Hours, minutes, seconds are easy */
284 ct->hour = rsec / 3600;
285 rsec = rsec % 3600;
286 ct->min = rsec / 60;
287 rsec = rsec % 60;
288 ct->sec = rsec;
289 ct->nsec = ts->tv_nsec;
290 if (ct_debug) {
291 printf("ts_to_ct(%jd.%09ld) = ",
291 printf("ts_to_ct(%jd.%09ld) = [",
292 (intmax_t)ts->tv_sec, ts->tv_nsec);
292 (intmax_t)ts->tv_sec, ts->tv_nsec);
293 print_ct(ct);
294 printf("\n");
293 clock_print_ct(ct, 9);
294 printf("]\n");
295 }
296}
297
298void
295 }
296}
297
298void
299clock_ts_to_bcd(struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
299clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
300{
301 struct clocktime ct;
302
303 clock_ts_to_ct(ts, &ct);
304
305 /* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */
306 bct->ispm = false;
307 if (ampm) {

--- 10 unchanged lines hidden (view full) ---

318 bct->day = TOBCD(ct.day);
319 bct->hour = TOBCD(ct.hour);
320 bct->min = TOBCD(ct.min);
321 bct->sec = TOBCD(ct.sec);
322 bct->dow = ct.dow;
323 bct->nsec = ct.nsec;
324}
325
300{
301 struct clocktime ct;
302
303 clock_ts_to_ct(ts, &ct);
304
305 /* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */
306 bct->ispm = false;
307 if (ampm) {

--- 10 unchanged lines hidden (view full) ---

318 bct->day = TOBCD(ct.day);
319 bct->hour = TOBCD(ct.hour);
320 bct->min = TOBCD(ct.min);
321 bct->sec = TOBCD(ct.sec);
322 bct->dow = ct.dow;
323 bct->nsec = ct.nsec;
324}
325
326void
327clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
328{
329
330 KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
331
332 if (nsdigits > 0) {
333 printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
334 bct->year, bct->mon, bct->day,
335 bct->hour, bct->min, bct->sec,
336 nsdigits, nsdigits, bct->nsec / nsdivisors[nsdigits]);
337 } else {
338 printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
339 bct->year, bct->mon, bct->day,
340 bct->hour, bct->min, bct->sec);
341 }
342}
343
344void
345clock_print_ct(const struct clocktime *ct, int nsdigits)
346{
347
348 KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
349
350 if (nsdigits > 0) {
351 printf("%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
352 ct->year, ct->mon, ct->day,
353 ct->hour, ct->min, ct->sec,
354 nsdigits, nsdigits, ct->nsec / nsdivisors[nsdigits]);
355 } else {
356 printf("%04d-%02d-%02d %02d:%02d:%02d",
357 ct->year, ct->mon, ct->day,
358 ct->hour, ct->min, ct->sec);
359 }
360}
361
362void
363clock_print_ts(const struct timespec *ts, int nsdigits)
364{
365 struct clocktime ct;
366
367 clock_ts_to_ct(ts, &ct);
368 clock_print_ct(&ct, nsdigits);
369}
370
326int
327utc_offset(void)
328{
329
330 return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
331}
371int
372utc_offset(void)
373{
374
375 return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
376}