/contrib/ntp/libntp/caljulian.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 146 lines · 94 code · 16 blank · 36 comment · 9 complexity · 4309566f8d6b855fd131435c0cb38bbf MD5 · raw file

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