Grav v1.14.0

Time and Stopwatch

import "std/sys" (included in import "std") · namespace std.sys

Clocks and timing. There are two kinds of clock:

  • Wall-clock time (Time.now(), Time.nowMs()): seconds or milliseconds since 1 January 1970 UTC. Use it for timestamps. It can jump when the system clock is adjusted.
  • Monotonic time (Time.monotonicMs(), monotonicNs()): only ever moves forward. Use it to measure how long something takes.

A Stopwatch measures elapsed time on the monotonic clock.

grav
import "std"

fn main() {
    let sw = new Stopwatch()
    Time.sleepMs(20)
    let ms = sw.elapsedMs()
    print(ms >= 20 && ms < 1000)
    sw.reset()
    print(sw.elapsedMs() < 20)
    print(Time.now() > 1700000000)
    print(Time.nowMs() / 1000 >= Time.now() - 1)
}
Output
true
true
true
true

For calendar dates and formatting, see DateTime.

Duration

A Duration is a length of time. It prints compactly, parses the same notation, and supports +, -, * and comparisons:

grav
import "std"

fn main() {
    let timeout = Duration.parse("1m30s")
    print(timeout.seconds())
    print(timeout + Duration.ofSeconds(45))
    print(Duration.ofMinutes(150))
    print(Duration.ofMinutes(2) < timeout * 2)
    let sw = new Stopwatch()
    print(sw.elapsed() < Duration.ofSeconds(1))
}
Output
90
2m15s
2h30m
true
true

Duration.parse accepts d, h, m, s and ms units ("2d 3h", "250ms") and throws ValueError for anything else.

API

class Time

Static members

  • static fn now() -> long — unix seconds
  • static fn nowMs() -> long — Milliseconds since 1970-01-01 00:00 UTC (wall clock).
  • static fn monotonicMs() -> long — Milliseconds on a clock that never goes back (for measuring durations).
  • static fn monotonicNs() -> long — Nanoseconds on a clock that never goes back.
  • static fn sleepMs(ms: long) — Pauses the calling thread for ms milliseconds.

class Stopwatch

Constructors

  • new Stopwatch()

Methods

  • fn reset() — Starts measuring again from now.
  • fn elapsedNs() -> long — Nanoseconds since the stopwatch was created or reset.
  • fn elapsedMs() -> long — Milliseconds since the stopwatch was created or reset.
  • fn elapsedSeconds() -> double — Seconds since the stopwatch was created or reset.
  • fn elapsed() -> Duration — The time since the stopwatch was created or reset.

class Duration

A length of time, stored in milliseconds: Duration.ofMinutes(90) prints as "1h30m".

Constructors

  • new Duration(ms: long)

Static members

  • static fn ofMillis(n: long) -> Duration — A duration of n milliseconds.
  • static fn ofSeconds(n: long) -> Duration — A duration of n seconds.
  • static fn ofMinutes(n: long) -> Duration — A duration of n minutes.
  • static fn ofHours(n: long) -> Duration — A duration of n hours.
  • static fn ofDays(n: long) -> Duration — A duration of n days.
  • static fn parse(text: string) -> Duration — Parses text like "90s", "1h30m", "2d", "250ms", "1h 5m 3s" (units d, h, m, s, ms); ValueError for anything else.

Fields

  • readonly ms: long — The length in milliseconds.

Methods

  • fn seconds() -> long — The length in whole seconds.
  • fn minutes() -> long — The length in whole minutes.
  • fn hours() -> long — The length in whole hours.
  • fn totalSeconds() -> float — The length in seconds, with the fraction.
  • fn operator+(o: Duration) -> Duration — The sum of two durations.
  • fn operator-(o: Duration) -> Duration — The difference of two durations.
  • fn operator*(k: long) -> Duration — The duration multiplied by a whole number.
  • fn operator==(o: Duration) -> bool — Whether two durations are equally long.
  • fn operator<(o: Duration) -> bool — Whether this duration is shorter.
  • fn toString() -> string — As text: "1d2h", "1h30m", "45s", "250ms", "0s" (negative durations start with "-").
  • fn sleep() — Pauses the calling thread for this long.

Edit this page on GitHub