1/*
2 * ntp_calendar.h - definitions for the calendar time-of-day routine
3 */
4#ifndef NTP_CALENDAR_H
5#define NTP_CALENDAR_H
6
7#include "ntp_types.h"
8
9struct calendar {
10	u_short year;		/* year (A.D.) */
11	u_short yearday;	/* day of year, 1 = January 1 */
12	u_char month;		/* month, 1 = January */
13	u_char monthday;	/* day of month */
14	u_char hour;		/* hour of day, midnight = 0 */
15	u_char minute;		/* minute of hour */
16	u_char second;		/* second of minute */
17};
18
19/*
20 * Days in each month.  30 days hath September...
21 */
22#define	JAN	31
23#define	FEB	28
24#define	FEBLEAP	29
25#define	MAR	31
26#define	APR	30
27#define	MAY	31
28#define	JUN	30
29#define	JUL	31
30#define	AUG	31
31#define	SEP	30
32#define	OCT	31
33#define	NOV	30
34#define	DEC	31
35
36/*
37 * We deal in a 4 year cycle starting at March 1, 1900.  We assume
38 * we will only want to deal with dates since then, and not to exceed
39 * the rollover day in 2036.
40 */
41#define	SECSPERMIN	(60)			/* seconds per minute */
42#define	MINSPERHR	(60)			/* minutes per hour */
43#define	HRSPERDAY	(24)			/* hours per day */
44#define	DAYSPERYEAR	(365)			/* days per year */
45
46#define	SECSPERDAY	(SECSPERMIN*MINSPERHR*HRSPERDAY)
47#define	SECSPERYEAR	(365 * SECSPERDAY)	/* regular year */
48#define	SECSPERLEAPYEAR	(366 * SECSPERDAY)	/* leap year */
49
50#define	MAR1900		((JAN+FEB) * SECSPERDAY) /* no leap year in 1900 */
51#define	DAYSPERCYCLE	(365+365+365+366)	/* 3 normal years plus leap */
52#define	SECSPERCYCLE	(DAYSPERCYCLE*SECSPERDAY)
53#define	YEARSPERCYCLE	4
54
55/*
56 * Gross hacks.  I have illicit knowlege that there won't be overflows
57 * here, the compiler often can't tell this.
58 */
59#define	TIMES60(val)	((((val)<<4) - (val))<<2)	/* *(16 - 1) * 4 */
60#define	TIMES24(val)	(((val)<<4) + ((val)<<3))	/* *16 + *8 */
61#define	TIMES7(val)	(((val)<<3) - (val))		/* *8  - *1 */
62#define	TIMESDPERC(val)	(((val)<<10) + ((val)<<8) \
63			+ ((val)<<7) + ((val)<<5) \
64			+ ((val)<<4) + ((val)<<2) + (val))	/* *big* hack */
65
66/*
67 * Another big hack.  Cycle 22 started on March 1, 1988.  This is
68 * STARTCYCLE22 seconds after the start of cycle 0.
69 */
70#define	CYCLE22		(22)
71#define	STARTCYCLE22	(u_long)(0xa586b500)	/* 2777068800 */
72#define	MAR1988		(u_long)(STARTCYCLE22 + (u_long)MAR1900)
73
74/*
75 * The length of January + February in leap and non-leap years.
76 */
77#define	JANFEBNOLEAP	((JAN+FEB) * SECSPERDAY)
78#define	JANFEBLEAP	((JAN+FEBLEAP) * SECSPERDAY)
79
80
81extern	void	caljulian	(u_long, struct calendar *);
82extern	u_long	caltontp	(const struct calendar *);
83
84/*
85 * Additional support stuff for Ed Rheingold's calendrical calculations
86 */
87
88/*
89 * Start day of NTP time as days past the imaginary date 12/1/1 BC.
90 * (This is the beginning of the Christian Era, or BCE.)
91 */
92#define	DAY_NTP_STARTS 693596
93/*
94 * The Gregorian calendar is based on a 400 year cycle.  This is the number
95 * of days in each cycle.
96 */
97#define	GREGORIAN_CYCLE_DAYS 146097
98
99/*
100 * Days in a normal 100 year leap year calendar.  We lose a leap year day
101 * in years evenly divisible by 100 but not by 400.
102 */
103#define	GREGORIAN_NORMAL_CENTURY_DAYS 36524
104
105/*
106 * Days in a normal 4 year leap year calendar cycle.
107 */
108#define	GREGORIAN_NORMAL_LEAP_CYCLE_DAYS 1461
109
110#define	is_leapyear(y)	(!((y) % 4) && !(!((y) % 100) && (y) % 400))
111
112#endif
113