systime.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #define YEAR0 1900
  43. #define EPOCH_YR 1970
  44. #define SECS_DAY (24L * 60L * 60L)
  45. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
  46. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  47. #define FOURCENTURIES (400 * 365 + 100 - 3)
  48. //! Inverse of gmtime: converts struct tm to time_t, assuming the data
  49. //! in tm is UTC rather than local timezone. This implementation
  50. //! returns the number of seconds since 1970-01-01, converted to time_t.
  51. //! @note this code adopted from
  52. //! http://osdir.com/ml/web.wget.patches/2005-07/msg00010.html
  53. //! Subject: A more robust timegm - msg#00010
  54. time_t TimeGM(const struct tm* t) {
  55. static const unsigned short int month_to_days[][13] = {
  56. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
  57. {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}};
  58. // Only handles years after 1970
  59. if (t->tm_year < 70) {
  60. return (time_t)-1;
  61. }
  62. int days = 365 * (t->tm_year - 70);
  63. // Take into account the leap days between 1970 and YEAR-1
  64. days += (t->tm_year - 1 - 68) / 4 - ((t->tm_year - 1) / 100) + ((t->tm_year - 1 + 300) / 400);
  65. if (t->tm_mon < 0 || t->tm_mon >= 12) {
  66. return (time_t)-1;
  67. }
  68. days += month_to_days[LEAPYEAR(1900 + t->tm_year)][t->tm_mon];
  69. days += t->tm_mday - 1;
  70. unsigned long secs = days * 86400ul + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec;
  71. return (time_t)secs;
  72. }
  73. struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf) {
  74. static const int _ytab[2][12] = {
  75. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  76. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
  77. i64 time = static_cast<i64>(*timer);
  78. ui64 dayclock, dayno;
  79. int year = EPOCH_YR;
  80. if (time < 0) {
  81. ui64 shift = (ui64)(-time - 1) / ((ui64)FOURCENTURIES * SECS_DAY) + 1;
  82. time += shift * ((ui64)FOURCENTURIES * SECS_DAY);
  83. year -= shift * 400;
  84. }
  85. dayclock = (ui64)time % SECS_DAY;
  86. dayno = (ui64)time / SECS_DAY;
  87. year += 400 * (dayno / FOURCENTURIES);
  88. dayno = dayno % FOURCENTURIES;
  89. tmbuf->tm_sec = dayclock % 60;
  90. tmbuf->tm_min = (dayclock % 3600) / 60;
  91. tmbuf->tm_hour = dayclock / 3600;
  92. tmbuf->tm_wday = (dayno + 4) % 7; // Day 0 was a thursday
  93. while (dayno >= (ui64)YEARSIZE(year)) {
  94. dayno -= YEARSIZE(year);
  95. ++year;
  96. }
  97. tmbuf->tm_year = year - YEAR0;
  98. tmbuf->tm_yday = dayno;
  99. tmbuf->tm_mon = 0;
  100. while (dayno >= (ui64)_ytab[LEAPYEAR(year)][tmbuf->tm_mon]) {
  101. dayno -= _ytab[LEAPYEAR(year)][tmbuf->tm_mon];
  102. ++tmbuf->tm_mon;
  103. }
  104. tmbuf->tm_mday = dayno + 1;
  105. tmbuf->tm_isdst = 0;
  106. #ifndef _win_
  107. tmbuf->tm_gmtoff = 0;
  108. tmbuf->tm_zone = (char*)"UTC";
  109. #endif
  110. return tmbuf;
  111. }
  112. TString CTimeR(const time_t* timer) {
  113. char sTime[32];
  114. sTime[0] = 0;
  115. ctime_r(timer, &sTime[0]);
  116. return sTime;
  117. }