libc_time.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Originally released by the musl project (http://www.musl-libc.org/) under the
  2. * MIT license. Taken from the file /src/time/__secs_to_tm.c */
  3. #include <limits.h>
  4. #include "libc_time.h"
  5. /* 2000-03-01 (mod 400 year, immediately after feb29 */
  6. #define LEAPOCH (946684800LL + 86400*(31+29))
  7. #define DAYS_PER_400Y (365*400 + 97)
  8. #define DAYS_PER_100Y (365*100 + 24)
  9. #define DAYS_PER_4Y (365*4 + 1)
  10. int __secs_to_tm(long long t, struct mytm *tm) {
  11. long long days, secs, years;
  12. int remdays, remsecs, remyears;
  13. int qc_cycles, c_cycles, q_cycles;
  14. int months;
  15. int wday, yday, leap;
  16. static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
  17. /* Reject time_t values whose year would overflow int */
  18. if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
  19. return -1;
  20. secs = t - LEAPOCH;
  21. days = secs / 86400LL;
  22. remsecs = (int)(secs % 86400);
  23. if (remsecs < 0) {
  24. remsecs += 86400;
  25. --days;
  26. }
  27. wday = (int)((3+days)%7);
  28. if (wday < 0) wday += 7;
  29. qc_cycles = (int)(days / DAYS_PER_400Y);
  30. remdays = (int)(days % DAYS_PER_400Y);
  31. if (remdays < 0) {
  32. remdays += DAYS_PER_400Y;
  33. --qc_cycles;
  34. }
  35. c_cycles = remdays / DAYS_PER_100Y;
  36. if (c_cycles == 4) --c_cycles;
  37. remdays -= c_cycles * DAYS_PER_100Y;
  38. q_cycles = remdays / DAYS_PER_4Y;
  39. if (q_cycles == 25) --q_cycles;
  40. remdays -= q_cycles * DAYS_PER_4Y;
  41. remyears = remdays / 365;
  42. if (remyears == 4) --remyears;
  43. remdays -= remyears * 365;
  44. leap = !remyears && (q_cycles || !c_cycles);
  45. yday = remdays + 31 + 28 + leap;
  46. if (yday >= 365+leap) yday -= 365+leap;
  47. years = remyears + 4*q_cycles + 100*c_cycles + 400LL*qc_cycles;
  48. for (months=0; days_in_month[months] <= remdays; ++months)
  49. remdays -= days_in_month[months];
  50. if (years+100 > INT_MAX || years+100 < INT_MIN)
  51. return -1;
  52. tm->tm_year = (int)(years + 100);
  53. tm->tm_mon = months + 2;
  54. if (tm->tm_mon >= 12) {
  55. tm->tm_mon -=12;
  56. ++tm->tm_year;
  57. }
  58. tm->tm_mday = remdays + 1;
  59. tm->tm_wday = wday;
  60. tm->tm_yday = yday;
  61. tm->tm_hour = remsecs / 3600;
  62. tm->tm_min = remsecs / 60 % 60;
  63. tm->tm_sec = remsecs % 60;
  64. return 0;
  65. }
  66. int __month_to_secs(int month, int is_leap) {
  67. static const int secs_through_month[] =
  68. {0, 31*86400, 59*86400, 90*86400,
  69. 120*86400, 151*86400, 181*86400, 212*86400,
  70. 243*86400, 273*86400, 304*86400, 334*86400 };
  71. int t = secs_through_month[month];
  72. if (is_leap && month >= 2)
  73. t+=86400;
  74. return t;
  75. }
  76. long long __year_to_secs(long long year, int *is_leap) {
  77. if (year-(int)2ULL <= 136) {
  78. long long y = (int)year;
  79. long long leaps = (y-68)>>2;
  80. if (!((y-8)&3)) {
  81. leaps--;
  82. if (is_leap) *is_leap = 1;
  83. } else if (is_leap) *is_leap = 0;
  84. return 31536000*(y-70) + 86400*leaps;
  85. }
  86. int cycles, centuries, leaps, rem;
  87. //if (!is_leap) is_leap = &(int){0};
  88. int is_leap_val = 0;
  89. if (!is_leap) {
  90. is_leap = &is_leap_val;
  91. }
  92. cycles = (int)((year-100) / 400);
  93. rem = (int)((year-100) % 400);
  94. /* Comparison is always false because rem >= 0.
  95. if (rem < 0) {
  96. cycles--;
  97. rem += 400;
  98. } */
  99. if (!rem) {
  100. *is_leap = 1;
  101. centuries = 0;
  102. leaps = 0;
  103. } else {
  104. if (rem >= 200) {
  105. if (rem >= 300) centuries = 3, rem -= 300;
  106. else centuries = 2, rem -= 200;
  107. } else {
  108. if (rem >= 100) centuries = 1, rem -= 100;
  109. else centuries = 0;
  110. }
  111. if (!rem) {
  112. *is_leap = 0;
  113. leaps = 0;
  114. } else {
  115. leaps = (rem / (int)4U);
  116. rem %= (int)4U;
  117. *is_leap = !rem;
  118. }
  119. }
  120. leaps += 97*cycles + 24*centuries - *is_leap;
  121. return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
  122. }
  123. long long __tm_to_secs(const struct mytm *tm) {
  124. int is_leap;
  125. long long year = tm->tm_year;
  126. int month = tm->tm_mon;
  127. if (month >= 12 || month < 0) {
  128. int adj = month / 12;
  129. month %= 12;
  130. if (month < 0) {
  131. adj--;
  132. month += 12;
  133. }
  134. year += adj;
  135. }
  136. long long t = __year_to_secs(year, &is_leap);
  137. t += __month_to_secs(month, is_leap);
  138. t += 86400LL * (tm->tm_mday-1);
  139. t += 3600LL * tm->tm_hour;
  140. t += 60LL * tm->tm_min;
  141. t += tm->tm_sec;
  142. return t;
  143. }