2016-08-15 18:51:39 +03:00
|
|
|
/* $NetBSD: clock_subr.c,v 1.27 2016/08/15 15:51:39 jakllsch Exp $ */
|
1997-02-20 02:29:28 +03:00
|
|
|
|
|
|
|
/*
|
2011-02-08 23:20:06 +03:00
|
|
|
* Copyright (c) 1988 University of Utah.
|
1997-02-20 02:29:28 +03:00
|
|
|
* Copyright (c) 1982, 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* the Systems Programming Group of the University of Utah Computer
|
|
|
|
* Science Department.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* from: Utah $Hdr: clock.c 1.18 91/01/21$
|
|
|
|
*
|
|
|
|
* @(#)clock.c 8.2 (Berkeley) 1/12/94
|
|
|
|
*/
|
|
|
|
|
1997-02-20 02:29:28 +03:00
|
|
|
/*
|
|
|
|
* Generic routines to convert between a POSIX date
|
|
|
|
* (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
|
|
|
|
* Derived from arch/hp300/hp300/clock.c
|
|
|
|
*/
|
|
|
|
|
2014-09-01 16:25:52 +04:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif /* HAVE_NBTOOL_CONFIG_H */
|
|
|
|
|
2014-08-31 12:54:40 +04:00
|
|
|
#ifdef _KERNEL
|
2001-11-13 08:32:49 +03:00
|
|
|
#include <sys/cdefs.h>
|
2016-08-15 18:51:39 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.27 2016/08/15 15:51:39 jakllsch Exp $");
|
2001-11-13 08:32:49 +03:00
|
|
|
|
2003-08-13 15:35:25 +04:00
|
|
|
#include <sys/param.h>
|
1997-02-20 02:29:28 +03:00
|
|
|
#include <sys/systm.h>
|
2014-09-07 15:50:23 +04:00
|
|
|
#include <sys/errno.h>
|
2014-09-01 16:25:52 +04:00
|
|
|
#else /* ! _KERNEL */
|
2014-08-31 12:54:40 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2014-09-07 15:50:23 +04:00
|
|
|
#include <errno.h>
|
2014-09-01 16:25:52 +04:00
|
|
|
#endif /* ! _KERNEL */
|
1997-02-20 02:29:28 +03:00
|
|
|
|
2014-11-17 13:55:42 +03:00
|
|
|
#include "../sys/clock.h"
|
1997-02-20 02:29:28 +03:00
|
|
|
#include <dev/clock_subr.h>
|
|
|
|
|
|
|
|
#define FEBRUARY 2
|
|
|
|
|
2014-09-06 22:04:28 +04:00
|
|
|
/* for easier alignment:
|
2016-08-15 18:51:39 +03:00
|
|
|
* time from the epoch to 2001 (there were 8 leap years): */
|
|
|
|
#define DAYSTO2001 (365*31+8)
|
2014-09-06 22:04:28 +04:00
|
|
|
|
|
|
|
/* 4 year intervals include 1 leap year */
|
|
|
|
#define DAYS4YEARS (365*4+1)
|
|
|
|
|
|
|
|
/* 100 year intervals include 24 leap years */
|
|
|
|
#define DAYS100YEARS (365*100+24)
|
|
|
|
|
|
|
|
/* 400 year intervals include 97 leap years */
|
|
|
|
#define DAYS400YEARS (365*400+97)
|
|
|
|
|
1997-02-20 02:29:28 +03:00
|
|
|
time_t
|
2009-02-14 23:32:29 +03:00
|
|
|
clock_ymdhms_to_secs(struct clock_ymdhms *dt)
|
1997-02-20 02:29:28 +03:00
|
|
|
{
|
2014-09-07 15:50:23 +04:00
|
|
|
uint64_t secs, i, year, days;
|
1997-02-20 02:29:28 +03:00
|
|
|
|
|
|
|
year = dt->dt_year;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute days since start of time
|
|
|
|
* First from years, then from months.
|
|
|
|
*/
|
2009-12-12 14:16:33 +03:00
|
|
|
if (year < POSIX_BASE_YEAR)
|
|
|
|
return -1;
|
1997-02-20 02:29:28 +03:00
|
|
|
days = 0;
|
2014-11-17 05:15:48 +03:00
|
|
|
if (is_leap_year(year) && dt->dt_mon > FEBRUARY)
|
1997-02-20 02:29:28 +03:00
|
|
|
days++;
|
|
|
|
|
2016-08-15 18:51:39 +03:00
|
|
|
if (year < 2001) {
|
2014-09-06 22:04:28 +04:00
|
|
|
/* simple way for early years */
|
|
|
|
for (i = POSIX_BASE_YEAR; i < year; i++)
|
2014-11-17 05:15:48 +03:00
|
|
|
days += days_per_year(i);
|
2014-09-06 22:04:28 +04:00
|
|
|
} else {
|
|
|
|
/* years are properly aligned */
|
2016-08-15 18:51:39 +03:00
|
|
|
days += DAYSTO2001;
|
|
|
|
year -= 2001;
|
2014-09-06 22:04:28 +04:00
|
|
|
|
|
|
|
i = year / 400;
|
|
|
|
days += i * DAYS400YEARS;
|
|
|
|
year -= i * 400;
|
|
|
|
|
|
|
|
i = year / 100;
|
|
|
|
days += i * DAYS100YEARS;
|
|
|
|
year -= i * 100;
|
|
|
|
|
|
|
|
i = year / 4;
|
|
|
|
days += i * DAYS4YEARS;
|
|
|
|
year -= i * 4;
|
|
|
|
|
|
|
|
for (i = dt->dt_year-year; i < dt->dt_year; i++)
|
2014-11-17 05:23:33 +03:00
|
|
|
days += days_per_year(i);
|
2014-09-06 22:04:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-02-20 02:29:28 +03:00
|
|
|
/* Months */
|
|
|
|
for (i = 1; i < dt->dt_mon; i++)
|
|
|
|
days += days_in_month(i);
|
|
|
|
days += (dt->dt_day - 1);
|
|
|
|
|
|
|
|
/* Add hours, minutes, seconds. */
|
2004-12-29 23:55:57 +03:00
|
|
|
secs = (((uint64_t)days
|
1997-02-20 02:29:28 +03:00
|
|
|
* 24 + dt->dt_hour)
|
|
|
|
* 60 + dt->dt_min)
|
|
|
|
* 60 + dt->dt_sec;
|
|
|
|
|
2014-09-01 15:21:29 +04:00
|
|
|
if ((time_t)secs < 0 || secs > __type_max(time_t))
|
2009-12-12 14:16:33 +03:00
|
|
|
return -1;
|
|
|
|
return secs;
|
1997-02-20 02:29:28 +03:00
|
|
|
}
|
|
|
|
|
2014-09-07 15:50:23 +04:00
|
|
|
int
|
2009-02-14 23:32:29 +03:00
|
|
|
clock_secs_to_ymdhms(time_t secs, struct clock_ymdhms *dt)
|
1997-02-20 02:29:28 +03:00
|
|
|
{
|
2014-09-07 15:50:23 +04:00
|
|
|
int leap;
|
|
|
|
uint64_t i;
|
2009-12-12 14:22:59 +03:00
|
|
|
time_t days;
|
|
|
|
time_t rsec; /* remainder seconds */
|
1997-02-20 02:29:28 +03:00
|
|
|
|
2014-09-07 15:50:23 +04:00
|
|
|
if (secs < 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
2014-11-17 05:15:48 +03:00
|
|
|
days = secs / SECS_PER_DAY;
|
|
|
|
rsec = secs % SECS_PER_DAY;
|
1997-02-20 02:29:28 +03:00
|
|
|
|
|
|
|
/* Day of week (Note: 1/1/1970 was a Thursday) */
|
|
|
|
dt->dt_wday = (days + 4) % 7;
|
|
|
|
|
2016-08-15 18:51:39 +03:00
|
|
|
if (days >= DAYSTO2001) {
|
|
|
|
days -= DAYSTO2001;
|
|
|
|
dt->dt_year = 2001;
|
2014-09-06 22:04:28 +04:00
|
|
|
|
|
|
|
i = days / DAYS400YEARS;
|
|
|
|
days -= i*DAYS400YEARS;
|
|
|
|
dt->dt_year += i*400;
|
|
|
|
|
|
|
|
i = days / DAYS100YEARS;
|
|
|
|
days -= i*DAYS100YEARS;
|
|
|
|
dt->dt_year += i*100;
|
|
|
|
|
|
|
|
i = days / DAYS4YEARS;
|
|
|
|
days -= i*DAYS4YEARS;
|
|
|
|
dt->dt_year += i*4;
|
|
|
|
|
2014-11-17 05:15:48 +03:00
|
|
|
for (i = dt->dt_year; days >= days_per_year(i); i++)
|
|
|
|
days -= days_per_year(i);
|
2014-09-06 22:04:28 +04:00
|
|
|
dt->dt_year = i;
|
|
|
|
} else {
|
|
|
|
/* Subtract out whole years, counting them in i. */
|
2014-11-17 05:15:48 +03:00
|
|
|
for (i = POSIX_BASE_YEAR; days >= days_per_year(i); i++)
|
|
|
|
days -= days_per_year(i);
|
2014-09-06 22:04:28 +04:00
|
|
|
dt->dt_year = i;
|
|
|
|
}
|
1997-02-20 02:29:28 +03:00
|
|
|
|
|
|
|
/* Subtract out whole months, counting them in i. */
|
2014-09-06 22:04:28 +04:00
|
|
|
for (leap = 0, i = 1; days >= days_in_month(i)+leap; i++) {
|
|
|
|
days -= days_in_month(i)+leap;
|
2014-11-17 05:15:48 +03:00
|
|
|
if (i == 1 && is_leap_year(dt->dt_year))
|
2014-09-06 22:04:28 +04:00
|
|
|
leap = 1;
|
|
|
|
else
|
|
|
|
leap = 0;
|
|
|
|
}
|
1997-02-20 02:29:28 +03:00
|
|
|
dt->dt_mon = i;
|
|
|
|
|
|
|
|
/* Days are what is left over (+1) from all that. */
|
|
|
|
dt->dt_day = days + 1;
|
|
|
|
|
|
|
|
/* Hours, minutes, seconds are easy */
|
2014-12-22 21:09:20 +03:00
|
|
|
dt->dt_hour = rsec / SECS_PER_HOUR;
|
|
|
|
rsec = rsec % SECS_PER_HOUR;
|
|
|
|
dt->dt_min = rsec / SECS_PER_MINUTE;
|
|
|
|
rsec = rsec % SECS_PER_MINUTE;
|
1997-02-20 02:29:28 +03:00
|
|
|
dt->dt_sec = rsec;
|
2014-09-07 15:50:23 +04:00
|
|
|
|
|
|
|
return 0;
|
1997-02-20 02:29:28 +03:00
|
|
|
}
|