/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/Engine/Foundation/Platform/Posix/Timestamp_Posix.h
80 строк
3 KB
Ingrater
Clang-format fixes and update to 18.1.1 (#1234)
10 мар 2024, 15:51
Не верифицирован
10 мар 2024, 15:51
2f16acf
Код
Авторство
О чём код?
#include <Foundation/FoundationInternal.h> EZ_FOUNDATION_INTERNAL_HEADER #include <Foundation/Time/Timestamp.h> #include <sys/time.h> #include <time.h> const ezTimestamp ezTimestamp::CurrentTimestamp() { timeval currentTime; gettimeofday(¤tTime, nullptr); return ezTimestamp::MakeFromInt(currentTime.tv_sec * 1000000LL + currentTime.tv_usec, ezSIUnitOfTime::Microsecond); } bool operator!=(const tm& lhs, const tm& rhs) { if (lhs.tm_isdst == rhs.tm_isdst) { return !((lhs.tm_sec == rhs.tm_sec) && (lhs.tm_min == rhs.tm_min) && (lhs.tm_hour == rhs.tm_hour) && (lhs.tm_mday == rhs.tm_mday) && (lhs.tm_mon == rhs.tm_mon) && (lhs.tm_year == rhs.tm_year) && (lhs.tm_isdst == rhs.tm_isdst)); } else { /// \todo check whether the times are equal if one is in dst and the other not. /// mktime totally ignores your settings and overwrites them, there is no easy way /// to check whether the times are equal when dst is involved. /// mktime's dst *fix-up* will change hour, dst, day, month and year in the worst case. return false; } } const ezTimestamp ezDateTime::GetTimestamp() const { tm timeinfo = {0}; timeinfo.tm_sec = m_uiSecond; /* seconds after the minute - [0,59] */ timeinfo.tm_min = m_uiMinute; /* minutes after the hour - [0,59] */ timeinfo.tm_hour = m_uiHour; /* hours since midnight - [0,23] */ timeinfo.tm_mday = m_uiDay; /* day of the month - [1,31] */ timeinfo.tm_wday = m_uiDayOfWeek; /* day of the week - [0,6] */ timeinfo.tm_mon = m_uiMonth - 1; /* months since January - [0,11] */ timeinfo.tm_year = m_iYear - 1900; /* years since 1900 */ timeinfo.tm_isdst = 0; /* daylight savings time flag */ timeinfo.tm_zone = "GMT"; tm timeinfoCopy = timeinfo; time_t iTimeStamp = mktime(&timeinfo); // mktime may have 'patched' our time to be valid, we don't want that to count as a valid date. if (iTimeStamp == (time_t)-1 || timeinfoCopy != timeinfo) return ezTimestamp::MakeInvalid(); iTimeStamp += timeinfo.tm_gmtoff; // Subtract one hour if daylight saving time was activated by mktime. if (timeinfo.tm_isdst == 1) iTimeStamp -= 3600; return ezTimestamp::MakeFromInt(iTimeStamp, ezSIUnitOfTime::Second); } ezResult ezDateTime::SetFromTimestamp(ezTimestamp timestamp) { tm timeinfo = {0}; time_t iTime = (time_t)timestamp.GetInt64(ezSIUnitOfTime::Second); if (gmtime_r(&iTime, &timeinfo) == nullptr) return EZ_FAILURE; m_iYear = timeinfo.tm_year + 1900; m_uiMonth = timeinfo.tm_mon + 1; m_uiDay = timeinfo.tm_mday; m_uiDayOfWeek = timeinfo.tm_wday; m_uiHour = timeinfo.tm_hour; m_uiMinute = timeinfo.tm_min; m_uiSecond = timeinfo.tm_sec; m_uiMicroseconds = 0; return EZ_SUCCESS; }