1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01: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

@ -3,6 +3,9 @@
// ------------------------------------------------------------------------------------------------
#include "Core/Common.hpp"
// ------------------------------------------------------------------------------------------------
#include <ctime>
// ------------------------------------------------------------------------------------------------
namespace SqMod {

View File

@ -12,6 +12,19 @@ SQMOD_DECL_TYPENAME(Typename, _SC("SqDate"))
// ------------------------------------------------------------------------------------------------
SQChar Date::Delimiter = '-';
// ------------------------------------------------------------------------------------------------
Date::Date(int64_t ts)
: Date()
{
const auto y = static_cast< uint16_t >(std::lround(std::floor(ts / 3.17098e-14)));
ts -= int64_t{y} * 3.17098e-14;
const auto m = static_cast< uint8_t >(std::lround(std::floor(ts / 2.628e+12)));
ts -= int64_t{m} * 2.628e+12;
const auto d = static_cast< uint8_t >(std::lround(std::floor(ts / 8.64e+10)));
// Set the specified date
Set(y, m, d);
}
// ------------------------------------------------------------------------------------------------
int32_t Date::Compare(const Date & o) const
{
@ -363,6 +376,12 @@ Timestamp Date::GetTimestamp() const
return Timestamp(static_cast< int64_t >(days * 86400000000LL));
}
// ------------------------------------------------------------------------------------------------
std::time_t Date::ToTimeT() const
{
return GetTimestamp().ToTimeT();
}
// ================================================================================================
void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
{

View File

@ -83,6 +83,11 @@ public:
SetStr(str);
}
/* ------------------------------------------------------------------------------------------------
* Time-stamp constructor.
*/
explicit Date(int64_t ts);
/* ------------------------------------------------------------------------------------------------
* Copy constructor.
*/
@ -349,6 +354,11 @@ public:
* Convert this date instance to a time-stamp.
*/
SQMOD_NODISCARD Timestamp GetTimestamp() const;
/* --------------------------------------------------------------------------------------------
*
*/
std::time_t ToTimeT() const;
};
} // Namespace:: SqMod

View File

@ -16,6 +16,26 @@ SQChar Datetime::Delimiter = ' ';
SQChar Datetime::DateDelim = '-';
SQChar Datetime::TimeDelim = ':';
// ------------------------------------------------------------------------------------------------
Datetime::Datetime(int64_t ts)
: Datetime()
{
const auto y = static_cast< uint16_t >(std::lround(std::floor(ts / 3.17098e-14)));
ts -= int64_t{y} * 3.17098e-14;
const auto mo = static_cast< uint8_t >(std::lround(std::floor(ts / 2.628e+12)));
ts -= int64_t{mo} * 2.628e+12;
const auto d = static_cast< uint8_t >(std::lround(std::floor(ts / 8.64e+10)));
ts -= int64_t{d} * 8.64e+10;
const auto h = static_cast< uint8_t >(std::lround(std::floor(ts / 3.6e+9)));
ts -= int64_t{h} * 3.6e+9;
const auto mi = static_cast< uint8_t >(std::lround(std::floor(ts / 6e+7)));
ts -= int64_t{mi} * 6e+7;
const auto s = static_cast< uint8_t >(std::lround(std::floor(ts / 1e+6)));
ts -= int64_t{s} * 1e+6;
// Set the specified date-time
Set(y, mo, d, h, mi, s, static_cast< uint16_t >(ts / 1000LL));
}
// ------------------------------------------------------------------------------------------------
int32_t Datetime::Compare(const Datetime & o) const
{
@ -745,6 +765,12 @@ Timestamp Datetime::GetTimestamp() const
return Timestamp(ms);
}
// ------------------------------------------------------------------------------------------------
std::time_t Datetime::ToTimeT() const
{
return GetTimestamp().ToTimeT();
}
// ================================================================================================
void Register_ChronoDatetime(HSQUIRRELVM vm, Table & /*cns*/)
{

View File

@ -140,6 +140,11 @@ public:
Set(year, month, day, hour, minute, second, millisecond);
}
/* --------------------------------------------------------------------------------------------
* Time-stamp constructor.
*/
explicit Datetime(int64_t ts);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
@ -572,6 +577,11 @@ public:
* Convert this date-time instance to a time-stamp.
*/
SQMOD_NODISCARD Timestamp GetTimestamp() const;
/* --------------------------------------------------------------------------------------------
*
*/
std::time_t ToTimeT() const;
};
} // Namespace:: SqMod

