Some checks failed
Bigfoot / Build & Test Debug (Unity Build: OFF) (push) Successful in 2m17s
Bigfoot / Build & Test Debug (Unity Build: ON) (push) Successful in 26s
Bigfoot / Build & Test RelWithDebInfo (Unity Build: OFF) (push) Successful in 25s
Bigfoot / Build & Test RelWithDebInfo (Unity Build: ON) (push) Successful in 26s
Bigfoot / Build & Test Release (Unity Build: OFF) (push) Successful in 27s
Bigfoot / Build & Test Release (Unity Build: ON) (push) Successful in 26s
Bigfoot / Clang Format Checks (push) Failing after 11s
139 lines
3.8 KiB
C++
139 lines
3.8 KiB
C++
/*********************************************************************
|
|
* \file Time.cpp
|
|
*
|
|
* \author Romain BOULLARD
|
|
* \date December 2025
|
|
*********************************************************************/
|
|
#include <System/Time/Time.hpp>
|
|
|
|
#include <System/SystemAssertHandler.hpp>
|
|
|
|
namespace
|
|
{
|
|
[[nodiscard]]
|
|
std::tm GetTimeInfo(const std::time_t& p_time)
|
|
{
|
|
std::tm timeInfo {};
|
|
#ifdef BIGFOOT_WINDOWS
|
|
[[maybe_unused]]
|
|
const errno_t error = gmtime_s(&timeInfo, &p_time);
|
|
if (error != 0)
|
|
{
|
|
char errnoStr[256];
|
|
strerror_s(errnoStr, error);
|
|
|
|
CRITICAL_ASSERT(Bigfoot::SystemAssertHandler,
|
|
false,
|
|
"Failed to convert epoch time to calendar time. {}",
|
|
errnoStr);
|
|
}
|
|
#elif BIGFOOT_LINUX
|
|
[[maybe_unused]]
|
|
const std::tm* error = gmtime_r(&p_time, &timeInfo);
|
|
CRITICAL_ASSERT(Bigfoot::SystemAssertHandler, error, "Failed to convert epoch time to calendar time");
|
|
#else
|
|
static_assert(false, "not implemented");
|
|
#endif
|
|
|
|
return timeInfo;
|
|
}
|
|
} // namespace
|
|
|
|
namespace Bigfoot
|
|
{
|
|
Time::Time(const std::uint64_t p_epoch):
|
|
m_epoch(p_epoch),
|
|
m_timeInfo(GetTimeInfo(std::chrono::system_clock::to_time_t(
|
|
std::chrono::time_point<std::chrono::system_clock> {std::chrono::microseconds {m_epoch}}))),
|
|
m_microseconds(std::chrono::duration_cast<std::chrono::microseconds>(
|
|
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::time_point<std::chrono::system_clock> {
|
|
std::chrono::microseconds {m_epoch}}.time_since_epoch()) %
|
|
std::chrono::seconds(1)))
|
|
{
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint32_t Time::Year() const
|
|
{
|
|
return m_timeInfo.tm_year + 1900;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint32_t Time::Month() const
|
|
{
|
|
return m_timeInfo.tm_mon + 1;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint32_t Time::Day() const
|
|
{
|
|
return m_timeInfo.tm_mday;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint32_t Time::Hour() const
|
|
{
|
|
return m_timeInfo.tm_hour;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint32_t Time::Minute() const
|
|
{
|
|
return m_timeInfo.tm_min;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint32_t Time::Second() const
|
|
{
|
|
return m_timeInfo.tm_sec;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint32_t Time::Microsecond() const
|
|
{
|
|
return static_cast<std::uint32_t>(m_microseconds.count());
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
std::uint64_t Time::Epoch() const
|
|
{
|
|
return m_epoch;
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
Time Time::Now()
|
|
{
|
|
const std::chrono::time_point<std::chrono::system_clock> time = std::chrono::system_clock::now();
|
|
const std::uint64_t epochInMicroSeconds =
|
|
std::chrono::duration_cast<std::chrono::microseconds>(time.time_since_epoch()).count();
|
|
|
|
return Time {epochInMicroSeconds};
|
|
}
|
|
} // namespace Bigfoot
|
|
|
|
/****************************************************************************************/
|
|
|
|
namespace flatbuffers
|
|
{
|
|
Bigfoot::Flat::Time Pack(const Bigfoot::Time& p_time)
|
|
{
|
|
return Bigfoot::Flat::Time {p_time.Epoch()};
|
|
}
|
|
|
|
/****************************************************************************************/
|
|
|
|
Bigfoot::Time UnPack(const Bigfoot::Flat::Time& p_flatTime)
|
|
{
|
|
return Bigfoot::Time {p_flatTime.epoch()};
|
|
}
|
|
} // namespace flatbuffers
|