morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::time::DateTime Struct Reference

A UTC instant with millisecond precision. More...

#include <datetime.hpp>

Public Member Functions

constexpr DateTime () noexcept=default
 Constructs the Unix epoch (1970-01-01T00:00:00.000Z).
constexpr DateTime (std::chrono::sys_time< std::chrono::milliseconds > instant) noexcept
 Wraps an existing UTC instant.
constexpr DateTime (std::chrono::year year, std::chrono::month month, std::chrono::day day, std::chrono::hours hour, std::chrono::minutes minute, std::chrono::seconds second, std::chrono::milliseconds millisecond=std::chrono::milliseconds{0}) noexcept
 Composes a UTC instant from calendar/clock components.
std::string toIso8601 () const
 Renders as ISO-8601 UTC: YYYY-MM-DDTHH:MM:SS.mmmZ.
constexpr std::strong_ordering operator<=> (const DateTime &other) const noexcept=default
 Ordering on the underlying instant.
template<typename Rep, typename Period>
constexpr DateTime & operator+= (std::chrono::duration< Rep, Period > delta) noexcept
 Shifts this instant by an arbitrary chrono duration.
template<typename Rep, typename Period>
constexpr DateTime & operator-= (std::chrono::duration< Rep, Period > delta) noexcept
 Shifts this instant backwards by an arbitrary chrono duration.

Static Public Member Functions

static DateTime now () noexcept
 The current UTC instant, truncated to milliseconds.
static std::optional< DateTime > fromIso8601 (std::string_view text) noexcept
 Strictly parses ISO-8601 UTC: YYYY-MM-DDTHH:MM:SS, optionally followed by .m/.mm/.mmm and an optional trailing Z.

Public Attributes

std::chrono::sys_time< std::chrono::milliseconds > value
 The underlying UTC instant (Unix time, leap seconds ignored).

Detailed Description

A UTC instant with millisecond precision.

The default state is the Unix epoch — DateTime itself has no empty state; use Timestamp for fields that may be blank.

Definition at line 71 of file datetime.hpp.

Constructor & Destructor Documentation

◆ DateTime() [1/2]

morph::time::DateTime::DateTime ( std::chrono::sys_time< std::chrono::milliseconds > instant)
inlineconstexprnoexcept

Wraps an existing UTC instant.

Parameters
instantThe UTC instant to wrap.

Definition at line 80 of file datetime.hpp.

◆ DateTime() [2/2]

morph::time::DateTime::DateTime ( std::chrono::year year,
std::chrono::month month,
std::chrono::day day,
std::chrono::hours hour,
std::chrono::minutes minute,
std::chrono::seconds second,
std::chrono::milliseconds millisecond = std::chrono::milliseconds{0} )
inlineconstexprnoexcept

Composes a UTC instant from calendar/clock components.

Parameters
yearCalendar year.
monthMonth-of-year in [1, 12].
dayDay-of-month in [1, 31].
hourHours in [0, 23].
minuteMinutes in [0, 59].
secondSeconds in [0, 59].
millisecondMilliseconds in [0, 999].

Definition at line 90 of file datetime.hpp.

Member Function Documentation

◆ now()

DateTime morph::time::DateTime::now ( )
inlinestaticnodiscardnoexcept

The current UTC instant, truncated to milliseconds.

Consults the process-wide clock override installed via setNowOverride/ ScopedNowOverride, if any; otherwise calls std::chrono::system_clock::now() directly. This is the seam that lets registry-constructed (remotely instantiated) models get a deterministic "now" in tests without any constructor parameter: production code calls DateTime::now() exactly as before, and a test installs an override for its scope. See ScopedNowOverride.

Warning
This function is noexcept. If an installed override callable throws, that exception escapes a noexcept function and std::terminate() is called — an override must not throw. The override also runs while the internal mutex is held, so it must not itself call DateTime::now() / Timestamp::now(), setNowOverride, or construct a ScopedNowOverride: the mutex is non-recursive and any of those would self-deadlock (the same constraint morph::log's sink callback documents for the same reason).
Returns
The current UTC date-time, or the overridden instant if one is installed.

Definition at line 114 of file datetime.hpp.

◆ toIso8601()

std::string morph::time::DateTime::toIso8601 ( ) const
inlinenodiscard

Renders as ISO-8601 UTC: YYYY-MM-DDTHH:MM:SS.mmmZ.

Returns
The formatted instant.

Definition at line 127 of file datetime.hpp.

◆ fromIso8601()

std::optional< DateTime > morph::time::DateTime::fromIso8601 ( std::string_view text)
inlinestaticnodiscardnoexcept

Strictly parses ISO-8601 UTC: YYYY-MM-DDTHH:MM:SS, optionally followed by .m/.mm/.mmm and an optional trailing Z.

The calendar date must exist (2026-02-30 is rejected) and clock components must be in range; no time zones other than Z, no lowercase separators, no trailing input. The fixed-width date and clock fields (month, day, hour, minute, second, fraction) reject a leading sign; only the year field may carry a - (negative years).

Parameters
textCandidate ISO-8601 string.
Returns
The parsed instant, or std::nullopt when text is malformed.

Definition at line 148 of file datetime.hpp.

◆ operator<=>()

std::strong_ordering morph::time::DateTime::operator<=> ( const DateTime & other) const
nodiscardconstexprdefaultnoexcept

Ordering on the underlying instant.

Parameters
otherInstant to compare against.
Returns
The relative ordering of the two instants.

◆ operator+=()

template<typename Rep, typename Period>
DateTime & morph::time::DateTime::operator+= ( std::chrono::duration< Rep, Period > delta)
inlineconstexprnoexcept

Shifts this instant by an arbitrary chrono duration.

Parameters
deltaDuration to add (may be negative).
Returns
*this.

Definition at line 238 of file datetime.hpp.

◆ operator-=()

template<typename Rep, typename Period>
DateTime & morph::time::DateTime::operator-= ( std::chrono::duration< Rep, Period > delta)
inlineconstexprnoexcept

Shifts this instant backwards by an arbitrary chrono duration.

Parameters
deltaDuration to subtract (may be negative).
Returns
*this.

Definition at line 247 of file datetime.hpp.

Member Data Documentation

◆ value

std::chrono::sys_time<std::chrono::milliseconds> morph::time::DateTime::value

The underlying UTC instant (Unix time, leap seconds ignored).

Definition at line 73 of file datetime.hpp.


The documentation for this struct was generated from the following file: