Plasma Engine  2.0
Loading...
Searching...
No Matches
Timestamp_Posix.h
1#include <Foundation/FoundationInternal.h>
2PL_FOUNDATION_INTERNAL_HEADER
3
4#include <Foundation/Time/Timestamp.h>
5
6#include <sys/time.h>
7#include <time.h>
8
10{
11 timeval currentTime;
12 gettimeofday(&currentTime, nullptr);
13
14 return plTimestamp::MakeFromInt(currentTime.tv_sec * 1000000LL + currentTime.tv_usec, plSIUnitOfTime::Microsecond);
15}
16
17bool operator!=(const tm& lhs, const tm& rhs)
18{
19 if (lhs.tm_isdst == rhs.tm_isdst)
20 {
21 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) &&
22 (lhs.tm_mon == rhs.tm_mon) && (lhs.tm_year == rhs.tm_year) && (lhs.tm_isdst == rhs.tm_isdst));
23 }
24 else
25 {
30 return false;
31 }
32}
33
34
36{
37 tm timeinfo = {0};
38
39 timeinfo.tm_sec = m_uiSecond; /* seconds after the minute - [0,59] */
40 timeinfo.tm_min = m_uiMinute; /* minutes after the hour - [0,59] */
41 timeinfo.tm_hour = m_uiHour; /* hours since midnight - [0,23] */
42 timeinfo.tm_mday = m_uiDay; /* day of the month - [1,31] */
43 timeinfo.tm_wday = m_uiDayOfWeek; /* day of the week - [0,6] */
44 timeinfo.tm_mon = m_uiMonth - 1; /* months since January - [0,11] */
45 timeinfo.tm_year = m_iYear - 1900; /* years since 1900 */
46 timeinfo.tm_isdst = 0; /* daylight savings time flag */
47 timeinfo.tm_zone = "GMT";
48
49 tm timeinfoCopy = timeinfo;
50
51 time_t iTimeStamp = mktime(&timeinfo);
52 // mktime may have 'patched' our time to be valid, we don't want that to count as a valid date.
53 if (iTimeStamp == (time_t)-1 || timeinfoCopy != timeinfo)
55
56 iTimeStamp += timeinfo.tm_gmtoff;
57 // Subtract one hour if daylight saving time was activated by mktime.
58 if (timeinfo.tm_isdst == 1)
59 iTimeStamp -= 3600;
61}
62
64{
65 tm timeinfo = {0};
66 time_t iTime = (time_t)timestamp.GetInt64(plSIUnitOfTime::Second);
67 if (gmtime_r(&iTime, &timeinfo) == nullptr)
68 return PL_FAILURE;
69
70 m_iYear = timeinfo.tm_year + 1900;
71 m_uiMonth = timeinfo.tm_mon + 1;
72 m_uiDay = timeinfo.tm_mday;
73 m_uiDayOfWeek = timeinfo.tm_wday;
74 m_uiHour = timeinfo.tm_hour;
75 m_uiMinute = timeinfo.tm_min;
76 m_uiSecond = timeinfo.tm_sec;
77 m_uiMicroseconds = 0;
78
79 return PL_SUCCESS;
80}
plResult SetFromTimestamp(plTimestamp timestamp)
Sets this instance to the given timestamp.
Definition Timestamp_Posix.h:63
const plTimestamp GetTimestamp() const
Converts this instance' values into a plTimestamp.
Definition Timestamp_Posix.h:35
The timestamp class encapsulates a date in time as microseconds since Unix epoch.
Definition Timestamp.h:23
plInt64 GetInt64(plSIUnitOfTime::Enum unitOfTime) const
Returns the number of 'unitOfTime' since Unix epoch.
Definition Timestamp.cpp:17
static plTimestamp MakeFromInt(plInt64 iTimeValue, plSIUnitOfTime::Enum unitOfTime)
Returns a timestamp initialized from 'iTimeValue' in 'unitOfTime' since Unix epoch.
Definition Timestamp.cpp:36
static plTimestamp MakeInvalid()
Returns an invalid timestamp.
Definition Timestamp.h:47
static const plTimestamp CurrentTimestamp()
Returns the current timestamp. Returned value will always be valid.
Definition Timestamp_Posix.h:9
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
@ Second
SI-unit of time (base unit)
Definition Timestamp.h:13
@ Microsecond
SI-unit of time (10^-6 second)
Definition Timestamp.h:11