Skip to content

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:

  1. 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.
  2. 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).
  3. time.Duration (Time Span)

    • Represents a non-negative duration of time.
    • Constructed using time.seconds(n) or time.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.cdt

API 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 ​

FunctionSignatureDescription
elapsedelapsed(start: time.Instant) -> time.Duration | time.TimeErrorCalculates the duration elapsed since the given monotonic instant
format_isoformat_iso(ts: time.Timestamp) -> String | time.TimeErrorFormats a UTC timestamp in RFC 3339 / ISO 8601 (YYYY-MM-DDTHH:MM:SSZ)
from_unixfrom_unix(secs: Int) -> time.TimestampCreates a Timestamp from Unix epoch seconds (UTC)
from_unix_millisfrom_unix_millis(ms: Int) -> time.TimestampCreates a Timestamp from Unix epoch milliseconds (UTC)
millisecondsmilliseconds(value: Int) -> time.Duration | time.TimeErrorCreates a duration from the given number of milliseconds
monotonicmonotonic() -> time.InstantReads the current monotonic clock instant
nownow() -> time.TimeError | time.TimestampReads the current system clock time
parse_isoparse_iso(s: String) -> time.Timestamp | time.TimeErrorParses YYYY-MM-DD or RFC 3339 / ISO 8601 timestamps (Z or +/-HH:MM)
secondsseconds(value: Int) -> time.Duration | time.TimeErrorCreates a duration from the given number of seconds
sleepsleep(ms: Int) -> Void | time.TimeErrorSleeps the current thread for ms milliseconds

Methods ​

TypeMethodSignatureDescription
Durationmillisecondsmilliseconds() -> Int | time.TimeErrorReturns the number of full milliseconds in the duration
Durationnanosecondsnanoseconds() -> Int | time.TimeErrorReturns the number of full nanoseconds in the duration
Durationsecondsseconds() -> Int | time.TimeErrorReturns the number of full seconds in the duration
Timestampaddadd(dur: time.Duration) -> time.Timestamp | time.TimeErrorReturns a new timestamp shifted forward by dur
Timestampdayday() -> IntUTC day of month (1..31)
Timestampdiffdiff(other: time.Timestamp) -> time.Duration | time.TimeErrorReturns self - other duration (TimeError if self < other)
Timestamphourhour() -> IntUTC hour (0..23)
Timestampminuteminute() -> IntUTC minute (0..59)
Timestampmonthmonth() -> IntUTC month (1..12)
Timestampsecondsecond() -> IntUTC second (0..59)
Timestampsubsub(dur: time.Duration) -> time.Timestamp | time.TimeErrorReturns a new timestamp shifted backward by dur
Timestampto_isoto_iso() -> String | time.TimeErrorFormats the timestamp as UTC RFC 3339 / ISO 8601
Timestampunix_millisecondsunix_milliseconds() -> Int | time.TimeErrorReturns Unix timestamp in milliseconds (UTC). Rounded down for values before epoch.
Timestampunix_secondsunix_seconds() -> Int | time.TimeErrorReturns Unix timestamp in seconds (UTC). Rounded down for values before epoch.
Timestampweekdayweekday() -> IntUTC ISO weekday (1 = Monday .. 7 = Sunday)
Timestampyearyear() -> IntUTC calendar year