systime.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. template <ui8 DaysInFeb>
  87. constexpr int DayOfYearToMonth(ui64& day) {
  88. Y_ASSERT(day >= 0);
  89. Y_ASSERT(day < 366);
  90. constexpr ui8 JanDays = 31;
  91. constexpr ui8 FebDays = JanDays + DaysInFeb;
  92. constexpr ui8 MarDays = FebDays + 31;
  93. constexpr ui8 AprDays = MarDays + 30;
  94. constexpr ui8 MayDays = AprDays + 31;
  95. constexpr ui8 JunDays = MayDays + 30;
  96. constexpr ui8 JulDays = JunDays + 31;
  97. constexpr ui16 AugDays = JulDays + 31;
  98. constexpr ui16 SepDays = AugDays + 30;
  99. constexpr ui16 OctDays = SepDays + 31;
  100. constexpr ui16 NovDays = OctDays + 30;
  101. // hard-coded binary search
  102. // this approach is faster that lookup in array using std::lower_bound()
  103. // GmTimeR takes ~40 cycles vs ~60 cycles using std::lower_bound version
  104. if (day < JunDays) {
  105. if (day < MarDays) {
  106. if (day < JanDays) {
  107. return 0;
  108. } else if (day < FebDays) {
  109. day -= JanDays;
  110. return 1;
  111. } else {
  112. day -= FebDays;
  113. return 2;
  114. }
  115. } else {
  116. if (day < AprDays) {
  117. day -= MarDays;
  118. return 3;
  119. } else if (day < MayDays) {
  120. day -= AprDays;
  121. return 4;
  122. } else {
  123. day -= MayDays;
  124. return 5;
  125. }
  126. }
  127. } else {
  128. if (day < SepDays) {
  129. if (day < JulDays) {
  130. day -= JunDays;
  131. return 6;
  132. } else if (day < AugDays) {
  133. day -= JulDays;
  134. return 7;
  135. } else {
  136. day -= AugDays;
  137. return 8;
  138. }
  139. } else {
  140. if (day < OctDays) {
  141. day -= SepDays;
  142. return 9;
  143. } else if (day < NovDays) {
  144. day -= OctDays;
  145. return 10;
  146. } else {
  147. day -= NovDays;
  148. return 11;
  149. }
  150. }
  151. }
  152. }
  153. class TDayNoToYearLookupTable {
  154. private:
  155. static constexpr int TableSize = 128;
  156. // lookup table for years in [1970, 1970 + 128 = 2098] range
  157. ui16 DaysSinceEpoch[TableSize] = {};
  158. public:
  159. constexpr TDayNoToYearLookupTable() {
  160. DaysSinceEpoch[0] = YearSize(UNIX_TIME_BASE_YEAR);
  161. for (int year = UNIX_TIME_BASE_YEAR + 1; year < UNIX_TIME_BASE_YEAR + TableSize; ++year) {
  162. DaysSinceEpoch[year - UNIX_TIME_BASE_YEAR] = DaysSinceEpoch[year - UNIX_TIME_BASE_YEAR - 1] + YearSize(year);
  163. }
  164. }
  165. // lookup year by days since epoch, decrement day counter to the corresponding amount of days.
  166. // The method returns the last year in the table, if year is too big
  167. int GetYear(ui64& days) const {
  168. size_t year = std::upper_bound(DaysSinceEpoch, Y_ARRAY_END(DaysSinceEpoch), days) - Y_ARRAY_BEGIN(DaysSinceEpoch);
  169. if (year > 0) {
  170. days -= DaysSinceEpoch[year - 1];
  171. }
  172. return year + UNIX_TIME_BASE_YEAR;
  173. }
  174. };
  175. constexpr TDayNoToYearLookupTable DAYS_TO_YEAR_LOOKUP;
  176. }
  177. //! Inverse of gmtime: converts struct tm to time_t, assuming the data
  178. //! in tm is UTC rather than local timezone. This implementation
  179. //! returns the number of seconds since 1970-01-01, converted to time_t.
  180. //! @note this code adopted from
  181. //! http://osdir.com/ml/web.wget.patches/2005-07/msg00010.html
  182. //! Subject: A more robust timegm - msg#00010
  183. time_t TimeGM(const struct tm* t) {
  184. // Only handles years after 1970
  185. if (Y_UNLIKELY(t->tm_year < 70)) {
  186. return (time_t)-1;
  187. }
  188. int days = 365 * (t->tm_year - 70);
  189. // Take into account the leap days between 1970 and YEAR-1
  190. days += (t->tm_year - 1 - 68) / 4 - ((t->tm_year - 1) / 100) + ((t->tm_year - 1 + 300) / 400);
  191. if (Y_UNLIKELY(t->tm_mon < 0 || t->tm_mon >= 12)) {
  192. return (time_t)-1;
  193. }
  194. if (IsLeapYear(1900 + t->tm_year)) {
  195. days += MONTH_TO_DAYS_LEAP[t->tm_mon];
  196. } else {
  197. days += MONTH_TO_DAYS[t->tm_mon];
  198. }
  199. days += t->tm_mday - 1;
  200. unsigned long secs = days * 86400ul + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec;
  201. return (time_t)secs;
  202. }
  203. struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf) {
  204. i64 time = static_cast<i64>(*timer);
  205. ui64 dayclock, dayno;
  206. int year = UNIX_TIME_BASE_YEAR;
  207. if (Y_UNLIKELY(time < 0)) {
  208. ui64 shift = (ui64)(-time - 1) / (FOUR_CENTURIES * SECONDS_PER_DAY) + 1;
  209. time += shift * (FOUR_CENTURIES * SECONDS_PER_DAY);
  210. year -= shift * 400;
  211. }
  212. dayclock = (ui64)time % SECONDS_PER_DAY;
  213. dayno = (ui64)time / SECONDS_PER_DAY;
  214. if (Y_UNLIKELY(dayno >= FOUR_CENTURIES)) {
  215. year += 400 * (dayno / FOUR_CENTURIES);
  216. dayno = dayno % FOUR_CENTURIES;
  217. }
  218. tmbuf->tm_sec = dayclock % 60;
  219. tmbuf->tm_min = (dayclock % 3600) / 60;
  220. tmbuf->tm_hour = dayclock / 3600;
  221. tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday
  222. if (Y_LIKELY(year == UNIX_TIME_BASE_YEAR)) {
  223. year = DAYS_TO_YEAR_LOOKUP.GetYear(dayno);
  224. }
  225. for (;;) {
  226. const ui16 yearSize = YearSize(year);
  227. if (dayno < yearSize) {
  228. break;
  229. }
  230. dayno -= yearSize;
  231. ++year;
  232. }
  233. tmbuf->tm_year = year - STRUCT_TM_BASE_YEAR;
  234. tmbuf->tm_yday = dayno;
  235. tmbuf->tm_mon = IsLeapYear(year)
  236. ? DayOfYearToMonth<29>(dayno)
  237. : DayOfYearToMonth<28>(dayno);
  238. tmbuf->tm_mday = dayno + 1;
  239. tmbuf->tm_isdst = 0;
  240. #ifndef _win_
  241. tmbuf->tm_gmtoff = 0;
  242. tmbuf->tm_zone = (char*)"UTC";
  243. #endif
  244. return tmbuf;
  245. }
  246. TString CTimeR(const time_t* timer) {
  247. char sTime[32];
  248. sTime[0] = 0;
  249. ctime_r(timer, &sTime[0]);
  250. return sTime;
  251. }