core/time

time

Types

8

Duration

Duration :: i64Source

Type representing duration, with nanosecond precision. This is the regular Unix timestamp, scaled to nanosecond precision.

Month

Month :: enum int { January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, November = 11, December = 12, }Source

Type representing a month.

Stopwatch

Stopwatch :: struct { running: bool, _start_time: Tick, _accumulation: Duration, }Source

Type representing a stopwatch.

The stopwatch is used for measuring the total time in multiple "runs". When the stopwatch is started, it starts counting time. When the stopwatch is stopped, the difference in time between the last start and the stop is added to the total. When the stopwatch resets, the total is reset.

Tick

Tick :: struct { _nsec: i64, }Source

Type representing monotonic time, useful for measuring durations.

Time

Time :: struct { _nsec: i64, }Source

Specifies time since the UNIX epoch, with nanosecond precision.

Capable of representing any time within the following range:

  • min: 1677-09-21 00:12:44.145224192 +0000 UTC
  • max: 2262-04-11 23:47:16.854775807 +0000 UTC

Weekday

Weekday :: enum int { Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, }Source

Type representing a weekday.

Constants

31

Variables

1

days_before

days_before :: [?]i32 = [?]i32{ 0, 31, 31 + 28, 31 + 28 + 31, 31 + 28 + 31 + 30, 31 + 28 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30, 31 + 28 + 31 + 30 + 31 + 30 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, 31 + 28 + 31 + 30 Source

Days before each month in a year, not counting the leap day on february 29th.

Procedures

77

accurate_sleep

accurate_sleep :: proc(d: Duration)Source

Accurate sleep

This procedure sleeps for the duration specified by d, very accurately.

Note: Implementation borrowed from: this source

Note(linux): The accuracy is within around 4µs (microseconds), in the worst case.

Note(windows): The accuracy depends but is comparable with regular sleep in the worst case. To get the same kind of accuracy as on Linux, have your program call windows.timeBeginPeriod(1) to tell Windows to use a more accurate timer for your process. Additionally your program should call windows.timeEndPeriod(1) once you're done with accurate_sleep.

benchmark

benchmark :: proc(options: ^Benchmark_Options, allocator: mem.Allocator = context.allocator) -> (err: Benchmark_Error)Source

Benchmark a procedure.

This procedure produces a benchmark. The procedure specified in the bench field of the options parameter will be benchmarked. The following metrics can be obtained:

  • Run time of the procedure
  • Number of elements per second processed on average
  • Number of bytes per second this processed on average

In order to obtain these metrics, the bench() procedure writes to options struct the number of elements or bytes it has processed.

components_to_time

components_to_time :: proc( any_int, any_int, any_int, any_int, any_int, second: i64, nsec: _, ) -> (t: Time, ok: bool)Source

Convert datetime components into time.

This procedure calculates the time from datetime components supplied in the arguments to this procedure. If the datetime components don't represent a valid datetime, the function returns false in the second argument.

duration_round

duration_round :: proc(d: Duration, m: Duration) -> (Duration)Source

Round a duration to a specific unit

This procedure rounds the duration to a specific unit

Note: Any duration can be supplied as a unit.

  • d: The duration to round
  • m: The unit to round to
  • The duration d, rounded to the unit specified by m

Example:

time.duration_round(my_duration, time.Second)

duration_truncate

duration_truncate :: proc(d: Duration, m: Duration) -> (Duration)Source

Truncate the duration to the specified unit.

This procedure truncates the duration d to the unit specified by m.

Note: Any duration can be supplied as a unit.

  • d: The duration to truncate.
  • m: The unit to truncate to.
  • The duration d, truncated to the unit specified by m.

Example:

time.duration_round(my_duration, time.Second)

has_invariant_tsc

has_invariant_tsc :: proc() -> (bool)Source

Check if the CPU has invariant TSC.

This procedure checks if the CPU contains an invariant TSC (Time stamp counter). Invariant TSC is a feature of modern processors that allows them to run their TSC at a fixed frequency, independent of ACPI state, and CPU frequency.

iso8601_to_components

iso8601_to_components :: proc(iso_datetime: string) -> (res: dt.DateTime, utc_offset: int, is_leap: bool, consumed: int)Source

Parse an ISO 8601 string into a datetime and a UTC offset in minutes.

This procedure parses an ISO 8601 string of roughly the following format:

YYYY-MM-DD[Tt]HH:mm:ss[.nn][Zz][+-]HH:mm

And returns datetime, in UTC represented by that string, and the UTC offset, in minutes.

Inputs:

  • iso_datetime: The string to be parsed

Returns:

  • res: The parsed datetime, in UTC.
  • utc_offset: The UTC offset, in minutes.
  • is_leap: Specifies whether the moment was a leap second.
  • consumed: The number of bytes consumed by parsing the string.

Notes:

  • This procedure performs no validation on whether components are valid,

e.g. it'll return hour = 25 if that's what it's given in the specified string.

iso8601_to_time_and_offset

iso8601_to_time_and_offset :: proc(iso_datetime: string, is_leap: ^bool) -> (res: Time, utc_offset: int, consumed: int)Source

Parse an ISO 8601 string into a time and a UTC offset in minutes.

This procedure parses an ISO 8601 string of roughly the following format:

YYYY-MM-DD[Tt]HH:mm:ss[.nn][Zz][+-]HH:mm

And returns time, in UTC represented by that string, and the UTC offset, in minutes.

Inputs:

  • iso_datetime: The string to be parsed.
  • is_leap: Optional output parameter, specifying if the moment was a leap second.

Returns:

  • res: The time in UTC.
  • utc_offset: The UTC offset of the time, in minutes.
  • consumed: Number of bytes consumed by parsing the string.

Notes:

  • Only 4-digit years are accepted.
  • Leap seconds are smeared into 23:59:59.

iso8601_to_time_utc

iso8601_to_time_utc :: proc(iso_datetime: string, is_leap: ^bool) -> (res: Time, consumed: int)Source

Parse an ISO 8601 string into a time with UTC offset applied to it.

This procedure parses an ISO 8601 string of roughly the following format:

YYYY-MM-DD[Tt]HH:mm:ss[.nn][Zz][+-]HH:mm

And returns time, in UTC represented by that string. In case the timezone offset is specified in the string, that timezone is applied to time.

Inputs:

  • iso_datetime: The string to be parsed.
  • is_leap: Optional output parameter, specifying if the moment was a leap second.

Returns:

  • res: The time represented by iso_datetime, with UTC offset applied.
  • consumed: Number of bytes consumed by parsing the string.

Notes:

  • Only 4-digit years are accepted.
  • Leap seconds are smeared into 23:59:59.

rfc3339_to_components

rfc3339_to_components :: proc(rfc_datetime: string) -> (res: dt.DateTime, utc_offset: int, is_leap: bool, consumed: int)Source

Parse an RFC 3339 string into a datetime and a UTC offset in minutes.

This procedure parses the specified RFC 3339 strings of roughly the following format:

YYYY-MM-DD[Tt]HH:mm:ss[.nn][Zz][+-]HH:mm

And returns the datetime, in UTC and the UTC offset, in minutes, that were represented by the RFC 3339 string.

Inputs:

  • rfc_datetime: The RFC 3339 string to parse.

Returns:

  • res: The datetime, in UTC, that was parsed from the RFC 3339 string.
  • utc_offset: The UTC offset, in minutes, that was parsed from the RFC 3339

string.

  • is_leap: Specifies whether the moment was a leap second.
  • consumed: Number of bytes consumed by parsing the string.

Performs no validation on whether components are valid, e.g. it'll return hour = 25 if that's what it's given

rfc3339_to_time_and_offset

rfc3339_to_time_and_offset :: proc(rfc_datetime: string, is_leap: ^bool) -> (res: Time, utc_offset: int, consumed: int)Source

Parse an RFC 3339 string into a time and a UTC offset in minutes.

This procedure parses the specified RFC 3339 strings of roughly the following format:

YYYY-MM-DD[Tt]HH:mm:ss[.nn][Zz][+-]HH:mm

And returns the time, in UTC and a UTC offset, in minutes, that were represented by the RFC 3339 string.

Inputs:

  • rfc_datetime: The RFC 3339 string to be parsed.
  • is_leap: Optional output parameter specifying whether the moment was a

leap second.

Returns:

  • res: The time, in UTC, that was parsed from the RFC 3339 string.
  • utc_offset: The UTC offset, in minutes, that was parsed from the RFC 3339

string.

  • consumed: The number of bytes consumed by parsing the string.

Notes:

  • Only 4-digit years are accepted.
  • Leap seconds are smeared into 23:59:59.

rfc3339_to_time_utc

rfc3339_to_time_utc :: proc(rfc_datetime: string, is_leap: ^bool) -> (res: Time, consumed: int)Source

Parse an RFC 3339 string into time with a UTC offset applied to it.

This procedure parses the specified RFC 3339 strings of roughly the following format:

YYYY-MM-DD[Tt]HH:mm:ss[.nn][Zz][+-]HH:mm

And returns the time that was represented by the RFC 3339 string, with the UTC offset applied to it.

Inputs:

  • rfc_datetime: An RFC 3339 string to parse.
  • is_leap: Optional output parameter specifying whether the moment was a leap

second.

Returns:

  • res: The time, with UTC offset applied, that was parsed from the RFC 3339

string.

  • consumed: The number of bytes consumed by parsing the RFC 3339 string.

Notes:

  • Only 4-digit years are accepted.
  • Leap seconds are smeared into 23:59:59.

stopwatch_duration

stopwatch_duration :: proc(stopwatch: Stopwatch) -> (Duration)Source

Obtain the total time, counted by the stopwatch.

This procedure obtains the total time, counted by the stopwatch. If the stopwatch isn't stopped at the time of calling this procedure, the time between the last start and the current time is also accounted for.

tick_lap_time

tick_lap_time :: proc(prev: ^Tick) -> (Duration)Source

Incrementally obtain durations since last tick.

This procedure returns the duration between the current tick and the tick stored in prev pointer, and then stores the current tick in location, specified by prev. If the prev pointer contains an zero-initialized tick, then the returned duration is 0.

This procedure is meant to be used in a loop, or in other scenarios, where one might want to obtain time between multiple ticks at specific points.

time_to_rfc3339

time_to_rfc3339 :: proc(time: Time, utc_offset: int, include_nanos: untyped boolean = true, allocator: mem.Allocator = context.allocator) -> (res: string, ok: bool)Source

Serialize the timestamp as a RFC 3339 string.

The boolean ok is false if the time is not a valid datetime, or if allocating the result string fails.

Inputs:

  • utc_offset: offset in minutes wrt UTC (ie. the timezone)
  • include_nanos: whether to include nanoseconds in the result.

to_string_hms_12

to_string_hms_12 :: proc(t: Time, buf: []u8, ampm: [2]string) -> (res: string)Source

Formats a Time as a 12-hour hh:mm:ss pm string

Does not allocate

  • t: The Time to format
  • buf: The backing buffer to use
  • ampm: An optional pair of am/pm strings to use in place of the default
  • res: The formatted string, backed by buf

Example:

buf: [64]u8
now := time.now()
fmt.println(time.to_string_hms_12(now, buf[:]))
fmt.println(time.to_string_hms_12(now, buf[:], {"㏂", "㏘"}))

tsc_frequency

tsc_frequency :: proc(fallback_sleep = 2 * Second) -> (bool, u64)Source

Obtain the CPU's TSC frequency, in hertz.

This procedure tries to obtain the CPU's TSC frequency in hertz. If the CPU doesn't have an invariant TSC, this procedure returns with an error. Otherwise an attempt is made to fetch the TSC frequency from the OS. If this fails, the frequency is obtained by sleeping for the specified amount of time and dividing the readings from TSC by the duration of the sleep.

The duration of sleep can be controlled by fallback_sleep parameter.

Procedure Groups

4

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.