gregoimp.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (c) 2003-2008, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * Author: Alan Liu
  9. * Created: September 2 2003
  10. * Since: ICU 2.8
  11. **********************************************************************
  12. */
  13. #include "gregoimp.h"
  14. #if !UCONFIG_NO_FORMATTING
  15. #include "unicode/ucal.h"
  16. #include "uresimp.h"
  17. #include "cstring.h"
  18. #include "uassert.h"
  19. U_NAMESPACE_BEGIN
  20. int32_t ClockMath::floorDivide(int32_t numerator, int32_t denominator) {
  21. return (numerator >= 0) ?
  22. numerator / denominator : ((numerator + 1) / denominator) - 1;
  23. }
  24. int64_t ClockMath::floorDivide(int64_t numerator, int64_t denominator) {
  25. return (numerator >= 0) ?
  26. numerator / denominator : ((numerator + 1) / denominator) - 1;
  27. }
  28. int32_t ClockMath::floorDivide(double numerator, int32_t denominator,
  29. int32_t* remainder) {
  30. // For an integer n and representable ⌊x/n⌋, ⌊RN(x/n)⌋=⌊x/n⌋, where RN is
  31. // rounding to nearest.
  32. double quotient = uprv_floor(numerator / denominator);
  33. if (remainder != nullptr) {
  34. // For doubles x and n, where n is an integer and ⌊x+n⌋ < 2³¹, the
  35. // expression `(int32_t) (x + n)` evaluated with rounding to nearest
  36. // differs from ⌊x+n⌋ if 0 < ⌈x⌉−x ≪ x+n, as `x + n` is rounded up to
  37. // n+⌈x⌉ = ⌊x+n⌋ + 1. Rewriting it as ⌊x⌋+n makes the addition exact.
  38. *remainder = (int32_t) (uprv_floor(numerator) - (quotient * denominator));
  39. }
  40. return (int32_t) quotient;
  41. }
  42. double ClockMath::floorDivide(double dividend, double divisor,
  43. double* remainder) {
  44. // Only designed to work for positive divisors
  45. U_ASSERT(divisor > 0);
  46. double quotient = floorDivide(dividend, divisor);
  47. double r = dividend - (quotient * divisor);
  48. // N.B. For certain large dividends, on certain platforms, there
  49. // is a bug such that the quotient is off by one. If you doubt
  50. // this to be true, set a breakpoint below and run cintltst.
  51. if (r < 0 || r >= divisor) {
  52. // E.g. 6.7317038241449352e+022 / 86400000.0 is wrong on my
  53. // machine (too high by one). 4.1792057231752762e+024 /
  54. // 86400000.0 is wrong the other way (too low).
  55. double q = quotient;
  56. quotient += (r < 0) ? -1 : +1;
  57. if (q == quotient) {
  58. // For quotients > ~2^53, we won't be able to add or
  59. // subtract one, since the LSB of the mantissa will be >
  60. // 2^0; that is, the exponent (base 2) will be larger than
  61. // the length, in bits, of the mantissa. In that case, we
  62. // can't give a correct answer, so we set the remainder to
  63. // zero. This has the desired effect of making extreme
  64. // values give back an approximate answer rather than
  65. // crashing. For example, UDate values above a ~10^25
  66. // might all have a time of midnight.
  67. r = 0;
  68. } else {
  69. r = dividend - (quotient * divisor);
  70. }
  71. }
  72. U_ASSERT(0 <= r && r < divisor);
  73. if (remainder != nullptr) {
  74. *remainder = r;
  75. }
  76. return quotient;
  77. }
  78. const int32_t JULIAN_1_CE = 1721426; // January 1, 1 CE Gregorian
  79. const int32_t JULIAN_1970_CE = 2440588; // January 1, 1970 CE Gregorian
  80. const int16_t Grego::DAYS_BEFORE[24] =
  81. {0,31,59,90,120,151,181,212,243,273,304,334,
  82. 0,31,60,91,121,152,182,213,244,274,305,335};
  83. const int8_t Grego::MONTH_LENGTH[24] =
  84. {31,28,31,30,31,30,31,31,30,31,30,31,
  85. 31,29,31,30,31,30,31,31,30,31,30,31};
  86. double Grego::fieldsToDay(int32_t year, int32_t month, int32_t dom) {
  87. int32_t y = year - 1;
  88. double julian = 365 * y + ClockMath::floorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal
  89. ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2 + // => Gregorian cal
  90. DAYS_BEFORE[month + (isLeapYear(year) ? 12 : 0)] + dom; // => month/dom
  91. return julian - JULIAN_1970_CE; // JD => epoch day
  92. }
  93. void Grego::dayToFields(double day, int32_t& year, int32_t& month,
  94. int32_t& dom, int32_t& dow, int32_t& doy) {
  95. // Convert from 1970 CE epoch to 1 CE epoch (Gregorian calendar)
  96. day += JULIAN_1970_CE - JULIAN_1_CE;
  97. // Convert from the day number to the multiple radix
  98. // representation. We use 400-year, 100-year, and 4-year cycles.
  99. // For example, the 4-year cycle has 4 years + 1 leap day; giving
  100. // 1461 == 365*4 + 1 days.
  101. int32_t n400 = ClockMath::floorDivide(day, 146097, &doy); // 400-year cycle length
  102. int32_t n100 = ClockMath::floorDivide(doy, 36524, &doy); // 100-year cycle length
  103. int32_t n4 = ClockMath::floorDivide(doy, 1461, &doy); // 4-year cycle length
  104. int32_t n1 = ClockMath::floorDivide(doy, 365, &doy);
  105. year = 400*n400 + 100*n100 + 4*n4 + n1;
  106. if (n100 == 4 || n1 == 4) {
  107. doy = 365; // Dec 31 at end of 4- or 400-year cycle
  108. } else {
  109. ++year;
  110. }
  111. UBool isLeap = isLeapYear(year);
  112. // Gregorian day zero is a Monday.
  113. dow = (int32_t) uprv_fmod(day + 1, 7);
  114. dow += (dow < 0) ? (UCAL_SUNDAY + 7) : UCAL_SUNDAY;
  115. // Common Julian/Gregorian calculation
  116. int32_t correction = 0;
  117. int32_t march1 = isLeap ? 60 : 59; // zero-based DOY for March 1
  118. if (doy >= march1) {
  119. correction = isLeap ? 1 : 2;
  120. }
  121. month = (12 * (doy + correction) + 6) / 367; // zero-based month
  122. dom = doy - DAYS_BEFORE[month + (isLeap ? 12 : 0)] + 1; // one-based DOM
  123. doy++; // one-based doy
  124. }
  125. void Grego::timeToFields(UDate time, int32_t& year, int32_t& month,
  126. int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid) {
  127. double millisInDay;
  128. double day = ClockMath::floorDivide((double)time, (double)U_MILLIS_PER_DAY, &millisInDay);
  129. mid = (int32_t)millisInDay;
  130. dayToFields(day, year, month, dom, dow, doy);
  131. }
  132. int32_t Grego::dayOfWeek(double day) {
  133. int32_t dow;
  134. ClockMath::floorDivide(day + int{UCAL_THURSDAY}, 7, &dow);
  135. return (dow == 0) ? UCAL_SATURDAY : dow;
  136. }
  137. int32_t Grego::dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom) {
  138. int32_t weekInMonth = (dom + 6)/7;
  139. if (weekInMonth == 4) {
  140. if (dom + 7 > monthLength(year, month)) {
  141. weekInMonth = -1;
  142. }
  143. } else if (weekInMonth == 5) {
  144. weekInMonth = -1;
  145. }
  146. return weekInMonth;
  147. }
  148. U_NAMESPACE_END
  149. #endif
  150. //eof