systime.cpp 7.9 KB

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