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