Skip to content

aivi.time

Clock, timestamp, and millisecond helpers.

aivi.time mixes two kinds of tools:

  • task-based functions that ask the runtime for clock or timestamp work
  • pure helpers for doing ordinary millisecond math in your own code

Unlike aivi.duration, this module does not define a time domain today. EpochMs is still just an Int alias.

Import

aivi
use aivi.time (
    EpochMs
    nowMs
    monotonicMs
    format
    parse
    isoPattern
    datePattern
    timePattern
    formatIso
    formatDate
    formatTime
    parseIso
    msPerSecond
    msPerMinute
    msPerHour
    msPerDay
    toSeconds
    toMinutes
    toHours
    toDays
    fromSeconds
    fromMinutes
    fromHours
    fromDays
    elapsed
)

Runtime clock functions

ValueTypeDescription
nowMsTask Text IntCurrent wall-clock time in milliseconds since the Unix epoch
monotonicMsTask Text IntMonotonic milliseconds since the runtime started
formatInt -> Text -> Task Text TextFormat a timestamp using a pattern
parseText -> Text -> Task Text IntParse text into a timestamp

nowMs

aivi
nowMs : Task Text Int

Use this when you need a real-world timestamp for storage, logging, or comparing with other epoch-based values.

monotonicMs

aivi
monotonicMs : Task Text Int

Use this when you want a steady clock for measuring elapsed time inside the running program. It is a better fit for timing than nowMs, because it is not tied to the wall clock.

aivi
use aivi.time (
    nowMs
    monotonicMs
)

value savedAt : Task Text Int = nowMs
value stopwatchNow : Task Text Int = monotonicMs

Current runtime note for format and parse

The API surface is already present, but the current runtime behavior is intentionally small:

  • format ignores the pattern argument and returns the millisecond number as plain text
  • parse ignores the pattern argument and only accepts text that is already an integer millisecond value

That means formatIso, formatDate, formatTime, and parseIso currently share the same fallback behavior.

Current limits

  • timestamps are raw epoch-millisecond Int values, not a dedicated domain
  • format and parse are still partial runtime stubs
  • there is no richer calendar/date-time domain surface yet

format

aivi
format : Int -> Text -> Task Text Text

The surface API takes an epoch millisecond value and a pattern string.

aivi
use aivi.time (
    format
    isoPattern
)

value shown : Task Text Text = format 1735689600000 isoPattern

Today this returns "1735689600000", not a human-readable ISO timestamp yet.

parse

aivi
parse : Text -> Text -> Task Text Int

The surface API takes text plus a pattern string and returns epoch milliseconds.

aivi
use aivi.time (
    parse
    isoPattern
)

value parsed : Task Text Int = parse "1735689600000" isoPattern

Today this succeeds for decimal millisecond text and fails for ordinary date strings such as "2025-01-01T00:00:00".

Pattern constants and wrappers

ValueTypeDescription
isoPatternTextNamed pattern string for ISO-like timestamps
datePatternTextNamed pattern string for dates
timePatternTextNamed pattern string for times
formatIsoInt -> Task Text Textformat ms isoPattern
formatDateInt -> Task Text Textformat ms datePattern
formatTimeInt -> Task Text Textformat ms timePattern
parseIsoText -> Task Text Intparse text isoPattern

These names make intent clearer even before the full formatter/parser behavior lands.

Pure millisecond helpers

ValueTypeDescription
EpochMsIntType alias for epoch milliseconds
msPerSecondInt1000
msPerMinuteInt60000
msPerHourInt3600000
msPerDayInt86400000
toSecondsInt -> IntConvert milliseconds to whole seconds
toMinutesInt -> IntConvert milliseconds to whole minutes
toHoursInt -> IntConvert milliseconds to whole hours
toDaysInt -> IntConvert milliseconds to whole days
fromSecondsInt -> IntConvert seconds to milliseconds
fromMinutesInt -> IntConvert minutes to milliseconds
fromHoursInt -> IntConvert hours to milliseconds
fromDaysInt -> IntConvert days to milliseconds
elapsedInt -> Int -> IntSubtract start from finish
aivi
use aivi.time (
    fromSeconds
    fromMinutes
    elapsed
    toSeconds
)

value timeoutMs : Int = fromSeconds 30
value cacheTtlMs : Int = fromMinutes 5
value requestTimeMs : Int = elapsed 1200 1875
value requestTimeSeconds : Int = toSeconds requestTimeMs

Example — wall clock plus steady clock

aivi
use aivi.time (
    nowMs
    monotonicMs
    elapsed
)

value createdAt : Task Text Int = nowMs
value timerStart : Int = 1000
value timerNow : Int = 1450
value timerElapsed : Int = elapsed timerStart timerNow
value steadySnapshot : Task Text Int = monotonicMs

AIVI Language Manual