Replace the existing print_ct() private debugging function with a set of

three public functions to format and print the three major data structures
used by realtime clock drivers (clocktime, bcd_clocktime, and timespec).
This commit is contained in:
Ian Lepore 2018-02-12 16:25:56 +00:00
parent 5ec69a1509
commit aedc51f11a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=329170
2 changed files with 68 additions and 14 deletions

View File

@ -107,6 +107,14 @@ static const int month_days[12] = {
static const int recent_base_year = 2017;
static const int recent_base_days = 17167;
/*
* Table to 'calculate' pow(10, 9 - nsdigits) via lookup of nsdigits.
* Before doing the lookup, the code asserts 0 <= nsdigits <= 9.
*/
static u_int nsdivisors[] = {
1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
};
/*
* This inline avoids some unnecessary modulo operations
* as compared with the usual macro:
@ -131,23 +139,15 @@ leapyear(int year)
return (rv);
}
static void
print_ct(const struct clocktime *ct)
{
printf("[%04d-%02d-%02d %02d:%02d:%02d]",
ct->year, ct->mon, ct->day,
ct->hour, ct->min, ct->sec);
}
int
clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
{
int i, year, days;
if (ct_debug) {
printf("ct_to_ts(");
print_ct(ct);
printf(")");
printf("ct_to_ts([");
clock_print_ct(ct, 9);
printf("])");
}
/*
@ -288,10 +288,10 @@ clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct)
ct->sec = rsec;
ct->nsec = ts->tv_nsec;
if (ct_debug) {
printf("ts_to_ct(%jd.%09ld) = ",
printf("ts_to_ct(%jd.%09ld) = [",
(intmax_t)ts->tv_sec, ts->tv_nsec);
print_ct(ct);
printf("\n");
clock_print_ct(ct, 9);
printf("]\n");
}
KASSERT(ct->year >= 0 && ct->year < 10000,
@ -337,6 +337,51 @@ clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
bct->nsec = ct.nsec;
}
void
clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
{
KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
if (nsdigits > 0) {
printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
bct->year, bct->mon, bct->day,
bct->hour, bct->min, bct->sec,
nsdigits, nsdigits, bct->nsec / nsdivisors[nsdigits]);
} else {
printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
bct->year, bct->mon, bct->day,
bct->hour, bct->min, bct->sec);
}
}
void
clock_print_ct(const struct clocktime *ct, int nsdigits)
{
KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
if (nsdigits > 0) {
printf("%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
ct->year, ct->mon, ct->day,
ct->hour, ct->min, ct->sec,
nsdigits, nsdigits, ct->nsec / nsdivisors[nsdigits]);
} else {
printf("%04d-%02d-%02d %02d:%02d:%02d",
ct->year, ct->mon, ct->day,
ct->hour, ct->min, ct->sec);
}
}
void
clock_print_ts(const struct timespec *ts, int nsdigits)
{
struct clocktime ct;
clock_ts_to_ct(ts, &ct);
clock_print_ct(&ct, nsdigits);
}
int
utc_offset(void)
{

View File

@ -182,6 +182,15 @@ void timespec2fattime(const struct timespec *tsp, int utc, u_int16_t *ddp,
void fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc,
struct timespec *tsp);
/*
* Print a [bcd_]clocktime or timespec, optionally with fractional seconds. The
* nsdig argument can range from 0-9, and specifies how many decimal digits to
* display for fractional seconds.
*/
void clock_print_bcd(const struct bcd_clocktime *bct, int nsdig);
void clock_print_ct(const struct clocktime *ct, int nsdig);
void clock_print_ts(const struct timespec *ts, int nsdig);
#endif /* _KERNEL */
#endif /* !_SYS_CLOCK_H_ */