feat(Core/Common): add new helpers for time utility (#10207)

This commit is contained in:
Kargatum
2022-01-19 12:01:59 +07:00
committed by GitHub
parent b5ab409614
commit 259b9133f6
60 changed files with 732 additions and 341 deletions

View File

@@ -21,6 +21,57 @@
#include "Common.h"
#include "Duration.h"
enum class TimeFormat : uint8
{
FullText, // 1 Days 2 Hours 3 Minutes 4 Seconds 5 Milliseconds
ShortText, // 1d 2h 3m 4s 5ms
Numeric // 1:2:3:4:5
};
enum class TimeOutput : uint8
{
Days, // 1d
Hours, // 1d 2h
Minutes, // 1d 2h 3m
Seconds, // 1d 2h 3m 4s
Milliseconds, // 1d 2h 3m 4s 5ms
Microseconds // 1d 2h 3m 4s 5ms 6us
};
namespace Acore::Time
{
template <class T>
AC_COMMON_API uint32 TimeStringTo(std::string_view timeString);
template<class T>
AC_COMMON_API std::string ToTimeString(uint64 durationTime, TimeOutput timeOutput = TimeOutput::Seconds, TimeFormat timeFormat = TimeFormat::ShortText);
template<class T>
AC_COMMON_API std::string ToTimeString(std::string_view durationTime, TimeOutput timeOutput = TimeOutput::Seconds, TimeFormat timeFormat = TimeFormat::ShortText);
AC_COMMON_API std::string ToTimeString(Microseconds durationTime, TimeOutput timeOutput = TimeOutput::Seconds, TimeFormat timeFormat = TimeFormat::ShortText);
AC_COMMON_API time_t LocalTimeToUTCTime(time_t time);
AC_COMMON_API time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime = true);
AC_COMMON_API std::tm TimeBreakdown(time_t t = 0);
AC_COMMON_API std::string TimeToTimestampStr(Seconds time = 0s, std::string_view fmt = {});
AC_COMMON_API std::string TimeToHumanReadable(Seconds time = 0s, std::string_view fmt = {});
AC_COMMON_API time_t GetNextTimeWithDayAndHour(int8 dayOfWeek, int8 hour); // int8 dayOfWeek: 0 (sunday) to 6 (saturday)
AC_COMMON_API time_t GetNextTimeWithMonthAndHour(int8 month, int8 hour); // int8 month: 0 (january) to 11 (december)
AC_COMMON_API uint32 GetSeconds(Seconds time = 0s); // seconds after the minute - [0, 60]
AC_COMMON_API uint32 GetMinutes(Seconds time = 0s); // minutes after the hour - [0, 59]
AC_COMMON_API uint32 GetHours(Seconds time = 0s); // hours since midnight - [0, 23]
AC_COMMON_API uint32 GetDayInWeek(Seconds time = 0s); // days since Sunday - [0, 6]
AC_COMMON_API uint32 GetDayInMonth(Seconds time = 0s); // day of the month - [1, 31]
AC_COMMON_API uint32 GetDayInYear(Seconds time = 0s); // days since January 1 - [0, 365]
AC_COMMON_API uint32 GetMonth(Seconds time = 0s); // months since January - [0, 11]
AC_COMMON_API uint32 GetYear(Seconds time = 0s); // years since 1900
}
AC_COMMON_API struct tm* localtime_r(time_t const* time, struct tm* result);
inline TimePoint GetApplicationStartTime()
{
using namespace std::chrono;
@@ -30,6 +81,25 @@ inline TimePoint GetApplicationStartTime()
return ApplicationStartTime;
}
inline Milliseconds GetTimeMS()
{
using namespace std::chrono;
return duration_cast<milliseconds>(steady_clock::now() - GetApplicationStartTime());
}
inline Milliseconds GetMSTimeDiff(Milliseconds oldMSTime, Milliseconds newMSTime)
{
if (oldMSTime > newMSTime)
{
return oldMSTime - newMSTime;
}
else
{
return newMSTime - oldMSTime;
}
}
inline uint32 getMSTime()
{
using namespace std::chrono;
@@ -63,12 +133,21 @@ inline uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
return getMSTimeDiff(oldMSTime, getMSTime());
}
inline Milliseconds GetMSTimeDiffToNow(Milliseconds oldMSTime)
{
return GetMSTimeDiff(oldMSTime, GetTimeMS());
}
inline Seconds GetEpochTime()
{
using namespace std::chrono;
return duration_cast<Seconds>(system_clock::now().time_since_epoch());
}
struct IntervalTimer
{
public:
IntervalTimer()
= default;
IntervalTimer() = default;
void Update(time_t diff)
{