80 lines
3.4 KiB
C++
80 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include "dtcore/inc/dtdefinitions.h"
|
|
#include "dtcore/inc/dtstream.h"
|
|
#include "dtcore/inc/dtstring.h"
|
|
|
|
struct _FILETIME;
|
|
|
|
namespace dt
|
|
{
|
|
enum class DateTimeFormat
|
|
{
|
|
Iso8601Extended, // YYYY-MM-DDThh:mm:ss.sss+00:00
|
|
Iso8601Basic, // YYYYMMDDThhmmSS.sssZ
|
|
Iso8601DateOnly // YYYY-MM-DD
|
|
};
|
|
|
|
// This class works with UTC time. Provided values will be converted to UTC time, based on the
|
|
// given UTC offset. If no offset is provided, local offset is considered.
|
|
class DateTime : public Serializable
|
|
{
|
|
public:
|
|
DT_CORE_DLL_ENTRY DateTime() = default;
|
|
DT_CORE_DLL_ENTRY explicit DateTime(MilliSeconds unixTimeMs);
|
|
DT_CORE_DLL_ENTRY explicit DateTime(const String& value);
|
|
#if defined(DT_PLATFORM_X64) || defined(DT_PLATFORM_DURANGO) || defined(DT_PLATFORM_DURANGO_GDK) || defined(DT_PLATFORM_SCARLETT)
|
|
DT_CORE_DLL_ENTRY explicit DateTime(_FILETIME* fileTime);
|
|
#endif
|
|
|
|
DT_CORE_DLL_ENTRY void Add(MilliSeconds value);
|
|
DT_CORE_DLL_ENTRY DateTime GetAdded(MilliSeconds value) const;
|
|
|
|
DT_CORE_DLL_ENTRY DateTime GetDayStart() const;
|
|
|
|
DT_CORE_DLL_ENTRY String ToString(DateTimeFormat format = DateTimeFormat::Iso8601Extended) const;
|
|
DT_CORE_DLL_ENTRY String& ToString(String& inplaceStr, DateTimeFormat format = DateTimeFormat::Iso8601Extended) const;
|
|
|
|
// Milliseconds since Epoch
|
|
DT_CORE_DLL_ENTRY MilliSeconds GetUnixTimeMs() const { return m_UnixTimeMs; }
|
|
|
|
// milliseconds after the second [0,999]
|
|
DT_CORE_DLL_ENTRY int GetMsec() const;
|
|
// seconds after the minute [0, 59]
|
|
DT_CORE_DLL_ENTRY int GetSec() const;
|
|
// minutes after the hour [0, 59]
|
|
DT_CORE_DLL_ENTRY int GetMin() const;
|
|
// hours since midnight [0, 23]
|
|
DT_CORE_DLL_ENTRY int GetHour() const;
|
|
// day of the month [1, 31]
|
|
DT_CORE_DLL_ENTRY int GetMday() const;
|
|
// month since January [1, 12]
|
|
DT_CORE_DLL_ENTRY int GetMon() const;
|
|
// years in AD [1, ...]
|
|
DT_CORE_DLL_ENTRY int GetYear() const;
|
|
// days since Sunday [0, 6]
|
|
DT_CORE_DLL_ENTRY int GetWday() const;
|
|
|
|
DT_CORE_DLL_ENTRY bool IsValid() const { return m_UnixTimeMs != MilliSeconds(INVALID_VALUE); }
|
|
|
|
DT_CORE_DLL_ENTRY bool operator>(const DateTime& moment) const { return m_UnixTimeMs > moment.m_UnixTimeMs; }
|
|
DT_CORE_DLL_ENTRY bool operator>=(const DateTime& moment) const { return m_UnixTimeMs >= moment.m_UnixTimeMs; }
|
|
DT_CORE_DLL_ENTRY bool operator<(const DateTime& moment) const { return m_UnixTimeMs < moment.m_UnixTimeMs; }
|
|
DT_CORE_DLL_ENTRY bool operator<=(const DateTime& moment) const { return m_UnixTimeMs <= moment.m_UnixTimeMs; }
|
|
DT_CORE_DLL_ENTRY bool operator==(const DateTime& moment) const { return m_UnixTimeMs == moment.m_UnixTimeMs; }
|
|
DT_CORE_DLL_ENTRY bool operator!=(const DateTime& moment) const { return m_UnixTimeMs != moment.m_UnixTimeMs; }
|
|
|
|
DT_CORE_DLL_ENTRY static DateTime Now();
|
|
|
|
DT_CORE_DLL_ENTRY virtual U8 GetSerializeVersion() const override { return 2; }
|
|
DT_CORE_DLL_ENTRY virtual void Serialize(U8 version, Stream& stream) override;
|
|
|
|
void Clear() { m_UnixTimeMs = MilliSeconds(INVALID_VALUE); }
|
|
|
|
private:
|
|
MilliSeconds m_UnixTimeMs = MilliSeconds(INVALID_VALUE);
|
|
};
|
|
|
|
DT_CORE_DLL_ENTRY String& operator<<(String& out, const DateTime& dateTime);
|
|
|
|
}
|