gregoimp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // Copyright (C) 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. #ifndef GREGOIMP_H
  14. #define GREGOIMP_H
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_FORMATTING
  17. #include "unicode/ures.h"
  18. #include "unicode/locid.h"
  19. #include "putilimp.h"
  20. U_NAMESPACE_BEGIN
  21. /**
  22. * A utility class providing mathematical functions used by time zone
  23. * and calendar code. Do not instantiate. Formerly just named 'Math'.
  24. * @internal
  25. */
  26. class ClockMath {
  27. public:
  28. /**
  29. * Divide two integers, returning the floor of the quotient.
  30. * Unlike the built-in division, this is mathematically
  31. * well-behaved. E.g., <code>-1/4</code> => 0 but
  32. * <code>floorDivide(-1,4)</code> => -1.
  33. * @param numerator the numerator
  34. * @param denominator a divisor which must be != 0
  35. * @return the floor of the quotient
  36. */
  37. static int32_t floorDivide(int32_t numerator, int32_t denominator);
  38. /**
  39. * Divide two numbers, returning the floor of the quotient.
  40. * Unlike the built-in division, this is mathematically
  41. * well-behaved. E.g., <code>-1/4</code> => 0 but
  42. * <code>floorDivide(-1,4)</code> => -1.
  43. * @param numerator the numerator
  44. * @param denominator a divisor which must be != 0
  45. * @return the floor of the quotient
  46. */
  47. static inline double floorDivide(double numerator, double denominator);
  48. /**
  49. * Divide two numbers, returning the floor of the quotient and
  50. * the modulus remainder. Unlike the built-in division, this is
  51. * mathematically well-behaved. E.g., <code>-1/4</code> => 0 and
  52. * <code>-1%4</code> => -1, but <code>floorDivide(-1,4)</code> =>
  53. * -1 with <code>remainder</code> => 3. NOTE: If numerator is
  54. * too large, the returned quotient may overflow.
  55. * @param numerator the numerator
  56. * @param denominator a divisor which must be != 0
  57. * @param remainder output parameter to receive the
  58. * remainder. Unlike <code>numerator % denominator</code>, this
  59. * will always be non-negative, in the half-open range <code>[0,
  60. * |denominator|)</code>.
  61. * @return the floor of the quotient
  62. */
  63. static int32_t floorDivide(double numerator, int32_t denominator,
  64. int32_t& remainder);
  65. /**
  66. * For a positive divisor, return the quotient and remainder
  67. * such that dividend = quotient*divisor + remainder and
  68. * 0 <= remainder < divisor.
  69. *
  70. * Works around edge-case bugs. Handles pathological input
  71. * (divident >> divisor) reasonably.
  72. *
  73. * Calling with a divisor <= 0 is disallowed.
  74. */
  75. static double floorDivide(double dividend, double divisor,
  76. double& remainder);
  77. };
  78. // Useful millisecond constants
  79. #define kOneDay (1.0 * U_MILLIS_PER_DAY) // 86,400,000
  80. #define kOneHour (60*60*1000)
  81. #define kOneMinute 60000
  82. #define kOneSecond 1000
  83. #define kOneMillisecond 1
  84. #define kOneWeek (7.0 * kOneDay) // 604,800,000
  85. // Epoch constants
  86. #define kJan1_1JulianDay 1721426 // January 1, year 1 (Gregorian)
  87. #define kEpochStartAsJulianDay 2440588 // January 1, 1970 (Gregorian)
  88. #define kEpochYear 1970
  89. #define kEarliestViableMillis -185331720384000000.0 // minimum representable by julian day -1e17
  90. #define kLatestViableMillis 185753453990400000.0 // max representable by julian day +1e17
  91. /**
  92. * The minimum supported Julian day. This value is equivalent to
  93. * MIN_MILLIS.
  94. */
  95. #define MIN_JULIAN (-0x7F000000)
  96. /**
  97. * The minimum supported epoch milliseconds. This value is equivalent
  98. * to MIN_JULIAN.
  99. */
  100. #define MIN_MILLIS ((MIN_JULIAN - kEpochStartAsJulianDay) * kOneDay)
  101. /**
  102. * The maximum supported Julian day. This value is equivalent to
  103. * MAX_MILLIS.
  104. */
  105. #define MAX_JULIAN (+0x7F000000)
  106. /**
  107. * The maximum supported epoch milliseconds. This value is equivalent
  108. * to MAX_JULIAN.
  109. */
  110. #define MAX_MILLIS ((MAX_JULIAN - kEpochStartAsJulianDay) * kOneDay)
  111. /**
  112. * A utility class providing proleptic Gregorian calendar functions
  113. * used by time zone and calendar code. Do not instantiate.
  114. *
  115. * Note: Unlike GregorianCalendar, all computations performed by this
  116. * class occur in the pure proleptic GregorianCalendar.
  117. */
  118. class Grego {
  119. public:
  120. /**
  121. * Return TRUE if the given year is a leap year.
  122. * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
  123. * @return TRUE if the year is a leap year
  124. */
  125. static inline UBool isLeapYear(int32_t year);
  126. /**
  127. * Return the number of days in the given month.
  128. * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
  129. * @param month 0-based month, with 0==Jan
  130. * @return the number of days in the given month
  131. */
  132. static inline int8_t monthLength(int32_t year, int32_t month);
  133. /**
  134. * Return the length of a previous month of the Gregorian calendar.
  135. * @param y the extended year
  136. * @param m the 0-based month number
  137. * @return the number of days in the month previous to the given month
  138. */
  139. static inline int8_t previousMonthLength(int y, int m);
  140. /**
  141. * Convert a year, month, and day-of-month, given in the proleptic
  142. * Gregorian calendar, to 1970 epoch days.
  143. * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
  144. * @param month 0-based month, with 0==Jan
  145. * @param dom 1-based day of month
  146. * @return the day number, with day 0 == Jan 1 1970
  147. */
  148. static double fieldsToDay(int32_t year, int32_t month, int32_t dom);
  149. /**
  150. * Convert a 1970-epoch day number to proleptic Gregorian year,
  151. * month, day-of-month, and day-of-week.
  152. * @param day 1970-epoch day (integral value)
  153. * @param year output parameter to receive year
  154. * @param month output parameter to receive month (0-based, 0==Jan)
  155. * @param dom output parameter to receive day-of-month (1-based)
  156. * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
  157. * @param doy output parameter to receive day-of-year (1-based)
  158. */
  159. static void dayToFields(double day, int32_t& year, int32_t& month,
  160. int32_t& dom, int32_t& dow, int32_t& doy);
  161. /**
  162. * Convert a 1970-epoch day number to proleptic Gregorian year,
  163. * month, day-of-month, and day-of-week.
  164. * @param day 1970-epoch day (integral value)
  165. * @param year output parameter to receive year
  166. * @param month output parameter to receive month (0-based, 0==Jan)
  167. * @param dom output parameter to receive day-of-month (1-based)
  168. * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
  169. */
  170. static inline void dayToFields(double day, int32_t& year, int32_t& month,
  171. int32_t& dom, int32_t& dow);
  172. /**
  173. * Convert a 1970-epoch milliseconds to proleptic Gregorian year,
  174. * month, day-of-month, and day-of-week, day of year and millis-in-day.
  175. * @param time 1970-epoch milliseconds
  176. * @param year output parameter to receive year
  177. * @param month output parameter to receive month (0-based, 0==Jan)
  178. * @param dom output parameter to receive day-of-month (1-based)
  179. * @param dow output parameter to receive day-of-week (1-based, 1==Sun)
  180. * @param doy output parameter to receive day-of-year (1-based)
  181. * @param mid output parameter to recieve millis-in-day
  182. */
  183. static void timeToFields(UDate time, int32_t& year, int32_t& month,
  184. int32_t& dom, int32_t& dow, int32_t& doy, int32_t& mid);
  185. /**
  186. * Return the day of week on the 1970-epoch day
  187. * @param day the 1970-epoch day (integral value)
  188. * @return the day of week
  189. */
  190. static int32_t dayOfWeek(double day);
  191. /**
  192. * Returns the ordinal number for the specified day of week within the month.
  193. * The valid return value is 1, 2, 3, 4 or -1.
  194. * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc.
  195. * @param month 0-based month, with 0==Jan
  196. * @param dom 1-based day of month
  197. * @return The ordinal number for the specified day of week within the month
  198. */
  199. static int32_t dayOfWeekInMonth(int32_t year, int32_t month, int32_t dom);
  200. /**
  201. * Converts Julian day to time as milliseconds.
  202. * @param julian the given Julian day number.
  203. * @return time as milliseconds.
  204. * @internal
  205. */
  206. static inline double julianDayToMillis(int32_t julian);
  207. /**
  208. * Converts time as milliseconds to Julian day.
  209. * @param millis the given milliseconds.
  210. * @return the Julian day number.
  211. * @internal
  212. */
  213. static inline int32_t millisToJulianDay(double millis);
  214. /**
  215. * Calculates the Gregorian day shift value for an extended year.
  216. * @param eyear Extended year
  217. * @returns number of days to ADD to Julian in order to convert from J->G
  218. */
  219. static inline int32_t gregorianShift(int32_t eyear);
  220. private:
  221. static const int16_t DAYS_BEFORE[24];
  222. static const int8_t MONTH_LENGTH[24];
  223. };
  224. inline double ClockMath::floorDivide(double numerator, double denominator) {
  225. return uprv_floor(numerator / denominator);
  226. }
  227. inline UBool Grego::isLeapYear(int32_t year) {
  228. // year&0x3 == year%4
  229. return ((year&0x3) == 0) && ((year%100 != 0) || (year%400 == 0));
  230. }
  231. inline int8_t
  232. Grego::monthLength(int32_t year, int32_t month) {
  233. return MONTH_LENGTH[month + (isLeapYear(year) ? 12 : 0)];
  234. }
  235. inline int8_t
  236. Grego::previousMonthLength(int y, int m) {
  237. return (m > 0) ? monthLength(y, m-1) : 31;
  238. }
  239. inline void Grego::dayToFields(double day, int32_t& year, int32_t& month,
  240. int32_t& dom, int32_t& dow) {
  241. int32_t doy_unused;
  242. dayToFields(day,year,month,dom,dow,doy_unused);
  243. }
  244. inline double Grego::julianDayToMillis(int32_t julian)
  245. {
  246. return (julian - kEpochStartAsJulianDay) * kOneDay;
  247. }
  248. inline int32_t Grego::millisToJulianDay(double millis) {
  249. return (int32_t) (kEpochStartAsJulianDay + ClockMath::floorDivide(millis, (double)kOneDay));
  250. }
  251. inline int32_t Grego::gregorianShift(int32_t eyear) {
  252. int32_t y = eyear-1;
  253. int32_t gregShift = ClockMath::floorDivide(y, 400) - ClockMath::floorDivide(y, 100) + 2;
  254. return gregShift;
  255. }
  256. U_NAMESPACE_END
  257. #endif // !UCONFIG_NO_FORMATTING
  258. #endif // GREGOIMP_H
  259. //eof