154359Sroberto/* 254359Sroberto * ntp_calendar.h - definitions for the calendar time-of-day routine 354359Sroberto */ 454359Sroberto#ifndef NTP_CALENDAR_H 554359Sroberto#define NTP_CALENDAR_H 654359Sroberto 754359Sroberto#include "ntp_types.h" 854359Sroberto 954359Srobertostruct calendar { 1054359Sroberto u_short year; /* year (A.D.) */ 1154359Sroberto u_short yearday; /* day of year, 1 = January 1 */ 1254359Sroberto u_char month; /* month, 1 = January */ 1354359Sroberto u_char monthday; /* day of month */ 1454359Sroberto u_char hour; /* hour of day, midnight = 0 */ 1554359Sroberto u_char minute; /* minute of hour */ 1654359Sroberto u_char second; /* second of minute */ 1754359Sroberto}; 1854359Sroberto 1954359Sroberto/* 2054359Sroberto * Days in each month. 30 days hath September... 2154359Sroberto */ 2254359Sroberto#define JAN 31 2354359Sroberto#define FEB 28 2454359Sroberto#define FEBLEAP 29 2554359Sroberto#define MAR 31 2654359Sroberto#define APR 30 2754359Sroberto#define MAY 31 2854359Sroberto#define JUN 30 2954359Sroberto#define JUL 31 3054359Sroberto#define AUG 31 3154359Sroberto#define SEP 30 3254359Sroberto#define OCT 31 3354359Sroberto#define NOV 30 3454359Sroberto#define DEC 31 3554359Sroberto 3654359Sroberto/* 3754359Sroberto * We deal in a 4 year cycle starting at March 1, 1900. We assume 3854359Sroberto * we will only want to deal with dates since then, and not to exceed 3954359Sroberto * the rollover day in 2036. 4054359Sroberto */ 4154359Sroberto#define SECSPERMIN (60) /* seconds per minute */ 4254359Sroberto#define MINSPERHR (60) /* minutes per hour */ 4354359Sroberto#define HRSPERDAY (24) /* hours per day */ 4454359Sroberto#define DAYSPERYEAR (365) /* days per year */ 4554359Sroberto 4654359Sroberto#define SECSPERDAY (SECSPERMIN*MINSPERHR*HRSPERDAY) 4754359Sroberto#define SECSPERYEAR (365 * SECSPERDAY) /* regular year */ 4854359Sroberto#define SECSPERLEAPYEAR (366 * SECSPERDAY) /* leap year */ 4954359Sroberto 5054359Sroberto#define MAR1900 ((JAN+FEB) * SECSPERDAY) /* no leap year in 1900 */ 5154359Sroberto#define DAYSPERCYCLE (365+365+365+366) /* 3 normal years plus leap */ 5254359Sroberto#define SECSPERCYCLE (DAYSPERCYCLE*SECSPERDAY) 5354359Sroberto#define YEARSPERCYCLE 4 5454359Sroberto 5554359Sroberto/* 5654359Sroberto * Gross hacks. I have illicit knowlege that there won't be overflows 5754359Sroberto * here, the compiler often can't tell this. 5854359Sroberto */ 5954359Sroberto#define TIMES60(val) ((((val)<<4) - (val))<<2) /* *(16 - 1) * 4 */ 6054359Sroberto#define TIMES24(val) (((val)<<4) + ((val)<<3)) /* *16 + *8 */ 6154359Sroberto#define TIMES7(val) (((val)<<3) - (val)) /* *8 - *1 */ 6254359Sroberto#define TIMESDPERC(val) (((val)<<10) + ((val)<<8) \ 6354359Sroberto + ((val)<<7) + ((val)<<5) \ 6454359Sroberto + ((val)<<4) + ((val)<<2) + (val)) /* *big* hack */ 6554359Sroberto 6654359Sroberto/* 6754359Sroberto * Another big hack. Cycle 22 started on March 1, 1988. This is 6854359Sroberto * STARTCYCLE22 seconds after the start of cycle 0. 6954359Sroberto */ 7054359Sroberto#define CYCLE22 (22) 7154359Sroberto#define STARTCYCLE22 (u_long)(0xa586b500) /* 2777068800 */ 7254359Sroberto#define MAR1988 (u_long)(STARTCYCLE22 + (u_long)MAR1900) 7354359Sroberto 7454359Sroberto/* 7554359Sroberto * The length of January + February in leap and non-leap years. 7654359Sroberto */ 7754359Sroberto#define JANFEBNOLEAP ((JAN+FEB) * SECSPERDAY) 7854359Sroberto#define JANFEBLEAP ((JAN+FEBLEAP) * SECSPERDAY) 7954359Sroberto 8054359Sroberto 8154359Srobertoextern void caljulian P((u_long, struct calendar *)); 8254359Srobertoextern u_long caltontp P((const struct calendar *)); 8354359Sroberto 8454359Sroberto/* 8554359Sroberto * Additional support stuff for Ed Rheingold's calendrical calculations 8654359Sroberto */ 8754359Sroberto 8854359Sroberto/* 8954359Sroberto * Start day of NTP time as days past the imaginary date 12/1/1 BC. 9054359Sroberto * P((This is the beginning of the Christian Era, or BCE.)) 9154359Sroberto */ 9254359Sroberto#define DAY_NTP_STARTS 693596 9354359Sroberto/* 9454359Sroberto * The Gregorian calendar is based on a 400 year cycle. This is the number 9554359Sroberto * of days in each cycle. 9654359Sroberto */ 9754359Sroberto#define GREGORIAN_CYCLE_DAYS 146097 9854359Sroberto 9954359Sroberto/* 10054359Sroberto * Days in a normal 100 year leap year calendar. We lose a leap year day 10154359Sroberto * in years evenly divisible by 100 but not by 400. 10254359Sroberto */ 10354359Sroberto#define GREGORIAN_NORMAL_CENTURY_DAYS 36524 10454359Sroberto 10554359Sroberto/* 10654359Sroberto * Days in a normal 4 year leap year calendar cycle. 10754359Sroberto */ 10854359Sroberto#define GREGORIAN_NORMAL_LEAP_CYCLE_DAYS 1461 10954359Sroberto 11054359Sroberto#define is_leapyear(y) (y%4 == 0 && !(y%100 == 0 && !(y%400 == 0))) 11154359Sroberto 11254359Sroberto#endif 113