/contrib/ntp/include/ntp_calendar.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 112 lines · 55 code · 17 blank · 40 comment · 5 complexity · 99d272aa103d7893afa86768f72e9df3 MD5 · raw file

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