ddn.datetime

ddn.datetime — standalone nanosecond temporal values.

Two value types, both timezone-free by construction:

  • TimeOfDay — nanoseconds since midnight

    (0 .. 86_400_000_000_000), no date, no timezone.

  • Timestamp — a signed count of nanoseconds since the

    Unix epoch; an instant. It does not carry a display timezone.

Conversions to and from Phobos temporal types are checked by default: inexact conversions throw rather than silently lose precision, with explicitly named truncating variants for callers who want the loss. There is no implicit local DateTime-to-instant conversion — that requires an explicit timezone and a daylight-saving ambiguity policy.

The module imports Phobos only (never ddn.var or format modules) and every operation is CTFE-safe.

Design record: doc/DDN-DATETIME.md.

Example:

import ddn.datetime;

auto t = TimeOfDay(14, 30, 15, 250_000_000);
assert(t.toString() == "14:30:15.25");
auto phobos = std.datetime.date.TimeOfDay(9, 5, 0);
assert(TimeOfDay(phobos).toString() == "09:05:00");

Types 5

structTimeOfDay

Time of day with nanosecond precision and no date or timezone.

The value is a validated count of nanoseconds since midnight in 0 .. 86_400_000_000_000. Days are strictly 86 400 seconds: leap seconds are an instant-domain concept and are not representable here (see the module documentation and the design record).

Fields
ulong nanosPerDayExclusive upper bound of the nanoseconds-of-day range.
Methods
long totalNanos() @property const @safe pure nothrow @nogcNanoseconds since midnight (`0 .. nanosPerDay`).
int hour() @property const @safe pure nothrow @nogcHour of day (`0..23`).
int minute() @property const @safe pure nothrow @nogcMinute within the hour (`0..59`).
int second() @property const @safe pure nothrow @nogcSecond within the minute (`0..59`).
int nanosecond() @property const @safe pure nothrow @nogcSub-second nanoseconds (`0..999_999_999`).
std.datetime.date.TimeOfDay toPhobos() const @safe pureConverts to a Phobos whole-second `TimeOfDay` (checked).
std.datetime.date.TimeOfDay toPhobosTruncated() const @safe pureConverts to a Phobos whole-second `TimeOfDay`, truncating the fraction toward zero (explicit precision loss).
string toString() const @safe pureFormats as ISO-8601 time text: `HH:MM:SS` for whole seconds, `HH:MM:SS.fffffffff` (trailing zeros trimmed) otherwise — byte-identical to Phobos rendering for whole seconds.
bool opEquals(const TimeOfDay rhs) const @safe pure nothrow @nogcValue equality over the nanoseconds-of-day count.
int opCmp(const TimeOfDay rhs) const @safe pure nothrow @nogcTotal order over the nanoseconds-of-day count.
size_t toHash() const @safe pure nothrow @nogcHash consistent with equality.
Constructors
this(int hour, int minute, int second, int nanosecond = 0)Constructs from parts; each component is range-checked.
this(long nanosOfDay)Constructs from a raw nanoseconds-of-day count.
this(std.datetime.date.TimeOfDay phobos)Constructs from a Phobos whole-second `TimeOfDay` (exact; the fraction becomes zero).
classDateTimeException : Exception

Thrown for rejected temporal operations: out-of-range values, invalid components and inexact conversions where exactness is required.

Constructors
this(string msg)Params: msg = description of the rejected operation.
structTimestamp

An instant: a signed count of nanoseconds since the Unix epoch (1970-01-01T00:00:00 UTC). A Timestamp carries no display timezone — toSysTime() renders in UTC by default or an explicitly supplied timezone.

