123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include "precompile.h"
- #include "y2k_time.h"
- #ifndef _WIN32
- #include <time.h>
- #endif
- #define __reference_point 946656000LL // _mktime64 2000-01-01 00:00:00
- TOOLKIT_API y2k_time_t y2k_time_now()
- {
- #ifdef _WIN32
- __time64_t now;
- struct tm t;
- _time64(&now);
- _localtime64_s(&t, &now);
- return (y2k_time_t)(now - __reference_point);
- #else
- time_t now;
- struct tm t;
- time(&now);
- localtime_r(&now, &t);
- return (y2k_time_t)(now - __reference_point);
- #endif //_WIN32
- }
- TOOLKIT_API int y2k_time_is_today(y2k_time_t time_value)
- {
- #ifdef _WIN32
- __time64_t now;
- struct tm tm1;
- struct tm tm2;
- _time64(&now);
- memset(&tm1, 0, sizeof(tm1));
- _localtime64_s(&tm1, &now);
- #else
- time_t now;
- struct tm tm1;
- struct tm tm2;
- time(&now);
- memset(&tm1, 0, sizeof(tm1));
- localtime_r(&now, &tm1);
- #endif //_WIN32
- memset(&tm2, 0, sizeof(tm2));
- y2k_to_localtime_tm(time_value, &tm2);
- return tm1.tm_year == tm2.tm_year && tm1.tm_mon == tm2.tm_mon && tm1.tm_mday == tm2.tm_mday;
- }
- TOOLKIT_API __time64_t y2k_time_to_time64(y2k_time_t t)
- {
- return __reference_point + (unsigned int)t;
- }
- TOOLKIT_API int y2k_to_string(y2k_time_t t, char *buf, size_t n)
- {
- char buff[80];
- /*struct tm when;
- __time64_t tt = __reference_point + (unsigned int)t;
- _localtime64_s(&when, &tt);
- asctime_s(buff, sizeof(buff), &when);*/
- SYSTEMTIME st;
- y2k_to_localtime(t, &st);
- sprintf(buff, "%04d-%02d-%02d %02d:%02d:%02d",
- st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
- if (n <= strlen(buff))
- return -1;
- strcpy(buf, buff);
- return 0;
- }
- TOOLKIT_API y2k_time_t y2k_from_ymdhms(int year, int month, int day, int hour, int minute, int second)
- {
- struct tm tm;
- tm.tm_year = year - 1900;
- tm.tm_mon = month - 1;
- tm.tm_mday = day;
- tm.tm_hour = hour;
- tm.tm_min = second;
- tm.tm_sec = second;
- #ifdef _WIN32
- return (unsigned int)(_mktime64(&tm) - __reference_point);
- #else
- return (unsigned int)(mktime(&tm) - __reference_point);
- #endif
- }
- TOOLKIT_API int y2k_to_localtime_tm(y2k_time_t t, struct tm *tm)
- {
- __time64_t tt = __reference_point + (unsigned int)t;
- #ifdef _WIN32
- _localtime64_s(tm, &tt);
- #else
- localtime_r(&tt, tm);
- #endif
- return 0;
- }
- TOOLKIT_API int y2k_to_localtime(y2k_time_t t, LPSYSTEMTIME pt)
- {
- struct tm tm;
- y2k_to_localtime_tm(t, &tm);
- pt->wYear = 1900 + tm.tm_year;
- pt->wMonth = 1 + tm.tm_mon;
- pt->wDay = tm.tm_mday;
- pt->wHour = tm.tm_hour;
- pt->wMinute = tm.tm_min;
- pt->wSecond = tm.tm_sec;
- pt->wDayOfWeek = tm.tm_wday;
- pt->wMilliseconds = 0;
- return 0;
- }
|