clock.dhover×58all
/**
The clock capability (SPEC §10.3): the `isClock` concept and `TestClock`,
the canonical handler-swap witness — any function generic over its clock
runs unmodified under virtual time. The live ring-backed clock joins the
loop-side `live` module in M9.

$(B Effects-side module): no ring imports; fibers park through the
`isWaker` seam.
*/
module 
(package) sparkles
sparkles
.
(package) sparkles.event_horizon
event_horizon
.
(module) sparkles.event_horizon.clock

The clock capability (SPEC §10.3): the isClock concept and TestClock, the canonical handler-swap witness — any function generic over its clock runs unmodified under virtual time. The live ring-backed clock joins the loop-side live module in M9.

Effects-side module: no ring imports; fibers park through the isWaker seam.

clock
;
import
(package) core
core
.
(module) core.time

Module containing core time functionality, such as Duration (which represents a duration of time) or MonoTime (which represents a timestamp of the system's monotonic clock).

Various functions take a string (or strings) to represent a unit of time (e.g. convert!("days", "hours")(numDays)). The valid strings to use with such functions are "years", "months", "weeks", "days", "hours", "minutes", "seconds", "msecs" (milliseconds), "usecs" (microseconds), "hnsecs" (hecto-nanoseconds - i.e. 100 ns) or some subset thereof. There are a few functions that also allow "nsecs", but very little actually has precision greater than hnsecs.

Symbol Description
Types
Duration Represents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds).
TickDuration DEPRECATED Represents a duration of time in system clock ticks, using the highest precision that the system provides.
MonoTime Represents a monotonic timestamp in system clock ticks, using the highest precision that the system provides.
Functions
convert Generic way of converting between two time units.
dur Allows constructing a Duration from the given time units with the given length.
weeks days hours

minutes seconds msecs

usecs hnsecs nsecs | Convenience aliases for dur. | | abs | Returns the absolute value of a duration. |

From Duration
From TickDuration
From units
To Duration
tickDuration.to, std,conv!Duration()
dur!"msecs"(5) or 5.msecs()

| To TickDuration | duration.to, std,conv!TickDuration() |

  • | TickDuration.from!"msecs"(msecs) |

| To units | duration.total!"days" | tickDuration.msecs | convert!("days", "msecs")(msecs) |

Source

core/time.d

@copyrightCopyright 2010 - 2012@licenseBoost License 1.0.@authorsJonathan M Davis and Kato Shoichi
time
:
(struct) core.time.Duration

Represents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds).

It is used when representing a duration of time - such as how long to sleep with Thread.sleep.

In std.datetime, it is also used as the result of various arithmetic operations on time points.

Use the dur function or one of its non-generic aliases to create Durations.

It's not possible to create a Duration of months or years, because the variable number of days in a month or year makes it impossible to convert between months or years and smaller units without a specific date. So, nothing uses Durations when dealing with months or years. Rather, functions specific to months and years are defined. For instance, Date has add!"years" and add!"months" for adding years and months rather than creating a Duration of years or months and adding that to a Date. But Duration is used when dealing with weeks or smaller.

Examples

import std.datetime;

assert(dur!"days"(12) == dur!"hnsecs"(10_368_000_000_000L));
assert(dur!"hnsecs"(27) == dur!"hnsecs"(27));
assert(std.datetime.Date(2010, 9, 7) + dur!"days"(5) ==
       std.datetime.Date(2010, 9, 12));

assert(days(-12) == dur!"hnsecs"(-10_368_000_000_000L));
assert(hnsecs(-27) == dur!"hnsecs"(-27));
assert(std.datetime.Date(2010, 9, 7) - std.datetime.Date(2010, 10, 3) ==
       days(-26));

Examples

import core.time;

// using the dur template
auto numDays = dur!"days"(12);

// using the days function
numDays = days(12);

// alternatively using UFCS syntax
numDays = 12.days;

auto myTime = 100.msecs + 20_000.usecs + 30_000.hnsecs;
assert(myTime == 123.msecs);
Duration
,
(struct) core.time.MonoTimeImpl!(ClockType.normal)
MonoTime
, msecs;
import
(package) sparkles
sparkles
.
(package) sparkles.event_horizon
event_horizon
.
(module) sparkles.event_horizon.capability

Capability seams and the effect row (SPEC §10). Effects-side module: imports only cause — never ring, loop, or scheduler code.

A capability is a plain struct value naming its row label (capName); handlers are values — swapping a handler is passing a different one. Dispatch is monomorphized: ctx.clock.now() is a constant-offset field access plus a direct, inlinable call — the evidence lookup costs zero instructions, and every capability operation is tail-resumptive by construction (the only suspension is a fiber park inside an implementation, a scheduler service, never a reified continuation).

The row is a testability and least-authority convention, not an enforcement boundary — D has ambient authority; the row buys swappability, auditability, and deterministic tests.

capability
:
(alias template) sparkles.event_horizon.clock.isCapability = sparkles.event_horizon.capability.isCapability(C)

A capability is any struct naming its row label. Two capability types with the same capName are interchangeable handlers for the same effect (TestClock for a live clock).

isCapability
,
(alias template) sparkles.event_horizon.clock.isWaker = sparkles.event_horizon.capability.isWaker(W)

The minimal one-shot park/wake seam (SPEC §10.3) that lets effects-side capabilities (TestClock, SimNet) suspend the current task without importing the scheduler. Exactly one wake per prepared handle; wake-before-park makes the park a no-op (the lost-wakeup guard).

isWaker
;
import
(package) sparkles
sparkles
.
(package) sparkles.event_horizon
event_horizon
.
(module) sparkles.event_horizon.errors

