libc_time.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. {
  68. static const int secs_through_month[] = {
  69. 0, 31*86400, 59*86400, 90*86400,
  70. 120*86400, 151*86400, 181*86400, 212*86400,
  71. 243*86400, 273*86400, 304*86400, 334*86400 };
  72. int t = secs_through_month[month];
  73. if (is_leap && month >= 2) t+=86400;
  74. return t;
  75. }
  76. long long __year_to_secs(long long year, int *is_leap)
  77. {
  78. if (year-(int)2ULL <= 136) {
  79. int y = (int)year;
  80. int leaps = (y-68)>>2;
  81. if (!((y-68)&3)) {
  82. leaps--;
  83. if (is_leap) *is_leap = 1;
  84. } else if (is_leap) *is_leap = 0;
  85. return 31536000*(y-70) + 86400*leaps;
  86. }
  87. int cycles, centuries, leaps, rem;
  88. //if (!is_leap) is_leap = &(int){0};
  89. int is_leap_val = 0;
  90. if (!is_leap){
  91. is_leap = &is_leap_val;
  92. }
  93. cycles = (int)((year-100) / 400);
  94. rem = (int)((year-100) % 400);
  95. /* Comparison is always false because rem >= 0.
  96. if (rem < 0) {
  97. cycles--;
  98. rem += 400;
  99. } */
  100. if (!rem) {
  101. *is_leap = 1;
  102. centuries = 0;
  103. leaps = 0;
  104. } else {
  105. if (rem >= 200) {
  106. if (rem >= 300) centuries = 3, rem -= 300;
  107. else centuries = 2, rem -= 200;
  108. } else {
  109. if (rem >= 100) centuries = 1, rem -= 100;
  110. else centuries = 0;
  111. }
  112. if (!rem) {
  113. *is_leap = 0;
  114. leaps = 0;
  115. } else {
  116. leaps = (rem / (int)4U);
  117. rem %= (int)4U;
  118. *is_leap = !rem;
  119. }
  120. }
  121. leaps += 97*cycles + 24*centuries - *is_leap;
  122. return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
  123. }
  124. long long __tm_to_secs(const struct mytm *tm)
  125. {
  126. int is_leap;
  127. long long year = tm->tm_year;
  128. int month = tm->tm_mon;
  129. if (month >= 12 || month < 0) {
  130. int adj = month / 12;
  131. month %= 12;
  132. if (month < 0) {
  133. adj--;
  134. month += 12;
  135. }
  136. year += adj;
  137. }
  138. long long t = __year_to_secs(year, &is_leap);
  139. t += __month_to_secs(month, is_leap);
  140. t += 86400LL * (tm->tm_mday-1);
  141. t += 3600LL * tm->tm_hour;
  142. t += 60LL * tm->tm_min;
  143. t += tm->tm_sec;
  144. return t;
  145. }