Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / caljulian.c
blob9f868781af4d7e84799d8b45cda2d7b749fb2128
1 /* $NetBSD: caljulian.c,v 1.2 2003/12/04 16:23:36 drochner Exp $ */
3 /*
4 * caljulian - determine the Julian date from an NTP time.
5 */
6 #include <sys/types.h>
8 #include "ntp_types.h"
9 #include "ntp_calendar.h"
10 #include "ntp_stdlib.h"
11 #include "ntp_fp.h"
13 #if 0
15 * calmonthtab - days-in-the-month table
17 static u_short calmonthtab[11] = {
18 JAN,
19 FEB,
20 MAR,
21 APR,
22 MAY,
23 JUN,
24 JUL,
25 AUG,
26 SEP,
27 OCT,
28 NOV
31 void
32 caljulian(
33 u_long ntptime,
34 register struct calendar *jt
37 u_long ntp_day;
38 u_long minutes;
40 * Absolute, zero-adjusted Christian era day, starting from the
41 * mythical day 12/1/1 BC
43 u_long acez_day;
45 u_long d400; /* Days into a Gregorian cycle */
46 u_long d100; /* Days into a normal century */
47 u_long d4; /* Days into a 4-year cycle */
48 u_long n400; /* # of Gregorian cycles */
49 u_long n100; /* # of normal centuries */
50 u_long n4; /* # of 4-year cycles */
51 u_long n1; /* # of years into a leap year */
52 /* cycle */
55 * Do the easy stuff first: take care of hh:mm:ss, ignoring leap
56 * seconds
58 jt->second = (u_char)(ntptime % SECSPERMIN);
59 minutes = ntptime / SECSPERMIN;
60 jt->minute = (u_char)(minutes % MINSPERHR);
61 jt->hour = (u_char)((minutes / MINSPERHR) % HRSPERDAY);
64 * Find the day past 1900/01/01 00:00 UTC
66 ntp_day = ntptime / SECSPERDAY;
67 acez_day = DAY_NTP_STARTS + ntp_day - 1;
68 n400 = acez_day/GREGORIAN_CYCLE_DAYS;
69 d400 = acez_day%GREGORIAN_CYCLE_DAYS;
70 n100 = d400 / GREGORIAN_NORMAL_CENTURY_DAYS;
71 d100 = d400 % GREGORIAN_NORMAL_CENTURY_DAYS;
72 n4 = d100 / GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
73 d4 = d100 % GREGORIAN_NORMAL_LEAP_CYCLE_DAYS;
74 n1 = d4 / DAYSPERYEAR;
77 * Calculate the year and year-of-day
79 jt->yearday = (u_short)(1 + d4%DAYSPERYEAR);
80 jt->year = (u_short)(400*n400 + 100*n100 + n4*4 + n1);
82 if (n100 == 4 || n1 == 4)
85 * If the cycle year ever comes out to 4, it must be December 31st
86 * of a leap year.
88 jt->month = 12;
89 jt->monthday = 31;
90 jt->yearday = 366;
92 else
95 * Else, search forwards through the months to get the right month
96 * and date.
98 int monthday;
100 jt->year++;
101 monthday = jt->yearday;
103 for (jt->month=0;jt->month<11; jt->month++)
105 int t;
107 t = monthday - calmonthtab[jt->month];
108 if (jt->month == 1 && is_leapyear(jt->year))
109 t--;
111 if (t > 0)
112 monthday = t;
113 else
114 break;
116 jt->month++;
117 jt->monthday = (u_char) monthday;
120 #else
122 /* Updated 2003-12-30 TMa
124 Uses common code with the *prettydate functions to convert an ntp
125 seconds count into a calendar date.
126 Will handle ntp epoch wraparound as long as the underlying os/library
127 does so for the unix epoch, i.e. works after 2038.
130 void
131 caljulian(
132 u_long ntptime,
133 register struct calendar *jt
136 struct tm *tm;
138 tm = ntp2unix_tm(ntptime, 0);
140 jt->hour = (u_char) tm->tm_hour;
141 jt->minute = (u_char) tm->tm_min;
142 jt->month = (u_char) (tm->tm_mon + 1);
143 jt->monthday = (u_char) tm->tm_mday;
144 jt->second = (u_char) tm->tm_sec;
145 jt->year = (u_short) (tm->tm_year + 1900);
146 jt->yearday = (u_short) (tm->tm_yday + 1); /* Assumes tm_yday starts with day 0! */
148 #endif