gregoimp.h 11 KB

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