The representable range is 1677-09-21T00:12:43.145224193Z through 2262-04-11T23:47:16.854775807Z (the signed 64-bit nanosecond span). Conversions from SysTime use tick arithmetic on the stdTime scalar (never a whole-second intermediate) and preserve the instant exactly; leap-second scalars flow through unchanged (rendering them is SysTime's business on the way back out).

Methods
long totalNanos() @property const @safe pure nothrow @nogcNanoseconds since the Unix epoch (signed).
SysTime toSysTime(immutable TimeZone tz = null) const @safeConverts to a `SysTime` (checked): exact for values aligned to 100 ns; inexact values throw.
SysTime toSysTimeTruncated(immutable TimeZone tz = null) const @safeConverts to a `SysTime`, truncating the sub-100-nanosecond part toward zero (explicit precision loss).
string toString() const @safeFormats as ISO-8601 UTC text: `yyyy-MM-ddTHH:MM:SSZ`, with `.fffffffff` (trailing zeros trimmed) inserted when the sub-second nanoseconds are nonzero.
bool opEquals(const Timestamp rhs) const @safe pure nothrow @nogcValue equality over the epoch-nanosecond count.
int opCmp(const Timestamp rhs) const @safe pure nothrow @nogcTotal order over the epoch-nanosecond count.
size_t toHash() const @safe pure nothrow @nogcHash consistent with equality.
Constructors
this(long epochNanos)Constructs from a raw nanosecond count (no clamping and no validation: the full signed range is a valid instant).
this(SysTime sysTime)Constructs from a `SysTime`, preserving the instant exactly via tick arithmetic (`stdTime` offset applied, then scaled by 100 — never a whole-second intermediate). The source timezone affects onl...

Alias for Timestamp following the established temporal-model vocabulary (java.time.Instant, JS Temporal.Instant, Noda Time): a Timestamp is an instant — a single point on the physical time line, independent of any timezone rendering. The alias adds no behavior; it exists for discoverability.

enumTemporalUnit : ubyte

The unit of an integer temporal count at an interchange boundary.

DDN stores every temporal value as a signed 64-bit nanosecond count (Timestamp since the epoch, TimeOfDay since midnight, a duration as a plain long); the unit is metadata that exists only where such a count enters or leaves - a file format, a wire protocol, a sensor reading. It is deliberately not a property of any stored value and not a resolution a value can be "in": the conversions below are exact integer arithmetic, or they refuse.

secondsSeconds (x 1_000_000_000 to nanoseconds).
millisecondsMilliseconds (x 1_000_000).
microsecondsMicroseconds (x 1_000).
nanosecondsNanoseconds (x 1; no scaling).

Functions 16

fnNullable!Timestamp parseTimestamp(const(char)[] text) @safe pure nothrowParses a timestamp from `YYYY-MM-DD[T ]HH:MM:SS[.f{1,9}][Z|±HH:MM]`.
fnNullable!TimeOfDay parseTimeOfDay(const(char)[] text) @safe pureParses a time of day from `HH:MM:SS[.f{1,9}]`, every field range-checked.
fnchar[] formatTimestampInto(long epochNanos, return char[] buf) @safe pure nothrowFormats a timestamp into `buf` exactly like `Timestamp.toString`: `YYYY-MM-DDTHH:MM:SS[.f{1,9}]Z`, the fraction omitted when zero and trailing zeros trimmed otherwise. Allocation-free; calendar ari...
fnchar[] formatTimeOfDayInto(long nanosOfDay, return char[] buf) @safe pure nothrowFormats a time of day into `buf` exactly like `TimeOfDay.toString`: `HH:MM:SS[.f{1,9}]`, the fraction omitted when zero and trailing zeros trimmed otherwise. Allocation-free.
fnstring formatTimestamp(long epochNanos) @safe pureConvenience: `formatTimestampInto` into a fresh string.
fnstring formatTimeOfDay(long nanosOfDay) @safe pureConvenience: `formatTimeOfDayInto` into a fresh string.
fnlong temporalUnitFactor(TemporalUnit unit) @safe pure nothrow @nogcThe exact positive factor from `unit` to nanoseconds (CTFE-safe).
fnlong nanosFromUnit(long count, TemporalUnit unit) @safe pureScales an integer `count` in `unit` to nanoseconds, exactly.
fnlong nanosToUnit(long nanos, TemporalUnit unit) @safe pureScales a nanosecond count to `unit`, exactly (checked).
fnlong nanosToUnitTruncated(long nanos, TemporalUnit unit) @safe pure nothrow @nogcScales a nanosecond count to `unit`, truncating a sub-unit remainder toward zero (explicit precision loss).
fnTimestamp timestampFromUnit(long count, TemporalUnit unit) @safe pureBuilds a `Timestamp` from an integer epoch count in `unit`, exactly.
fnlong timestampToUnit(Timestamp t, TemporalUnit unit) @safe pureExpresses the instant as an integer count in `unit`, exactly (checked: a remainder refuses - see nanosToUnit).
fnlong timestampToUnitTruncated(Timestamp t, TemporalUnit unit) @safe pure nothrow @nogcExpresses the instant as an integer count in `unit`, truncating a sub-unit remainder toward zero (explicit precision loss).
fnTimeOfDay timeOfDayFromUnit(long count, TemporalUnit unit) @safe pureBuilds a `TimeOfDay` from an integer count since midnight in `unit`, exactly. The result is range-checked like every `TimeOfDay` (`0 .. 86_400_000_000_000` nanoseconds).
fnlong timeOfDayToUnit(TimeOfDay t, TemporalUnit unit) @safe pureExpresses the time of day as an integer count since midnight in `unit`, exactly (checked: a remainder refuses - see nanosToUnit).
fnlong timeOfDayToUnitTruncated(TimeOfDay t, TemporalUnit unit) @safe pure nothrow @nogcExpresses the time of day as an integer count since midnight in `unit`, truncating a sub-unit remainder toward zero (explicit precision loss).