1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-27 12:37:12 +02:00

More additions to chrono utilities.

This commit is contained in:
Sandu Liviu Catalin
2021-08-18 21:37:33 +03:00
parent 503b61c3df
commit 9d62233cfc
9 changed files with 410 additions and 4 deletions

View File

@ -1,9 +1,14 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Chrono/Timer.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Numeric/Long.hpp"
// ------------------------------------------------------------------------------------------------
#include <chrono>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -58,6 +63,10 @@ void Timestamp::SetMicroseconds(const SLongInt & amount)
m_Timestamp = amount.GetNum();
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::AddMicroseconds(const SLongInt & amount) { m_Timestamp += amount.GetNum(); return *this; }
Timestamp & Timestamp::SubMicroseconds(const SLongInt & amount) { m_Timestamp -= amount.GetNum(); return *this; }
// ------------------------------------------------------------------------------------------------
SLongInt Timestamp::GetMilliseconds() const
{
@ -70,6 +79,127 @@ void Timestamp::SetMilliseconds(const SLongInt & amount)
m_Timestamp = (amount.GetNum() * 1000L);
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::AddMilliseconds(const SLongInt & amount) { m_Timestamp += (amount.GetNum() * 1000L); return *this; }
Timestamp & Timestamp::SubMilliseconds(const SLongInt & amount) { m_Timestamp -= (amount.GetNum() * 1000L); return *this; }
// ------------------------------------------------------------------------------------------------
Time Timestamp::GetTime() const
{
return Time(m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
void Timestamp::SetTime(const Time & t)
{
SetHoursI(t.GetHour());
AddMinutesI(t.GetMinute());
AddSecondsI(t.GetSecond());
AddMillisecondsRaw(t.GetMillisecond());
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::AddTime(const Time & t)
{
AddHoursI(t.GetHour());
AddMinutesI(t.GetMinute());
AddSecondsI(t.GetSecond());
AddMillisecondsRaw(t.GetMillisecond());
return *this;
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::SubTime(const Time & t)
{
SubMillisecondsRaw(t.GetMillisecond());
SubSecondsI(t.GetSecond());
SubMinutesI(t.GetMinute());
SubHoursI(t.GetHour());
return *this;
}
// ------------------------------------------------------------------------------------------------
Date Timestamp::GetDate() const
{
return Date(m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
void Timestamp::SetDate(const Date & d)
{
SetYearsI(d.GetYear());
AddDaysF(d.GetMonth() * 30.4167);
AddDaysI(d.GetDay());
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::AddDate(const Date & d)
{
AddYearsI(d.GetYear());
AddDaysF(d.GetMonth() * 30.4167);
AddDaysI(d.GetDay());
return *this;
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::SubDate(const Date & d)
{
SubDaysI(d.GetDay());
SubDaysF(d.GetMonth() * 30.4167);
SubYearsI(d.GetYear());
return *this;
}
// ------------------------------------------------------------------------------------------------
Datetime Timestamp::GetDatetime() const
{
return Datetime(m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
void Timestamp::SetDatetime(const Datetime & dt)
{
SetYearsI(dt.GetYear());
AddDaysF(dt.GetMonth() * 30.4167);
AddDaysI(dt.GetDay());
AddHoursI(dt.GetHour());
AddMinutesI(dt.GetMinute());
AddSecondsI(dt.GetSecond());
AddMillisecondsRaw(dt.GetMillisecond());
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::AddDatetime(const Datetime & dt)
{
AddYearsI(dt.GetYear());
AddDaysF(dt.GetMonth() * 30.4167);
AddDaysI(dt.GetDay());
AddHoursI(dt.GetHour());
AddMinutesI(dt.GetMinute());
AddSecondsI(dt.GetSecond());
AddMillisecondsRaw(dt.GetMillisecond());
return *this;
}
// ------------------------------------------------------------------------------------------------
Timestamp & Timestamp::SubDatetime(const Datetime & dt)
{
SubMillisecondsRaw(dt.GetMillisecond());
SubSecondsI(dt.GetSecond());
SubMinutesI(dt.GetMinute());
SubHoursI(dt.GetHour());
SubDaysI(dt.GetDay());
SubDaysF(dt.GetMonth() * 30.4167);
SubYearsI(dt.GetYear());
return *this;
}
// ------------------------------------------------------------------------------------------------
std::time_t Timestamp::ToTimeT() const
{
return std::chrono::system_clock::to_time_t(std::chrono::time_point< std::chrono::system_clock >{std::chrono::microseconds{m_Timestamp}});
}
// ------------------------------------------------------------------------------------------------
static Timestamp SqGetEpochTimeNow()
{
@ -156,8 +286,46 @@ void Register_ChronoTimestamp(HSQUIRRELVM vm, Table & /*cns*/)
.Prop(_SC("DaysI"), &Timestamp::GetDaysI, &Timestamp::SetDaysI)
.Prop(_SC("YearsF"), &Timestamp::GetYearsF, &Timestamp::SetYearsF)
.Prop(_SC("YearsI"), &Timestamp::GetYearsI, &Timestamp::SetYearsI)
.Prop(_SC("Time"), &Timestamp::GetTime, &Timestamp::SetTime)
.Prop(_SC("Date"), &Timestamp::GetDate, &Timestamp::SetDate)
.Prop(_SC("Datetime"), &Timestamp::GetDatetime, &Timestamp::SetDatetime)
// Member Methods
.Func(_SC("SetNow"), &Timestamp::SetNow)
// Properties
.Func(_SC("AddMicroseconds"), &Timestamp::AddMicroseconds)
.Func(_SC("SubMicroseconds"), &Timestamp::SubMicroseconds)
.Func(_SC("AddMicrosecondsRaw"), &Timestamp::AddMicrosecondsRaw)
.Func(_SC("SubMicrosecondsRaw"), &Timestamp::SubMicrosecondsRaw)
.Func(_SC("AddMilliseconds"), &Timestamp::AddMilliseconds)
.Func(_SC("SubMilliseconds"), &Timestamp::SubMilliseconds)
.Func(_SC("AddMillisecondsRaw"), &Timestamp::AddMillisecondsRaw)
.Func(_SC("SubMillisecondsRaw"), &Timestamp::SubMillisecondsRaw)
.Func(_SC("AddSecondsF"), &Timestamp::AddSecondsF)
.Func(_SC("SubSecondsF"), &Timestamp::SubSecondsF)
.Func(_SC("AddSecondsI"), &Timestamp::AddSecondsI)
.Func(_SC("SubSecondsI"), &Timestamp::SubSecondsI)
.Func(_SC("AddMinutesF"), &Timestamp::AddMinutesF)
.Func(_SC("SubMinutesF"), &Timestamp::SubMinutesF)
.Func(_SC("AddMinutesI"), &Timestamp::AddMinutesI)
.Func(_SC("SubMinutesI"), &Timestamp::SubMinutesI)
.Func(_SC("AddHoursF"), &Timestamp::AddHoursF)
.Func(_SC("SubHoursF"), &Timestamp::SubHoursF)
.Func(_SC("AddHoursI"), &Timestamp::AddHoursI)
.Func(_SC("SubHoursI"), &Timestamp::SubHoursI)
.Func(_SC("AddDaysF"), &Timestamp::AddDaysF)
.Func(_SC("SubDaysF"), &Timestamp::SubDaysF)
.Func(_SC("AddDaysI"), &Timestamp::AddDaysI)
.Func(_SC("SubDaysI"), &Timestamp::SubDaysI)
.Func(_SC("AddYearsF"), &Timestamp::AddYearsF)
.Func(_SC("SubYearsF"), &Timestamp::SubYearsF)
.Func(_SC("AddYearsI"), &Timestamp::AddYearsI)
.Func(_SC("SubYearsI"), &Timestamp::SubYearsI)
.Func(_SC("AddTime"), &Timestamp::AddTime)
.Func(_SC("SubTime"), &Timestamp::SubTime)
.Func(_SC("AddDate"), &Timestamp::AddDate)
.Func(_SC("SubDate"), &Timestamp::SubDate)
.Func(_SC("AddDatetime"), &Timestamp::AddDatetime)
.Func(_SC("SubDatetime"), &Timestamp::SubDatetime)
// Static Functions
.StaticFunc(_SC("GetNow"), &SqGetEpochTimeNow)
.StaticFunc(_SC("GetMicrosRaw"), &SqGetMicrosecondsRaw)