154359Sroberto/*
254359Sroberto * caljulian - determine the Julian date from an NTP time.
354359Sroberto */
454359Sroberto#include <sys/types.h>
554359Sroberto
654359Sroberto#include "ntp_types.h"
754359Sroberto#include "ntp_calendar.h"
854359Sroberto#include "ntp_stdlib.h"
9182007Sroberto#include "ntp_fp.h"
1054359Sroberto
11182007Sroberto#if 0
1254359Sroberto/*
1354359Sroberto * calmonthtab - days-in-the-month table
1454359Sroberto */
1554359Srobertostatic u_short calmonthtab[11] = {
1654359Sroberto	JAN,
1754359Sroberto	FEB,
1854359Sroberto	MAR,
1954359Sroberto	APR,
2054359Sroberto	MAY,
2154359Sroberto	JUN,
2254359Sroberto	JUL,
2354359Sroberto	AUG,
2454359Sroberto	SEP,
2554359Sroberto	OCT,
2654359Sroberto	NOV
2754359Sroberto};
2854359Sroberto
2954359Srobertovoid
3054359Srobertocaljulian(
3154359Sroberto	u_long		  		ntptime,
3254359Sroberto	register struct calendar	*jt
3354359Sroberto	)
3454359Sroberto{
3554359Sroberto	u_long ntp_day;
3654359Sroberto	u_long minutes;
3754359Sroberto	/*
3854359Sroberto	 * Absolute, zero-adjusted Christian era day, starting from the
3954359Sroberto	 * mythical day 12/1/1 BC
4054359Sroberto	 */
4154359Sroberto	u_long acez_day;
4254359Sroberto
4354359Sroberto	u_long d400;				 /* Days into a Gregorian cycle */
4454359Sroberto	u_long d100;				 /* Days into a normal century */
4554359Sroberto	u_long d4;					 /* Days into a 4-year cycle */
4654359Sroberto	u_long n400;				 /* # of Gregorian cycles */
4754359Sroberto	u_long n100;				 /* # of normal centuries */
4854359Sroberto	u_long n4;					 /* # of 4-year cycles */
4954359Sroberto	u_long n1;					 /* # of years into a leap year */
5054359Sroberto						 /*   cycle */
5154359Sroberto
5254359Sroberto	/*
5354359Sroberto	 * Do the easy stuff first: take care of hh:mm:ss, ignoring leap
5454359Sroberto	 * seconds
5554359Sroberto	 */
5654359Sroberto	jt->second = (u_char)(ntptime % SECSPERMIN);
5754359Sroberto	minutes    = ntptime / SECSPERMIN;
5854359Sroberto	jt->minute = (u_char)(minutes % MINSPERHR);
5954359Sroberto	jt->hour   = (u_char)((minutes / MINSPERHR) % HRSPERDAY);
6054359Sroberto
6154359Sroberto	/*
6254359Sroberto	 * Find the day past 1900/01/01 00:00 UTC
6354359Sroberto	 */
6454359Sroberto	ntp_day = ntptime / SECSPERDAY;
6554359Sroberto	acez_day = DAY_NTP_STARTS + ntp_day - 1;
6654359Sroberto	n400	 = acez_day/GREGORIAN_CYCLE_DAYS;
6754359Sroberto	d400	 = acez_day%GREGORIAN_CYCLE_DAYS;
6854359Sroberto	n100	 = d400 / GREGORIAN_NORMAL_CENTURY_DAYS;
6954359Sroberto	d100	 = d400 % GREGORIAN_NORMAL_CENTURY_DAYS;
7054359Sroberto	n4		 = d100 / GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
7154359Sroberto	d4		 = d100 % GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
7254359Sroberto	n1		 = d4 / DAYSPERYEAR;
7354359Sroberto
7454359Sroberto	/*
7554359Sroberto	 * Calculate the year and year-of-day
7654359Sroberto	 */
7754359Sroberto	jt->yearday = (u_short)(1 + d4%DAYSPERYEAR);
7854359Sroberto	jt->year	= (u_short)(400*n400 + 100*n100 + n4*4 + n1);
7954359Sroberto
8054359Sroberto	if (n100 == 4 || n1 == 4)
8154359Sroberto	{
8254359Sroberto	/*
8354359Sroberto	 * If the cycle year ever comes out to 4, it must be December 31st
8454359Sroberto	 * of a leap year.
8554359Sroberto	 */
8654359Sroberto	jt->month	 = 12;
8754359Sroberto	jt->monthday = 31;
8854359Sroberto	jt->yearday  = 366;
8954359Sroberto	}
9054359Sroberto	else
9154359Sroberto	{
9254359Sroberto	/*
9354359Sroberto	 * Else, search forwards through the months to get the right month
9454359Sroberto	 * and date.
9554359Sroberto	 */
9654359Sroberto	int monthday;
9754359Sroberto
9854359Sroberto	jt->year++;
9954359Sroberto	monthday = jt->yearday;
10054359Sroberto
10154359Sroberto	for (jt->month=0;jt->month<11; jt->month++)
10254359Sroberto	{
10354359Sroberto		int t;
10454359Sroberto
10554359Sroberto		t = monthday - calmonthtab[jt->month];
10654359Sroberto		if (jt->month == 1 && is_leapyear(jt->year))
10754359Sroberto		t--;
10854359Sroberto
10954359Sroberto		if (t > 0)
11054359Sroberto		monthday = t;
11154359Sroberto		else
11254359Sroberto		break;
11354359Sroberto	}
11454359Sroberto	jt->month++;
115132451Sroberto	jt->monthday = (u_char) monthday;
11654359Sroberto	}
11754359Sroberto}
118182007Sroberto#else
119182007Sroberto
120182007Sroberto/* Updated 2003-12-30 TMa
121182007Sroberto
122182007Sroberto   Uses common code with the *prettydate functions to convert an ntp
123182007Sroberto   seconds count into a calendar date.
124182007Sroberto   Will handle ntp epoch wraparound as long as the underlying os/library
125182007Sroberto   does so for the unix epoch, i.e. works after 2038.
126182007Sroberto*/
127182007Sroberto
128182007Srobertovoid
129182007Srobertocaljulian(
130182007Sroberto	u_long		  		ntptime,
131182007Sroberto	register struct calendar	*jt
132182007Sroberto	)
133182007Sroberto{
134182007Sroberto	struct tm *tm;
135182007Sroberto
136182007Sroberto	tm = ntp2unix_tm(ntptime, 0);
137182007Sroberto
138182007Sroberto	jt->hour = (u_char) tm->tm_hour;
139182007Sroberto	jt->minute = (u_char) tm->tm_min;
140182007Sroberto	jt->month = (u_char) (tm->tm_mon + 1);
141182007Sroberto	jt->monthday = (u_char) tm->tm_mday;
142182007Sroberto	jt->second = (u_char) tm->tm_sec;
143182007Sroberto	jt->year = (u_short) (tm->tm_year + 1900);
144182007Sroberto	jt->yearday = (u_short) (tm->tm_yday + 1);  /* Assumes tm_yday starts with day 0! */
145182007Sroberto}
146182007Sroberto#endif
147