The loop's error vocabulary: sparkles.base.io.errors``, re-exported, plus the forms that take a raw errno.

Every failure is the shared IoError, whose kind is computed here, where a completion's res or a call's errno first becomes a value (SPEC §9.1). The module stays a leaf of the library: it imports only sparkles:base, the loop-free error table of sparkles:event-horizon-sys and expected, so every other module can use it without touching ring code.

errors
:
(alias template) sparkles.event_horizon.clock.IoResult = sparkles.base.io.errors.IoResult(T)

The result of every I/O operation.

IoResult
,
(alias template) sparkles.event_horizon.clock.ioOk = sparkles.base.io.errors.ioOk(T)(auto ref T value)

Constructs a successful IoResult carrying value. Attributes infer and the payload is forwarded, so a move-only payload is neither rejected nor copied.

ioOk
;
/// The clock capability concept — the exact expressions callers write. enum bool
(constant) bool sparkles.event_horizon.clock.isClock(C) = isCapability!C && (C.capName == "clock") && __traits(compiles, (ref C c) { _error_ t = c.now(); _error_ r = c.sleep(1.msecs); } )

The clock capability concept — the exact expressions callers write.

isClock
(C) =
(template instance) isCapability!C
isCapability
!C && C.
C.capName
capName
== "clock"
&& __traits(compiles, (ref C
(parameter) C c
c
) {
(unresolved type) MonoTime
MonoTime
_error_ t
t
= c.
c.now
now
();
IoResult!void
_error_ r
r
= c.
c.sleep
sleep
(1.msecs);
}); /** Virtual, deterministic time. `sleep` parks the calling task through the waker seam; `advance` wakes due sleepers in deadline order (FIFO among equals). The determinism contract (ZIO's TestClock lesson): let running fibers reach quiescence — parked on `sleep` — before advancing, or the wake set is racy; `testing.advanceAndSettle` packages that discipline. (A test double: GC-backed waiter storage is fine here.) */ struct
(struct) sparkles.event_horizon.clock.TestClock(W) if (isWaker!W)

Virtual, deterministic time. sleep parks the calling task through the waker seam; advance wakes due sleepers in deadline order (FIFO among equals). The determinism contract (ZIO's TestClock lesson): let running fibers reach quiescence — parked on sleep — before advancing, or the wake set is racy; testing.advanceAndSettle packages that discipline.

(A test double: GC-backed waiter storage is fine here.)

TestClock
(W)
if (isWaker!W) { enum string
(constant) string capName = "clock"
capName
= "clock";
/// Constructs over the executor's park/wake view. this(W
(parameter) W waker
waker
) @safe nothrow @nogc
{ _waker = waker; } /// Current virtual time (starts at `MonoTime.zero`). MonoTime
MonoTime now() const pure nothrow @nogc @safe

Current virtual time (starts at MonoTime.zero).

now
() const @safe pure nothrow @nogc => _now;
/// Parks the calling task until virtual time reaches `now + d`. IoResult!void
IoResult!void sleep(Duration d) nothrow scope @trusted

Parks the calling task until virtual time reaches now + d.

sleep
(
(unresolved type) Duration
Duration
(parameter) Duration d
d
) scope @trusted nothrow
{ if (d <= Duration.zero) return ioOk(); auto
_error_ h
h
= _waker.prepare();
insertSleeper(Sleeper(_now + d, h)); _waker.park(h); return ioOk(); } /// Advances virtual time by `d`, waking sleepers whose deadline is due, /// in deadline order. void
void advance(Duration d) nothrow scope @trusted

Advances virtual time by d, waking sleepers whose deadline is due, in deadline order.

advance
(
(unresolved type) Duration
Duration
(parameter) Duration d
d
) scope @trusted nothrow
{ _now += d; while (_sleepers.length > 0 && _sleepers[0].
_sleepers[0].deadline
deadline
<= _now)
{ auto
_error_ h
h
= _sleepers[0].
_sleepers[0].handle
handle
;
_sleepers = _sleepers[1 .. $]; _waker.wake(h); } } /// Advances exactly to the next pending deadline; `false` if none. bool
bool advanceToNext() nothrow scope @trusted

Advances exactly to the next pending deadline; false if none.

advanceToNext
() scope @trusted nothrow
{ if (_sleepers.length == 0) return false; advance(_sleepers[0].
_sleepers[0].deadline
deadline
- _now);
return true; } /// Parked sleepers (a quiescence probe for test drivers). size_t
size_t pendingSleepers() const pure nothrow @nogc @safe

Parked sleepers (a quiescence probe for test drivers).

pendingSleepers
() const @safe pure nothrow @nogc
=> _sleepers.length; private: static struct
(struct) Sleeper
Sleeper
{
(unresolved type) MonoTime
MonoTime
_error_ deadline
deadline
;
W.Handle
_error_ handle
handle
;
} void
void insertSleeper(Sleeper s) nothrow scope @trusted
insertSleeper
(
(unresolved type) Sleeper
Sleeper
(parameter) Sleeper s
s
) scope @trusted nothrow
{ // Insertion sort: deadline order, FIFO among equals. size_t
_error_ i
i
= _sleepers.length;
while (i > 0 && _sleepers[i - 1].
_sleepers[i - 1].deadline
deadline
> s.
s.deadline
deadline
)
--i; _sleepers = _sleepers[0 .. i] ~ s ~ _sleepers[i .. $]; }
(unresolved type) MonoTime
MonoTime
_error_ _now
_now
= MonoTime.zero;
W
_error_ _waker
_waker
;
(unresolved type) Sleeper
Sleeper
[]
_error_ _sleepers
_sleepers
;
}