#include <sys/types.h>
#include "ntp_types.h"
#include "ntp_calendar.h"
#include "ntp_stdlib.h"
static u_short calmonthtab[11] = {
JAN,
FEB,
MAR,
APR,
MAY,
JUN,
JUL,
AUG,
SEP,
OCT,
NOV
};
void
caljulian(
u_long ntptime,
register struct calendar *jt
)
{
u_long ntp_day;
u_long minutes;
u_long acez_day;
u_long d400;
u_long d100;
u_long d4;
u_long n400;
u_long n100;
u_long n4;
u_long n1;
jt->second = (u_char)(ntptime % SECSPERMIN);
minutes = ntptime / SECSPERMIN;
jt->minute = (u_char)(minutes % MINSPERHR);
jt->hour = (u_char)((minutes / MINSPERHR) % HRSPERDAY);
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;
jt->yearday = (u_short)(1 + d4%DAYSPERYEAR);
jt->year = (u_short)(400*n400 + 100*n100 + n4*4 + n1);
if (n100 == 4 || n1 == 4)
{
jt->month = 12;
jt->monthday = 31;
jt->yearday = 366;
}
else
{
int monthday;
jt->year++;
monthday = jt->yearday;
for (jt->month=0;jt->month<11; jt->month++)
{
int t;
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 = (u_char) monthday;
}
}