std / time

time

import "std:time"

Provides time operations using POSIX clock_gettime and nanosleep. Duration is measured in nanoseconds as int64. Also provides calendar date operations with Month, Weekday, and Date types for Gregorian calendar dates (UTC).

View source on Codeberg →

Types

#
type Duration int64

Duration represents the elapsed time between two instants, measured in nanoseconds as an int64 value.

#
type Time struct {
    sec int64
    nsec int64
}

Time represents an instant in time, stored as seconds and nanoseconds since the Unix epoch (January 1, 1970 UTC).

Duration Constants

#
const (
    Nanosecond  Duration = 1
    Microsecond Duration = 1000
    Millisecond Duration = 1000000
    Second      Duration = 1000000000
    Minute      Duration = 60000000000
    Hour        Duration = 3600000000000
)

Common duration values for convenient time calculations. Multiply by these constants to convert to nanosecond-based durations.

Time Construction

#
func Now() Time

Returns the current wall-clock time as a Time value using POSIX clock_gettime.

#
func Unix(sec int64, nsec int64) Time

Returns the Time corresponding to the given Unix time: sec seconds and nsec nanoseconds since January 1, 1970 UTC.

#
func NowUnix() int64

Returns the current time as a Unix timestamp in seconds.

Time Accessors

#
func UnixSec(t Time) int64

Returns the Unix time of t in seconds since the epoch.

#
func UnixNano(t Time) int64

Returns the Unix time of t in nanoseconds since the epoch.

Time Arithmetic

#
func Since(t Time) Duration

Returns the duration elapsed since t. Equivalent to Sub(Now(), t).

#
func Until(t Time) Duration

Returns the duration until t. Equivalent to Sub(t, Now()).

#
func Sub(t, u Time) Duration

Returns the duration t - u. If the result exceeds the maximum or minimum value that can be stored in a Duration, the maximum or minimum duration is returned.

#
func Add(t Time, d Duration) Time

Returns the time t + d, adding the given duration to the time value.

#
func Before(t, u Time) bool

Reports whether the time instant t is before u.

#
func After(t, u Time) bool

Reports whether the time instant t is after u.

#
func Equal(t, u Time) bool

Reports whether t and u represent the same time instant.

Duration Conversions

#
func Seconds(d Duration) float64

Returns the duration as a floating-point number of seconds.

#
func Milliseconds(d Duration) int64

Returns the duration as an integer millisecond count.

#
func Microseconds(d Duration) int64

Returns the duration as an integer microsecond count.

#
func Nanoseconds(d Duration) int64

Returns the duration as an integer nanosecond count.

Sleep

#
func Sleep(d Duration)

Pauses the current execution for at least the specified duration using POSIX nanosleep.

#
func SleepMillis(ms int)

Pauses the current execution for at least the specified number of milliseconds. Convenience wrapper around Sleep.

Monotonic Clock

#
func MonoNow() int64

Returns the current value of the monotonic clock in nanoseconds. The monotonic clock is not affected by system time changes and is suitable for measuring elapsed time.

Formatting

#
func FormatUnix(t Time) string

Returns a human-readable string representation of the time value based on its Unix timestamp.

Date Types

#
type Month int

Month represents a month of the year (January = 1, December = 12).

#
type Weekday int

Weekday represents a day of the week (Sunday = 0, Saturday = 6).

#
type Date struct {
    year  int
    month Month
    day   int
}

Date represents a calendar date with no time-of-day component. All dates use the Gregorian calendar in UTC.

Month Constants

#
const (
    January   Month = 1
    February  Month = 2
    March     Month = 3
    April     Month = 4
    May       Month = 5
    June      Month = 6
    July      Month = 7
    August    Month = 8
    September Month = 9
    October   Month = 10
    November  Month = 11
    December  Month = 12
)

Month constants representing each month of the year.

Weekday Constants

#
const (
    Sunday    Weekday = 0
    Monday    Weekday = 1
    Tuesday   Weekday = 2
    Wednesday Weekday = 3
    Thursday  Weekday = 4
    Friday    Weekday = 5
    Saturday  Weekday = 6
)

Weekday constants representing each day of the week.

Date Construction

#
func NewDate(year int, month Month, day int) Date

Creates a Date from year, month, and day components.

#
func Today() Date

Returns the current date (UTC).

#
func DateFromTime(t Time) Date

Extracts the UTC date from a Time value.

Date Accessors

#
func Year(d Date) int

Returns the year component of a Date.

#
func MonthOf(d Date) Month

Returns the month component of a Date.

#
func Day(d Date) int

Returns the day-of-month component of a Date.

#
func WeekdayOf(d Date) Weekday

Returns the day of the week for a Date.

#
func YearDay(d Date) int

Returns the day of the year (1-366) for a Date.

Date Comparison

#
func DateEqual(a, b Date) bool

Reports whether two dates represent the same day.

#
func DateBefore(a, b Date) bool

Reports whether date a is before date b.

#
func DateAfter(a, b Date) bool

Reports whether date a is after date b.

Date Arithmetic

#
func AddDays(d Date, n int) Date

Returns the date that is n days after d. n can be negative to subtract days.

#
func DaysBetween(a, b Date) int

Returns the number of days from a to b. Positive if b is after a.

Date Validation

#
func IsLeapYear(year int) bool

Reports whether the given year is a leap year.

#
func DaysInMonth(year int, month Month) int

Returns the number of days in the given month of the given year.

#
func ValidDate(year int, month Month, day int) bool

Reports whether year, month, and day form a valid calendar date.

Date Formatting

#
func FormatDate(d Date) string

Formats a Date as an ISO 8601 string "YYYY-MM-DD".

#
func ParseDate(s string) (Date, bool)

Parses an ISO 8601 date string "YYYY-MM-DD". Returns the Date and true on error.

Date Conversion

#
func DateToTime(d Date) Time

Converts a Date to a Time at midnight UTC.