core/time/datetime

datetime

Types

13

Date

Date :: struct { year: i64, month: i8, day: i8, }Source

A type representing a date.

The minimum and maximum values for a year can be found in MIN_DATE and MAX_DATE constants. The month field can range from 1 to 12, and the day ranges from 1 to however many days there are in the specified month.

Delta

Delta :: struct { days: i64, seconds: i64, nanos: i64, }Source

A type representing a difference between two instances of datetime.

Note: All fields are i64 because we can also use it to add a number of seconds or nanos to a moment, that are then normalized within their respective ranges.

Error

Error :: enum int { None = 0, Invalid_Year = 1, Invalid_Month = 2, Invalid_Day = 3, Invalid_Hour = 4, Invalid_Minute = 5, Invalid_Second = 6, Invalid_Nano = 7, Invalid_Ordinal = 8, Invalid_Delta = 9, }Source

Possible errors returned by datetime functions.

Month

Month :: enum i8 { 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 one of the months.

Ordinal

Ordinal :: i64Source

Type representing a mononotic day number corresponding to a date.

Ordinal 1 = Midnight Monday, January 1, 1 A.D. (Gregorian)
	        |   Midnight Monday, January 3, 1 A.D. (Julian)

Every other ordinal counts days forwards, starting from the above date.

Time

Time :: struct { hour: i8, minute: i8, second: i8, nano: i32, }Source

A type representing a time within a single day within a nanosecond precision.

Weekday

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

Type representing one of the weekdays.

Constants

4

MAX_DATE

MAX_DATE :: Date = Date{year = 25_252_734_927_766_552, month = 12, day = 31}Source

Maximum valid value for date

The value is chosen such that a conversion date -> ordinal -> date is always safe.

MIN_DATE

MIN_DATE :: Date = Date{year = -25_252_734_927_766_552, month = 1, day = 1}Source

Minimum valid value for date.

The value is chosen such that a conversion date -> ordinal -> date is always safe.

Procedures

42

add_days_to_date

add_days_to_date :: proc(a: Date, days: i64) -> (date: Date, err: Error)Source

Add certain amount of days to a date.

This procedure adds the specified amount of days to a date and returns a new date. The new date would have happened the specified amount of days after the specified date.

add_delta_to_date

add_delta_to_date :: proc(a: Date, delta: Delta) -> (date: Date, err: Error)Source

Add delta to a date.

This procedure adds a delta to a date, and returns a new date. The new date would have happened the time specified by delta after the specified date.

Note: The delta is assumed to be normalized. That is, if it contains seconds or milliseconds, regardless of the amount only the days will be added.

components_to_date

components_to_date :: proc(any_int, any_int, day: i64) -> (date: Date, err: Error)Source

Obtain a date from date components.

This procedure converts date components, specified by a year, a month and a day, into a date object. If the provided date components don't represent a valid date, an error is returned.

components_to_datetime

components_to_datetime :: proc( any_int, any_int, any_int, any_int, any_int, second: i64, nanos: _, ) -> (datetime: DateTime, err: Error)Source

Obtain datetime from components.

This procedure converts date components and time components into a datetime object. If the provided date components or time components don't represent a valid datetime, an error is returned.

components_to_ordinal

components_to_ordinal :: proc(any_int, any_int, day: i64) -> (ordinal: Ordinal, err: Error)Source

Obtain an ordinal from date components.

This procedure converts the specified date, provided by its individual components, into an ordinal. If the specified date is not a valid date, an error is returned.

components_to_time

components_to_time :: proc(any_int, any_int, second: i64, nanos) -> (time: Time, err: Error)Source

Obtain time from time components.

This procedure converts time components, specified by an hour, a minute, a second and nanoseconds, into a time object. If the provided time components don't represent a valid time, an error is returned.

day_number

day_number :: proc(date: Date) -> (day_number: i64, err: Error)Source

Obtain the day number in a year

This procedure returns the number of the day in a year, starting from 1. If the date is not a valid date, an error is returned.

days_remaining

days_remaining :: proc(date: Date) -> (days_remaining: i64, err: Error)Source

Obtain the remaining number of days in a year.

This procedure returns the number of days between the specified date and December 31 of the same year. If the date is not a valid date, an error is returned.

divmod

divmod :: proc(x: T, y: T, loc = #caller_location) -> (a: T, r: T)Source

Caller has to ensure y != 0

last_day_of_month

last_day_of_month :: proc(year: i64, month: i8) -> (day: i8, err: Error)Source

Obtain the last day of a given month on a given year.

This procedure returns the amount of days in a specified month on a specified date. If the specified year or month is not valid, an error is returned.

new_year

new_year :: proc(year: i64) -> (new_year: Date, err: Error)Source

Obtain the new year date of a given year.

This procedure returns the January 1st date of the specified year. If the year is not valid, an error is returned.

normalize_delta

normalize_delta :: proc(delta: Delta) -> (normalized: Delta, err: Error)Source

Normalize the delta.

This procedure normalizes the delta in such a way that the number of seconds is between 0 and the number of seconds in the day and nanoseconds is between 0 and 10^9.

If the value for days overflows during this operation, an error is returned.

ordinal_to_datetime

ordinal_to_datetime :: proc(ordinal: Ordinal) -> (datetime: DateTime, err: Error)Source

Obtain an datetime from an ordinal.

This procedure converts the value of an ordinal into a datetime. Since the ordinal only has the amount of days, the resulting time in the datetime object will always have the time equal to 00:00:00.000.

subtract_dates

subtract_dates :: proc(a: Date, b: Date) -> (delta: Delta, err: Error)Source

Calculate the difference between two dates.

This procedure calculates the difference between two dates a - b, and returns a delta between the two dates in days. If either a or b is not a valid date, an error is returned.

subtract_datetimes

subtract_datetimes :: proc(a: DateTime, b: DateTime) -> (delta: Delta, err: Error)Source

Calculate the difference between two datetimes.

This procedure calculates the difference between two datetimes, a - b, and returns a delta between the two dates. The difference is returned in all three fields of the Delta struct: the difference in days, the difference in seconds and the difference in nanoseconds.

If either a or b is not a valid datetime, an error is returned.

unsafe_date_to_ordinal

unsafe_date_to_ordinal :: proc(date: Date) -> (ordinal: Ordinal)Source

The following procedures don't check whether their inputs are in a valid range. They're still exported for those who know their inputs have been validated. Obtain an ordinal from a date.

This procedure converts a date into an ordinal. If the date is not a valid date, the result is unspecified.

validate_date

validate_date :: proc(date: Date) -> (err: Error)Source

Check for errors in date formation.

This procedure validates all fields of a date, and if any of the fields is outside of allowed range, an error is returned.

year_end

year_end :: proc(year: i64) -> (year_end: Date, err: Error)Source

Obtain the end year of a given date.

This procedure returns the December 31st date of the specified year. If the year is not valid, an error is returned.

year_range

year_range :: proc(year: i64, allocator: mem.Allocator = context.allocator) -> (range: []Date)Source

Obtain the range of dates for a given year.

This procedure returns dates, for every day of a given year in a slice.

Procedure Groups

3

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.