View File

@ -12,6 +12,20 @@ SQMOD_DECL_TYPENAME(Typename, _SC("SqTime"))
// ------------------------------------------------------------------------------------------------
SQChar Time::Delimiter = ':';
// ------------------------------------------------------------------------------------------------
Time::Time(int64_t ts)
: Time()
{
const auto h = static_cast< uint8_t >(std::lround(std::floor(ts / 3.6e+9)));
ts -= int64_t{h} * 3.6e+9;
const auto m = static_cast< uint8_t >(std::lround(std::floor(ts / 6e+7)));
ts -= int64_t{m} * 6e+7;
const auto s = static_cast< uint8_t >(std::lround(std::floor(ts / 1e+6)));
ts -= int64_t{s} * 1e+6;
// Set the specified time
Set(h, m, s, static_cast< uint16_t >(ts / 1000LL));
}
// ------------------------------------------------------------------------------------------------
int32_t Time::Compare(const Time & o) const
{
@ -410,6 +424,12 @@ Timestamp Time::GetTimestamp() const
return Timestamp(ms);
}
// ------------------------------------------------------------------------------------------------
std::time_t Time::ToTimeT() const
{
return GetTimestamp().ToTimeT();
}
// ================================================================================================
void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
{

View File

@ -26,10 +26,10 @@ protected:
private:
// ------------------------------------------------------------------------------------------------
uint8_t m_Hour{}; // Hour
uint8_t m_Minute{}; // Minute
uint8_t m_Second{}; // Second
uint16_t m_Millisecond{}; // Millisecond
uint8_t m_Hour{0}; // Hour
uint8_t m_Minute{0}; // Minute
uint8_t m_Second{0}; // Second
uint16_t m_Millisecond{0}; // Millisecond
// ------------------------------------------------------------------------------------------------
SQChar m_Delimiter; // Component delimiter when generating strings.
@ -94,6 +94,11 @@ public:
SetStr(str);
}
/* ------------------------------------------------------------------------------------------------
* Time-stamp constructor.
*/
explicit Time(int64_t ts);
/* ------------------------------------------------------------------------------------------------
* Copy constructor.
*/
@ -354,6 +359,11 @@ public:
* Convert this time instance to a time-stamp.
*/
SQMOD_NODISCARD Timestamp GetTimestamp() const;
/* --------------------------------------------------------------------------------------------
*
*/
std::time_t ToTimeT() const;
};
} // Namespace:: SqMod

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)

View File

