1998-03-06 21:17:13 +03:00
|
|
|
/* $NetBSD: caljulian.c,v 1.3 1998/03/06 18:17:14 christos Exp $ */
|
1998-01-09 06:15:09 +03:00
|
|
|
|
1997-04-18 17:22:49 +04:00
|
|
|
/*
|
|
|
|
* caljulian - determine the Julian date from an NTP time.
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "ntp_types.h"
|
|
|
|
#include "ntp_calendar.h"
|
|
|
|
#include "ntp_stdlib.h"
|
|
|
|
|
|
|
|
/*
|
1998-03-06 21:17:13 +03:00
|
|
|
* calmonthtab - days-in-the-month table
|
1997-04-18 17:22:49 +04:00
|
|
|
*/
|
1998-03-06 21:17:13 +03:00
|
|
|
static u_short calmonthtab[11] = {
|
|
|
|
JAN,
|
|
|
|
FEB,
|
|
|
|
MAR,
|
|
|
|
APR,
|
|
|
|
MAY,
|
|
|
|
JUN,
|
|
|
|
JUL,
|
|
|
|
AUG,
|
|
|
|
SEP,
|
|
|
|
OCT,
|
|
|
|
NOV
|
1997-04-18 17:22:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
caljulian(ntptime, jt)
|
1998-03-06 21:17:13 +03:00
|
|
|
u_long ntptime;
|
|
|
|
register struct calendar *jt;
|
1997-04-18 17:22:49 +04:00
|
|
|
{
|
1998-03-06 21:17:13 +03:00
|
|
|
u_long ntp_day;
|
|
|
|
u_long minutes;
|
|
|
|
/*
|
|
|
|
* Absolute, zero-adjusted Christian era day, starting from the
|
|
|
|
* mythical day 12/1/1 BC
|
|
|
|
*/
|
|
|
|
u_long acez_day;
|
|
|
|
|
|
|
|
u_long d400; /* Days into a Gregorian cycle */
|
|
|
|
u_long d100; /* Days into a normal century */
|
|
|
|
u_long d4; /* Days into a 4-year cycle */
|
|
|
|
u_long n400; /* # of Gregorian cycles */
|
|
|
|
u_long n100; /* # of normal centuries */
|
|
|
|
u_long n4; /* # of 4-year cycles */
|
|
|
|
u_long n1; /* # of years into a leap year */
|
|
|
|
/* cycle */
|
1997-04-18 17:22:49 +04:00
|
|
|
|
1998-03-06 21:17:13 +03:00
|
|
|
/*
|
|
|
|
* Do the easy stuff first: take care of hh:mm:ss, ignoring leap
|
|
|
|
* seconds
|
|
|
|
*/
|
|
|
|
jt->second = ntptime % SECSPERMIN;
|
|
|
|
minutes = ntptime / SECSPERMIN;
|
|
|
|
jt->minute = minutes % MINSPERHR;
|
|
|
|
jt->hour = (minutes / MINSPERHR) % HRSPERDAY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the day past 1900/01/01 00:00 UTC
|
|
|
|
*/
|
|
|
|
ntp_day = ntptime / SECSPERDAY;
|
|
|
|
acez_day = DAY_NTP_STARTS + ntp_day - 1;
|
|
|
|
n400 = acez_day/GREGORIAN_CYCLE_DAYS;
|
|
|
|
d400 = acez_day%GREGORIAN_CYCLE_DAYS;
|
|
|
|
n100 = d400 / GREGORIAN_NORMAL_CENTURY_DAYS;
|
|
|
|
d100 = d400 % GREGORIAN_NORMAL_CENTURY_DAYS;
|
|
|
|
n4 = d100 / GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
|
|
|
|
d4 = d100 % GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
|
|
|
|
n1 = d4 / DAYSPERYEAR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the year and year-of-day
|
|
|
|
*/
|
|
|
|
jt->yearday = 1 + d4%DAYSPERYEAR;
|
|
|
|
jt->year = 400*n400 + 100*n100 + n4*4 + n1;
|
|
|
|
|
|
|
|
if (n100 == 4 || n1 == 4)
|
|
|
|
{
|
1997-04-18 17:22:49 +04:00
|
|
|
/*
|
1998-03-06 21:17:13 +03:00
|
|
|
* If the cycle year ever comes out to 4, it must be December 31st
|
|
|
|
* of a leap year.
|
1997-04-18 17:22:49 +04:00
|
|
|
*/
|
1998-03-06 21:17:13 +03:00
|
|
|
jt->month = 12;
|
|
|
|
jt->monthday = 31;
|
|
|
|
jt->yearday = 366;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1997-04-18 17:22:49 +04:00
|
|
|
/*
|
1998-03-06 21:17:13 +03:00
|
|
|
* Else, search forwards through the months to get the right month
|
|
|
|
* and date.
|
1997-04-18 17:22:49 +04:00
|
|
|
*/
|
1998-03-06 21:17:13 +03:00
|
|
|
int monthday;
|
1997-04-18 17:22:49 +04:00
|
|
|
|
1998-03-06 21:17:13 +03:00
|
|
|
jt->year++;
|
|
|
|
monthday = jt->yearday;
|
1997-04-18 17:22:49 +04:00
|
|
|
|
1998-03-06 21:17:13 +03:00
|
|
|
for (jt->month=0;jt->month<11; jt->month++)
|
|
|
|
{
|
|
|
|
int t;
|
1997-04-18 17:22:49 +04:00
|
|
|
|
1998-03-06 21:17:13 +03:00
|
|
|
t = monthday - calmonthtab[jt->month];
|
|
|
|
if (jt->month == 1 && is_leapyear(jt->year))
|
|
|
|
t--;
|
|
|
|
|
|
|
|
if (t > 0)
|
|
|
|
monthday = t;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
jt->month++;
|
|
|
|
jt->monthday = monthday;
|
|
|
|
}
|
1997-04-18 17:22:49 +04:00
|
|
|
}
|