cdt.time
The cdt.time standard library provides system and monotonic clock operations, duration arithmetic, and UTC Unix epoch conversions.
conduit
import * as time from "cdt.time"Conceptual Overview
System Time vs. Monotonic Time
Conduit maintains a strict distinction between wall-clock system time and monotonic measurement time:
time.Timestamp(System Clock)- Measured via
time.now(). - Represents a specific point on the human calendar.
- Used for logging events, database timestamps, and Unix epoch conversion.
- May change or jump if the host system clock is updated or synchronized via NTP.
- Measured via
time.Instant(Monotonic Clock)- Measured via
time.monotonic(). - Guaranteed to never jump backwards, even during NTP adjustments or leap seconds.
- Exclusively used for measuring elapsed intervals, benchmarking, and timeouts via
time.elapsed(start).
- Measured via
time.Duration(Time Span)- Represents a non-negative duration of time.
- Constructed using
time.seconds(n)ortime.milliseconds(n). - Created automatically when calculating
time.elapsed(instant). - Cannot represent negative spans. Attempting to construct a negative duration returns a
time.TimeError.
Runnable Example
conduit
import * as time from "cdt.time"
func run_timer() -> Void | time.TimeError {
ts := time.now()?
print("Timestamp seconds: {ts.unix_seconds()?}")
dur := time.seconds(5)?
print("Duration seconds: {dur.seconds()?}")
start := time.monotonic()
_elapsed := time.elapsed(start)?
print("Time measurement completed successfully.")
}
func main() -> Void {
switch run_timer() {
case Void:
case time.TimeError(e): print(e.message)
}
}Running the Example
bash
conduit run time_demo.cdtAPI Reference
Types
Duration
- Qualified Name:
time.Duration - Category: Opaque Type
- Description: Non-negative time span representation
Instant
- Qualified Name:
time.Instant - Category: Opaque Type
- Description: Monotonically increasing clock measurement point for benchmarks and elapsed timing
TimeError
- Qualified Name:
time.TimeError - Category: Error Type
- Description: Error occurring during time operations (overflow, negative duration, invalid order)
- Fields:
message: String
Timestamp
- Qualified Name:
time.Timestamp - Category: Opaque Type
- Description: Moment of system time measured from the system clock
Functions
| Function | Signature | Description |
|---|---|---|
elapsed | elapsed(start: time.Instant) -> time.Duration | time.TimeError | Calculates the duration elapsed since the given monotonic instant |
format_iso | format_iso(ts: time.Timestamp) -> String | time.TimeError | Formats a UTC timestamp in RFC 3339 / ISO 8601 (YYYY-MM-DDTHH:MM:SSZ) |
from_unix | from_unix(secs: Int) -> time.Timestamp | Creates a Timestamp from Unix epoch seconds (UTC) |
from_unix_millis | from_unix_millis(ms: Int) -> time.Timestamp | Creates a Timestamp from Unix epoch milliseconds (UTC) |
milliseconds | milliseconds(value: Int) -> time.Duration | time.TimeError | Creates a duration from the given number of milliseconds |
monotonic | monotonic() -> time.Instant | Reads the current monotonic clock instant |
now | now() -> time.TimeError | time.Timestamp | Reads the current system clock time |
parse_iso | parse_iso(s: String) -> time.Timestamp | time.TimeError | Parses YYYY-MM-DD or RFC 3339 / ISO 8601 timestamps (Z or +/-HH:MM) |
seconds | seconds(value: Int) -> time.Duration | time.TimeError | Creates a duration from the given number of seconds |
sleep | sleep(ms: Int) -> Void | time.TimeError | Sleeps the current thread for ms milliseconds |
Methods
| Type | Method | Signature | Description |
|---|---|---|---|
Duration | milliseconds | milliseconds() -> Int | time.TimeError | Returns the number of full milliseconds in the duration |
Duration | nanoseconds | nanoseconds() -> Int | time.TimeError | Returns the number of full nanoseconds in the duration |
Duration | seconds | seconds() -> Int | time.TimeError | Returns the number of full seconds in the duration |
Timestamp | add | add(dur: time.Duration) -> time.Timestamp | time.TimeError | Returns a new timestamp shifted forward by dur |
Timestamp | day | day() -> Int | UTC day of month (1..31) |
Timestamp | diff | diff(other: time.Timestamp) -> time.Duration | time.TimeError | Returns self - other duration (TimeError if self < other) |
Timestamp | hour | hour() -> Int | UTC hour (0..23) |
Timestamp | minute | minute() -> Int | UTC minute (0..59) |
Timestamp | month | month() -> Int | UTC month (1..12) |
Timestamp | second | second() -> Int | UTC second (0..59) |
Timestamp | sub | sub(dur: time.Duration) -> time.Timestamp | time.TimeError | Returns a new timestamp shifted backward by dur |
Timestamp | to_iso | to_iso() -> String | time.TimeError | Formats the timestamp as UTC RFC 3339 / ISO 8601 |
Timestamp | unix_milliseconds | unix_milliseconds() -> Int | time.TimeError | Returns Unix timestamp in milliseconds (UTC). Rounded down for values before epoch. |
Timestamp | unix_seconds | unix_seconds() -> Int | time.TimeError | Returns Unix timestamp in seconds (UTC). Rounded down for values before epoch. |
Timestamp | weekday | weekday() -> Int | UTC ISO weekday (1 = Monday .. 7 = Sunday) |
Timestamp | year | year() -> Int | UTC calendar year |