y2k_time.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "precompile.h"
  2. #include "y2k_time.h"
  3. #ifndef _WIN32
  4. #include <time.h>
  5. #endif
  6. #define __reference_point 946656000LL // _mktime64 2000-01-01 00:00:00
  7. TOOLKIT_API y2k_time_t y2k_time_now()
  8. {
  9. #ifdef _WIN32
  10. __time64_t now;
  11. struct tm t;
  12. _time64(&now);
  13. _localtime64_s(&t, &now);
  14. return (y2k_time_t)(now - __reference_point);
  15. #else
  16. time_t now;
  17. struct tm t;
  18. time(&now);
  19. localtime_r(&now, &t);
  20. return (y2k_time_t)(now - __reference_point);
  21. #endif //_WIN32
  22. }
  23. TOOLKIT_API int y2k_time_is_today(y2k_time_t time_value)
  24. {
  25. #ifdef _WIN32
  26. __time64_t now;
  27. struct tm tm1;
  28. struct tm tm2;
  29. _time64(&now);
  30. memset(&tm1, 0, sizeof(tm1));
  31. _localtime64_s(&tm1, &now);
  32. #else
  33. time_t now;
  34. struct tm tm1;
  35. struct tm tm2;
  36. time(&now);
  37. memset(&tm1, 0, sizeof(tm1));
  38. localtime_r(&now, &tm1);
  39. #endif //_WIN32
  40. memset(&tm2, 0, sizeof(tm2));
  41. y2k_to_localtime_tm(time_value, &tm2);
  42. return tm1.tm_year == tm2.tm_year && tm1.tm_mon == tm2.tm_mon && tm1.tm_mday == tm2.tm_mday;
  43. }
  44. TOOLKIT_API __time64_t y2k_time_to_time64(y2k_time_t t)
  45. {
  46. return __reference_point + (unsigned int)t;
  47. }
  48. TOOLKIT_API int y2k_to_string(y2k_time_t t, char *buf, size_t n)
  49. {
  50. char buff[80];
  51. /*struct tm when;
  52. __time64_t tt = __reference_point + (unsigned int)t;
  53. _localtime64_s(&when, &tt);
  54. asctime_s(buff, sizeof(buff), &when);*/
  55. SYSTEMTIME st;
  56. y2k_to_localtime(t, &st);
  57. sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d",
  58. st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
  59. if (n <= strlen(buff))
  60. return -1;
  61. strcpy(buf, buff);
  62. return 0;
  63. }
  64. TOOLKIT_API y2k_time_t y2k_from_ymdhms(int year, int month, int day, int hour, int minute, int second)
  65. {
  66. struct tm tm;
  67. tm.tm_year = year - 1900;
  68. tm.tm_mon = month - 1;
  69. tm.tm_mday = day;
  70. tm.tm_hour = hour;
  71. tm.tm_min = second;
  72. tm.tm_sec = second;
  73. #ifdef _WIN32
  74. return (unsigned int)(_mktime64(&tm) - __reference_point);
  75. #else
  76. return (unsigned int)(mktime(&tm) - __reference_point);
  77. #endif
  78. }
  79. TOOLKIT_API int y2k_to_localtime_tm(y2k_time_t t, struct tm *tm)
  80. {
  81. __time64_t tt = __reference_point + (unsigned int)t;
  82. #ifdef _WIN32
  83. _localtime64_s(tm, &tt);
  84. #else
  85. localtime_r(&tt, tm);
  86. #endif
  87. return 0;
  88. }
  89. TOOLKIT_API int y2k_to_localtime(y2k_time_t t, LPSYSTEMTIME pt)
  90. {
  91. struct tm tm;
  92. y2k_to_localtime_tm(t, &tm);
  93. pt->wYear = 1900 + tm.tm_year;
  94. pt->wMonth = 1 + tm.tm_mon;
  95. pt->wDay = tm.tm_mday;
  96. pt->wHour = tm.tm_hour;
  97. pt->wMinute = tm.tm_min;
  98. pt->wSecond = tm.tm_sec;
  99. pt->wDayOfWeek = tm.tm_wday;
  100. pt->wMilliseconds = 0;
  101. return 0;
  102. }