systime.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "systime.h"
  2. #include <util/system/yassert.h>
  3. #include <util/system/defaults.h>
  4. #ifdef _win_
  5. namespace {
  6. // Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
  7. constexpr ui64 NUMBER_OF_100_NANO_BETWEEN_1601_1970 =
  8. ULL(116444736000000000);
  9. constexpr ui64 NUMBER_OF_100_NANO_IN_SECOND = ULL(10000000);
  10. union TFTUnion {
  11. ui64 FTScalar;
  12. FILETIME FTStruct;
  13. };
  14. } // namespace
  15. void FileTimeToTimeval(const FILETIME* ft, timeval* tv) {
  16. Y_ASSERT(ft);
  17. Y_ASSERT(tv);
  18. TFTUnion ntTime;
  19. ntTime.FTStruct = *ft;
  20. ntTime.FTScalar -= NUMBER_OF_100_NANO_BETWEEN_1601_1970;
  21. tv->tv_sec =
  22. static_cast<long>(ntTime.FTScalar / NUMBER_OF_100_NANO_IN_SECOND);
  23. tv->tv_usec = static_cast<long>(
  24. (ntTime.FTScalar % NUMBER_OF_100_NANO_IN_SECOND) / LL(10));
  25. }
  26. void FileTimeToTimespec(const FILETIME& ft, struct timespec* ts) {
  27. Y_ASSERT(ts);
  28. TFTUnion ntTime;
  29. ntTime.FTStruct = ft;
  30. ntTime.FTScalar -= NUMBER_OF_100_NANO_BETWEEN_1601_1970;
  31. ts->tv_sec =
  32. static_cast<time_t>(ntTime.FTScalar / NUMBER_OF_100_NANO_IN_SECOND);
  33. ts->tv_nsec = static_cast<long>(
  34. (ntTime.FTScalar % NUMBER_OF_100_NANO_IN_SECOND) * LL(100));
  35. }
  36. int gettimeofday(timeval* tp, void*) {
  37. FILETIME ft;
  38. GetSystemTimeAsFileTime(&ft);
  39. FileTimeToTimeval(&ft, tp);
  40. return 0;
  41. }
  42. tm* localtime_r(const time_t* clock, tm* result) {
  43. tzset();
  44. tm* res = localtime(clock);
  45. if (res) {
  46. memcpy(result, res, sizeof(tm));
  47. return result;
  48. }
  49. return 0;
  50. }
  51. tm* gmtime_r(const time_t* clock, tm* result) {
  52. return gmtime_s(result, clock) == 0 ? result : 0;
  53. }
  54. char* ctime_r(const time_t* clock, char* buf) {
  55. char* res = ctime(clock);
  56. if (res) {
  57. memcpy(buf, res, 26);
  58. return buf;
  59. }
  60. return 0;
  61. }
  62. #endif /* _win_ */
  63. namespace {
  64. constexpr int STRUCT_TM_BASE_YEAR = 1900;
  65. constexpr int UNIX_TIME_BASE_YEAR = 1970;
  66. constexpr long SECONDS_PER_DAY = (24L * 60L * 60L);
  67. constexpr bool IsLeapYear(int year) {
  68. if (year % 4 != 0) {
  69. return false;
  70. }
  71. if (year % 100 != 0) {
  72. return true;
  73. }
  74. return year % 400 == 0;
  75. }
  76. constexpr ui16 DAYS_IN_YEAR = 365;
  77. constexpr ui16 DAYS_IN_LEAP_YEAR = 366;
  78. constexpr ui16 YearSize(int year) {
  79. return IsLeapYear(year) ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR;
  80. }
  81. constexpr ui64 FOUR_CENTURIES = (400 * 365 + 100 - 3);
  82. constexpr ui16 MONTH_TO_DAYS[12] = {
  83. 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  84. constexpr ui16 MONTH_TO_DAYS_LEAP[12] = {
  85. 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
  86. struct TMonth32LUT {
  87. char LastMonthDay32[12];
  88. char FirstMonthDay32[12];
  89. };
  90. constexpr TMonth32LUT MONTH_32_LUT[2] = {
  91. {
  92. // common year
  93. .LastMonthDay32 = {31, 27, 26, 24, 23, 21, 20, 19, 17, 16, 14, 13},
  94. .FirstMonthDay32 = {0, 1, 5, 6, 8, 9, 11, 12, 13, 15, 16, 18},
  95. },
  96. {
  97. // leap year
  98. .LastMonthDay32 = {31, 28, 27, 25, 24, 22, 21, 20, 18, 17, 15, 14},
  99. .FirstMonthDay32 = {0, 1, 4, 5, 7, 8, 10, 11, 12, 14, 15, 17},
  100. },
  101. };
  102. constexpr int DayOfYearToMonth(ui64& yearDay, bool leapYear) {
  103. Y_ASSERT(yearDay < 366);
  104. int approxMonth = yearDay / 32;
  105. int approxMDay = yearDay % 32;
  106. int dayThreshold = MONTH_32_LUT[leapYear].LastMonthDay32[approxMonth];
  107. int currentMonthMDayOffset = MONTH_32_LUT[leapYear].FirstMonthDay32[approxMonth];
  108. bool nextMonth = (approxMDay >= dayThreshold);
  109. int dayCorrection = nextMonth ? -dayThreshold : currentMonthMDayOffset;
  110. int day = approxMDay + dayCorrection;
  111. int month = approxMonth + nextMonth;
  112. yearDay = day;
  113. return month;
  114. }
  115. class TDayNoToYearLookupTable {
  116. static constexpr int TableSize = 128;
  117. // lookup table for years in [1970, 1970 + 128 = 2098] range
  118. ui16 DaysSinceEpoch[TableSize] = {};
  119. public:
  120. constexpr TDayNoToYearLookupTable() {
  121. ui16 daysAccumulated = 0;
  122. for (int year = UNIX_TIME_BASE_YEAR; year < UNIX_TIME_BASE_YEAR + TableSize; ++year) {
  123. daysAccumulated += YearSize(year);
  124. DaysSinceEpoch[year - UNIX_TIME_BASE_YEAR] = daysAccumulated;
  125. }
  126. }
  127. // lookup year by days since epoch, decrement day counter to the corresponding amount of days.
  128. // The method returns the last year in the table, if year is too big
  129. int FindYear(ui64& days) const {
  130. if (days >= DaysSinceEpoch[TableSize - 1]) {
  131. days -= DaysSinceEpoch[TableSize - 1];
  132. return TableSize + UNIX_TIME_BASE_YEAR;
  133. }
  134. const ui64 yearIndex = days / DAYS_IN_LEAP_YEAR;
  135. // we can miss by at most 1 year
  136. Y_ASSERT(yearIndex < TableSize);
  137. if (const auto diff = DaysSinceEpoch[yearIndex]; diff <= days) {
  138. days -= diff;
  139. return static_cast<int>(yearIndex + UNIX_TIME_BASE_YEAR + 1);
  140. }
  141. if (yearIndex > 0) {
  142. days -= DaysSinceEpoch[yearIndex - 1];
  143. }
  144. return static_cast<int>(yearIndex + UNIX_TIME_BASE_YEAR);
  145. }
  146. };
  147. constexpr TDayNoToYearLookupTable DAYS_TO_YEAR_LOOKUP;
  148. } // namespace
  149. //! Inverse of gmtime: converts struct tm to time_t, assuming the data
  150. //! in tm is UTC rather than local timezone. This implementation
  151. //! returns the number of seconds since 1970-01-01, converted to time_t.
  152. //! @note this code adopted from
  153. //! http://osdir.com/ml/web.wget.patches/2005-07/msg00010.html
  154. //! Subject: A more robust timegm - msg#00010
  155. time_t TimeGM(const struct tm* t) {
  156. // Only handles years after 1970
  157. if (Y_UNLIKELY(t->tm_year < 70)) {
  158. return (time_t)-1;
  159. }
  160. int days = 365 * (t->tm_year - 70);
  161. // Take into account the leap days between 1970 and YEAR-1
  162. days += (t->tm_year - 1 - 68) / 4 - ((t->tm_year - 1) / 100) + ((t->tm_year - 1 + 300) / 400);
  163. if (Y_UNLIKELY(t->tm_mon < 0 || t->tm_mon >= 12)) {
  164. return (time_t)-1;
  165. }
  166. if (IsLeapYear(1900 + t->tm_year)) {
  167. days += MONTH_TO_DAYS_LEAP[t->tm_mon];
  168. } else {
  169. days += MONTH_TO_DAYS[t->tm_mon];
  170. }
  171. days += t->tm_mday - 1;
  172. unsigned long secs = days * 86400ul + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec;
  173. return (time_t)secs;
  174. }
  175. struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf) {
  176. i64 time = static_cast<i64>(*timer);
  177. ui64 dayclock, dayno;
  178. int year = UNIX_TIME_BASE_YEAR;
  179. if (Y_UNLIKELY(time < 0)) {
  180. ui64 shift = (ui64)(-time - 1) / (FOUR_CENTURIES * SECONDS_PER_DAY) + 1;
  181. time += shift * (FOUR_CENTURIES * SECONDS_PER_DAY);
  182. year -= shift * 400;
  183. }
  184. dayclock = (ui64)time % SECONDS_PER_DAY;
  185. dayno = (ui64)time / SECONDS_PER_DAY;
  186. if (Y_UNLIKELY(dayno >= FOUR_CENTURIES)) {
  187. year += 400 * (dayno / FOUR_CENTURIES);
  188. dayno = dayno % FOUR_CENTURIES;
  189. }
  190. tmbuf->tm_sec = dayclock % 60;
  191. tmbuf->tm_min = (dayclock % 3600) / 60;
  192. tmbuf->tm_hour = dayclock / 3600;
  193. tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday
  194. if (Y_LIKELY(year == UNIX_TIME_BASE_YEAR)) {
  195. year = DAYS_TO_YEAR_LOOKUP.FindYear(dayno);
  196. }
  197. bool isLeapYear = IsLeapYear(year);
  198. for (;;) {
  199. const ui16 yearSize = isLeapYear ? DAYS_IN_LEAP_YEAR : DAYS_IN_YEAR;
  200. if (dayno < yearSize) {
  201. break;
  202. }
  203. dayno -= yearSize;
  204. ++year;
  205. isLeapYear = IsLeapYear(year);
  206. }
  207. tmbuf->tm_year = year - STRUCT_TM_BASE_YEAR;
  208. tmbuf->tm_yday = dayno;
  209. tmbuf->tm_mon = DayOfYearToMonth(dayno, isLeapYear);
  210. tmbuf->tm_mday = dayno + 1;
  211. tmbuf->tm_isdst = 0;
  212. #ifndef _win_
  213. tmbuf->tm_gmtoff = 0;
  214. tmbuf->tm_zone = (char*)"UTC";
  215. #endif
  216. return tmbuf;
  217. }
  218. TString CTimeR(const time_t* timer) {
  219. char sTime[32];
  220. sTime[0] = 0;
  221. ctime_r(timer, &sTime[0]);
  222. return sTime;
  223. }