@ -7,7 +7,10 @@
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class Date;
class Time;
class Timer;
class Datetime;
/* ------------------------------------------------------------------------------------------------
*
@ -126,6 +129,12 @@ public:
*/
void SetMicroseconds(const SLongInt & amount);
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddMicroseconds(const SLongInt & amount);
Timestamp & SubMicroseconds(const SLongInt & amount);
/* --------------------------------------------------------------------------------------------
*
*/
@ -142,6 +151,12 @@ public:
m_Timestamp = amount;
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddMicrosecondsRaw(SQInteger amount) { m_Timestamp += amount; return *this; }
Timestamp & SubMicrosecondsRaw(SQInteger amount) { m_Timestamp -= amount; return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -152,6 +167,12 @@ public:
*/
void SetMilliseconds(const SLongInt & amount);
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddMilliseconds(const SLongInt & amount);
Timestamp & SubMilliseconds(const SLongInt & amount);
/* --------------------------------------------------------------------------------------------
*
*/
@ -168,6 +189,12 @@ public:
m_Timestamp = int64_t(int64_t(amount) * 1000L);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddMillisecondsRaw(SQInteger amount) { m_Timestamp += int64_t(int64_t(amount) * 1000L); return *this; }
Timestamp & SubMillisecondsRaw(SQInteger amount) { m_Timestamp -= int64_t(int64_t(amount) * 1000L); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -184,6 +211,12 @@ public:
m_Timestamp = int64_t(double(amount) * 1000000L);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddSecondsF(SQFloat amount) { m_Timestamp += int64_t(double(amount) * 1000000L); return *this; }
Timestamp & SubSecondsF(SQFloat amount) { m_Timestamp -= int64_t(double(amount) * 1000000L); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -200,6 +233,12 @@ public:
m_Timestamp = int64_t(int64_t(amount) * 1000000L);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddSecondsI(SQInteger amount) { m_Timestamp += int64_t(int64_t(amount) * 1000000L); return *this; }
Timestamp & SubSecondsI(SQInteger amount) { m_Timestamp -= int64_t(int64_t(amount) * 1000000L); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -216,6 +255,12 @@ public:
m_Timestamp = int64_t(double(amount) * 60000000L);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddMinutesF(SQFloat amount) { m_Timestamp += int64_t(double(amount) * 60000000L); return *this; }
Timestamp & SubMinutesF(SQFloat amount) { m_Timestamp -= int64_t(double(amount) * 60000000L); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -232,6 +277,12 @@ public:
m_Timestamp = int64_t(int64_t(amount) * 60000000L);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddMinutesI(SQInteger amount) { m_Timestamp += int64_t(int64_t(amount) * 60000000L); return *this; }
Timestamp & SubMinutesI(SQInteger amount) { m_Timestamp -= int64_t(int64_t(amount) * 60000000L); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -248,6 +299,12 @@ public:
m_Timestamp = int64_t(double(amount) * 3600000000LL);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddHoursF(SQFloat amount) { m_Timestamp += int64_t(double(amount) * 3600000000LL); return *this; }
Timestamp & SubHoursF(SQFloat amount) { m_Timestamp -= int64_t(double(amount) * 3600000000LL); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -264,6 +321,12 @@ public:
m_Timestamp = int64_t(double(amount) * 3600000000LL);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddHoursI(SQInteger amount) { m_Timestamp += int64_t(double(amount) * 3600000000LL); return *this; }
Timestamp & SubHoursI(SQInteger amount) { m_Timestamp -= int64_t(double(amount) * 3600000000LL); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -280,6 +343,12 @@ public:
m_Timestamp = int64_t(double(amount) * 86400000000LL);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddDaysF(SQFloat amount) { m_Timestamp += int64_t(double(amount) * 86400000000LL); return *this; }
Timestamp & SubDaysF(SQFloat amount) { m_Timestamp -= int64_t(double(amount) * 86400000000LL); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -296,6 +365,12 @@ public:
m_Timestamp = int64_t(double(amount) * 86400000000LL);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddDaysI(SQInteger amount) { m_Timestamp += int64_t(double(amount) * 86400000000LL); return *this; }
Timestamp & SubDaysI(SQInteger amount) { m_Timestamp -= int64_t(double(amount) * 86400000000LL); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -312,6 +387,12 @@ public:
m_Timestamp = int64_t(double(amount) * 31557600000000LL);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddYearsF(SQFloat amount) { m_Timestamp += int64_t(double(amount) * 31557600000000LL); return *this; }
Timestamp & SubYearsF(SQFloat amount) { m_Timestamp -= int64_t(double(amount) * 31557600000000LL); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
@ -328,6 +409,65 @@ public:
m_Timestamp = int64_t(double(amount) * 31557600000000LL);
}
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddYearsI(SQInteger amount) { m_Timestamp += int64_t(double(amount) * 31557600000000LL); return *this; }
Timestamp & SubYearsI(SQInteger amount) { m_Timestamp -= int64_t(double(amount) * 31557600000000LL); return *this; }
/* --------------------------------------------------------------------------------------------
*
*/
SQMOD_NODISCARD Time GetTime() const;
/* --------------------------------------------------------------------------------------------
*
*/
void SetTime(const Time & t);
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddTime(const Time & t);
Timestamp & SubTime(const Time & t);
/* --------------------------------------------------------------------------------------------
*
*/
SQMOD_NODISCARD Date GetDate() const;
/* --------------------------------------------------------------------------------------------
*
*/
void SetDate(const Date & d);
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddDate(const Date & d);
Timestamp & SubDate(const Date & d);
/* --------------------------------------------------------------------------------------------
*
*/
SQMOD_NODISCARD Datetime GetDatetime() const;
/* --------------------------------------------------------------------------------------------
*
*/
void SetDatetime(const Datetime & dt);
/* --------------------------------------------------------------------------------------------
*
*/
Timestamp & AddDatetime(const Datetime & dt);
Timestamp & SubDatetime(const Datetime & dt);
/* --------------------------------------------------------------------------------------------
*
*/
std::time_t ToTimeT() const;
private:
// --------------------------------------------------------------------------------------------