Documentation

Std.Time

Time #

The Lean API for date, time, and duration functionalities.

Overview #

This module of the standard library defines various concepts related to time, such as dates, times, time zones and durations. These types are designed to be strongly-typed and to avoid problems with conversion. It offers both unbounded and bounded variants to suit different use cases, like adding days to a date or representing ordinal values.

Date and Time Components #

Date and time components are classified into different types based how you SHOULD use them. These components are categorized as:

Offset #

Offsets represent unbounded shifts in specific date or time units. They are typically used in operations like date.addDays where a Day.Offset is the parameter. Some offsets, such as Month.Offset, do not correspond directly to a specific duration in seconds, as their value depends on the context (if the year is leap or the size of the month). Offsets with a clear correspondent to seconds can be converted because they use an internal type called UnitVal.

Ordinal #

Ordinal types represent specific bounded values in reference to another unit, e.g., Day.Ordinal represents a day in a month, ranging from 1 to 31. Some ordinal types like Hour.Ordinal and Second.Ordinal, allow for values beyond the normal range (e.g, 60 seconds) to accommodate special cases with leap seconds like 23:59:60 that is valid in ISO 8601.

Span #

Span types are used as subcomponents of other types. They represent a range of values in the limits of the parent type, e.g, Nanosecond.Span ranges from -999,999,999 to 999,999,999, as 1,000,000,000 nanoseconds corresponds to one second.

Date and Time Types #

Dates and times are made up of different parts. An Ordinal is an absolute value, like a specific day in a month, while an Offset is a shift forward or backward in time, used in arithmetic operations to add or subtract days, months or years. Dates use components like Year.Ordinal, Month.Ordinal, and Day.Ordinal to ensure they represent valid points in time.

Some types, like Duration, include a Span to represent ranges over other types, such as Second.Offset. This type can have a fractional nanosecond part that can be negative or positive that is represented as a Nanosecond.Span.

Date #

These types provide precision down to the day level, useful for representing and manipulating dates.

Time #

These types offer precision down to the nanosecond level, useful for representing and manipulating time of day.

Date and time #

Combines date and time into a single representation, useful for precise timestamps and scheduling.

Zoned date and times. #

Combines date, time and time zones.

Duration #

Represents spans of time and the difference between two points in time.

Formats #

Format strings are used to convert between String representations and date/time types, like yyyy-MM-dd'T'HH:mm:ss.sssZ. The table below shows the available format specifiers. Some specifiers can be repeated to control truncation or offsets. When a character is repeated n times, it usually truncates the value to n characters.

The supported formats include:

Macros #

In order to help the user build dates easily, there are a lot of macros available for creating dates. The .sssssssss can be ommited in most cases.