flatten 20260225
This commit is contained in:
17
tjp/core/time/Source.cpp
Normal file
17
tjp/core/time/Source.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#include "Source.hpp"
|
||||
#include "Time+chrono.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
Time Source::get()
|
||||
{
|
||||
return fromTimePoint(std::chrono::system_clock::now());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
12
tjp/core/time/Source.h
Normal file
12
tjp/core/time/Source.h
Normal file
@@ -0,0 +1,12 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
struct Source;
|
||||
using SourceStandard = Source;
|
||||
|
||||
} // namespace
|
||||
17
tjp/core/time/Source.hpp
Normal file
17
tjp/core/time/Source.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Source.h"
|
||||
#include "Time.hpp"
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
struct Source
|
||||
{
|
||||
Time get();
|
||||
} ;
|
||||
|
||||
} // namespace
|
||||
30
tjp/core/time/Time+IO.cpp
Normal file
30
tjp/core/time/Time+IO.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#include "Time.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
std::ostream &operator <<(std::ostream &o, const Time &v)
|
||||
{
|
||||
return o << v.v;
|
||||
}
|
||||
|
||||
std::ostream &operator <<(std::ostream &o, const Duration &v)
|
||||
{
|
||||
return o<< v.v;
|
||||
}
|
||||
|
||||
std::istream &operator >>(std::istream &i, Time &v)
|
||||
{
|
||||
return i >> v.v;
|
||||
}
|
||||
|
||||
std::istream &operator >>(std::istream &i, Duration &v)
|
||||
{
|
||||
return i >> v.v;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
32
tjp/core/time/Time+IO.hpp
Normal file
32
tjp/core/time/Time+IO.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Time.hpp"
|
||||
#include <iosfwd>
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
template<typename IO>
|
||||
void io_(IO &io, Time &v)
|
||||
{
|
||||
io.any(v.v);
|
||||
}
|
||||
|
||||
template<typename IO>
|
||||
void io_(IO &io, Duration &v)
|
||||
{
|
||||
io.any(v.v);
|
||||
}
|
||||
|
||||
std::ostream &operator <<(std::ostream &, const Time &v);
|
||||
std::ostream &operator <<(std::ostream &, const Duration &v);
|
||||
|
||||
std::istream &operator >>(std::istream &, Time &v);
|
||||
std::istream &operator >>(std::istream &, Duration &v);
|
||||
|
||||
} // namespace
|
||||
|
||||
#include "Time+operations.hpp"
|
||||
31
tjp/core/time/Time+chrono+misc.hpp
Normal file
31
tjp/core/time/Time+chrono+misc.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Time+chrono.hpp"
|
||||
|
||||
#include <ctime>
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
inline
|
||||
Time time_today_at(time::Duration offset)
|
||||
{
|
||||
auto now = std::chrono::system_clock::now();
|
||||
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
|
||||
|
||||
std::tm timeinfo;
|
||||
localtime_r(&now_c, &timeinfo);
|
||||
|
||||
timeinfo.tm_hour = int(offset / time::Hours);
|
||||
timeinfo.tm_min = int(offset / time::Minutes) % 60;
|
||||
timeinfo.tm_sec = int(offset / time::Seconds) % 60;
|
||||
|
||||
auto time_point = std::chrono::system_clock::from_time_t(std::mktime(&timeinfo));
|
||||
|
||||
return fromTimePoint(time_point);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
37
tjp/core/time/Time+chrono.hpp
Normal file
37
tjp/core/time/Time+chrono.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Time.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
template<typename Clock>
|
||||
Time fromTimePoint(const std::chrono::time_point<Clock> &t)
|
||||
{
|
||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(t.time_since_epoch());
|
||||
return { V(millis.count()) };
|
||||
}
|
||||
|
||||
template<typename Clock = std::chrono::system_clock>
|
||||
auto toTimePoint(Time time)
|
||||
{
|
||||
// 2100/1/1
|
||||
if (time == FarFuture)
|
||||
return std::chrono::time_point<Clock>(std::chrono::seconds(4102444800));
|
||||
|
||||
// get a timepoint for the milliseconds past epoch
|
||||
return std::chrono::time_point<Clock>(std::chrono::milliseconds(time.v));
|
||||
}
|
||||
|
||||
template<typename Clock = std::chrono::system_clock>
|
||||
Time now()
|
||||
{
|
||||
return fromTimePoint(std::chrono::system_clock::now());
|
||||
}
|
||||
|
||||
}
|
||||
216
tjp/core/time/Time+operations.hpp
Normal file
216
tjp/core/time/Time+operations.hpp
Normal file
@@ -0,0 +1,216 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Time.hpp"
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Amount operator /(Duration d, Converter c)
|
||||
{
|
||||
return d.v / (Amount)c.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Amount operator /(Duration lhs, Duration rhs) {
|
||||
return (Amount)lhs.v / (Amount)rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Rate operator /(Amount lhs, Duration rhs) {
|
||||
return { (Amount)lhs / (Amount)rhs.v };
|
||||
}
|
||||
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration operator *(Amount a, Converter c)
|
||||
{
|
||||
return { V(a * c.v) };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Amount operator *(Duration a, Rate c)
|
||||
{
|
||||
return a.v * c.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Rate operator *(Amount a, Rate c)
|
||||
{
|
||||
return { a * c.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration operator *(Amount a, Duration c)
|
||||
{
|
||||
return { V(a * c.v) };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Time operator +(Time t, Duration d) {
|
||||
return { t.v + d.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Time operator -(Time t, Duration d) {
|
||||
return { t.v - d.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Time &operator +=(Time &t, Duration d) {
|
||||
t.v += d.v;
|
||||
return t;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Time &operator -=(Time &t, Duration d) {
|
||||
t.v -= d.v;
|
||||
return t;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration operator -(Time lhs, Time rhs) {
|
||||
return { lhs.v - rhs.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration operator %(Time lhs, Duration rhs) {
|
||||
return { lhs.v % rhs.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration operator %(Duration lhs, Duration rhs) {
|
||||
return { lhs.v % rhs.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator <(Time lhs, Time rhs) {
|
||||
return lhs.v < rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator <=(Time lhs, Time rhs) {
|
||||
return lhs.v <= rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator >(Time lhs, Time rhs) {
|
||||
return lhs.v > rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator >=(Time lhs, Time rhs) {
|
||||
return lhs.v >= rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator ==(Time lhs, Time rhs) {
|
||||
return lhs.v == rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator !=(Time lhs, Time rhs) {
|
||||
return lhs.v != rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator ==(Duration lhs, Duration rhs) {
|
||||
return lhs.v == rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator !=(Duration lhs, Duration rhs) {
|
||||
return lhs.v != rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator <(Duration lhs, Duration rhs) {
|
||||
return lhs.v < rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator <=(Duration lhs, Duration rhs) {
|
||||
return lhs.v <= rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator >(Duration lhs, Duration rhs) {
|
||||
return lhs.v > rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
bool operator >=(Duration lhs, Duration rhs) {
|
||||
return lhs.v >= rhs.v;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration operator -(Duration lhs, Duration rhs) {
|
||||
return { lhs.v - rhs.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration operator +(Duration lhs, Duration rhs) {
|
||||
return { lhs.v + rhs.v };
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration &operator -=(Duration &lhs, Duration rhs) {
|
||||
lhs.v -= rhs.v;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
inline
|
||||
constexpr
|
||||
Duration &operator +=(Duration &lhs, Duration rhs) {
|
||||
lhs.v += rhs.v;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
constexpr
|
||||
T cast(const Duration &d, const Converter &accuracy, const Converter &to)
|
||||
{
|
||||
return (d.v / accuracy.v) / (T(to.v) / accuracy.v);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr
|
||||
T cast(const Time &d, const Converter &accuracy, const Converter &to)
|
||||
{
|
||||
return (d.v / accuracy.v) / (T(to.v) / accuracy.v);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
59
tjp/core/time/Time+timestamp.hpp
Normal file
59
tjp/core/time/Time+timestamp.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Time.hpp"
|
||||
#include "Time+chrono.hpp"
|
||||
#include "../string/String.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
template <typename Clock>
|
||||
String toTimeStamp(const std::chrono::time_point<Clock>& tp)
|
||||
{
|
||||
auto time = Clock::to_time_t(tp);
|
||||
std::tm tm = *std::localtime(&time);
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()) % 1000;
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
|
||||
oss << '.' << std::setfill('0') << std::setw(3) << ms.count();
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
inline
|
||||
String toTimeStamp(Time time)
|
||||
{
|
||||
return toTimeStamp(toTimePoint(time));
|
||||
}
|
||||
|
||||
|
||||
template<typename Clock>
|
||||
std::chrono::time_point<Clock> fromTimeStamp(const String& timestamp)
|
||||
{
|
||||
std::tm tm = {};
|
||||
int milliseconds;
|
||||
std::istringstream iss(timestamp);
|
||||
iss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
|
||||
char dot;
|
||||
iss >> dot >> milliseconds;
|
||||
|
||||
auto time = std::mktime(&tm);
|
||||
auto tp = Clock::from_time_t(time) + std::chrono::milliseconds(milliseconds);
|
||||
return tp;
|
||||
}
|
||||
|
||||
inline
|
||||
Time fromTimeStamp(const String& timestamp)
|
||||
{
|
||||
return fromTimePoint(fromTimeStamp<std::chrono::system_clock>(timestamp));
|
||||
}
|
||||
|
||||
}
|
||||
12
tjp/core/time/Time.cpp
Normal file
12
tjp/core/time/Time.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#include "Time.hpp"
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
const Duration Duration::Zero { 0 };
|
||||
const Time Time::Zero { 0 };
|
||||
|
||||
} // namespace
|
||||
17
tjp/core/time/Time.h
Normal file
17
tjp/core/time/Time.h
Normal file
@@ -0,0 +1,17 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../types/Types.h"
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
struct Duration;
|
||||
struct Time;
|
||||
struct Converter;
|
||||
using Amount = r64;
|
||||
|
||||
} // namespace
|
||||
|
||||
59
tjp/core/time/Time.hpp
Normal file
59
tjp/core/time/Time.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Time.h"
|
||||
#include <limits>
|
||||
|
||||
namespace tjp::core::time {
|
||||
|
||||
using V = s64;
|
||||
|
||||
struct Rate {
|
||||
Amount v;
|
||||
|
||||
static const Amount Zero;
|
||||
} ;
|
||||
|
||||
struct Duration {
|
||||
V v;
|
||||
|
||||
static const Duration Zero;
|
||||
} ;
|
||||
|
||||
constexpr Duration Forever { V(60) * 60 * 24 * 365 * 100 };
|
||||
constexpr Duration Now { 0 };
|
||||
|
||||
struct Time {
|
||||
V v;
|
||||
|
||||
static const Time Zero;
|
||||
} ;
|
||||
|
||||
|
||||
struct Converter {
|
||||
V v;
|
||||
|
||||
constexpr Duration operator()(Amount a) const {
|
||||
return { V(a * v) };
|
||||
}
|
||||
} ;
|
||||
|
||||
constexpr Converter Milliseconds { 1 };
|
||||
constexpr Converter Centiseconds { 10 };
|
||||
constexpr Converter Deciseconds { 100 };
|
||||
constexpr Converter Seconds { 1000 };
|
||||
constexpr Converter Minutes { 60 * 1000 };
|
||||
constexpr Converter Hours { 60 * 60 * 1000 };
|
||||
constexpr Converter Days { 24 * 60 * 60 * 1000 };
|
||||
constexpr Converter Years { V(365) * 24 * 60 * 60 * 1000 };
|
||||
|
||||
constexpr Time FarFuture { std::numeric_limits<V>::max() };
|
||||
|
||||
constexpr Time Epoch { 0 };
|
||||
|
||||
}
|
||||
|
||||
#include "Time+operations.hpp"
|
||||
Reference in New Issue
Block a user