Skip to content
heff

std.localdatetime

A date and a time of day together, with no zone: 2024-02-29T15:04:05 on a calendar and a clock on the wall, which is still no instant, as it comes at a different one in each place — an offset from UTC (std.offset) makes it one, a std.offsetdatetime. NodaTime’s LocalDateTime is the model: a std.localdate and a std.localtime, ordered by the date, then the time. std.period’s addToDateTime moves one by years, months and hours alike. Import it by a name, import std.localdatetime as LocalDateTime, as its names are short.

import std.localdatetime as LocalDateTime
import std.localtime as LocalTime
-- | the start of the day of the date-time
-- | >>> maybe "" (\t -> show (startOfDay t)) (LocalDateTime.parse "2024-02-29T15:04:05")
-- | "2024-02-29T00:00:00"
startOfDay : LocalDateTime.LocalDateTime -> LocalDateTime.LocalDateTime
startOfDay t = LocalDateTime.make (LocalDateTime.date t) LocalTime.midnight

LocalDateTime

data LocalDateTime

A date-time: its date and its time of day.

make

make : LocalDate.LocalDate -> LocalTime.LocalTime -> LocalDateTime

The date at the time of day.

date

date : LocalDateTime -> LocalDate.LocalDate

Its date.

time

time : LocalDateTime -> LocalTime.LocalTime

Its time of day.

instanceEqualityLocalDateTime

instance instanceEqualityLocalDateTime : Equality LocalDateTime

The same date and the same time.

instanceOrderLocalDateTime

instance instanceOrderLocalDateTime : Order LocalDateTime

The earlier first: by the date, then the time.

instanceDebugLocalDateTime

instance instanceDebugLocalDateTime : Debug LocalDateTime

As show writes it, 2024-02-29T15:04:05, as NodaTime’s and Rust’s chrono’s debug form is.

instanceShowLocalDateTime

instance instanceShowLocalDateTime : Show LocalDateTime

As ISO 8601 writes it: the date as std.localdate writes it, a T, the time as std.localtime writes it (2024-02-29T15:04:05, -0001-12-31T23:59:59.5).

>>> make (LocalDate.fromEpochDays (-719529)) (fromMaybe LocalTime.noon (LocalTime.fromNanosecondOfDay 86399500000000))
MkLocalDateTime (MkLocalDate (-1) 12 31) (MkLocalTime 86399500000000)
>>> show (make (LocalDate.fromEpochDays (-719529)) (fromMaybe LocalTime.noon (LocalTime.fromNanosecondOfDay 86399500000000)))
"-0001-12-31T23:59:59.5"

parse

parse : String -> Maybe LocalDateTime

The date-time ISO 8601 writes as s, extended: a date as std.localdate’s parse reads it, a T (or a t), and a time as std.localtime’s reads it (2024-02-29T15:04:05, 2024-02-29T15:04:05.25). Nothing if it writes none, or a field is out of its range; an offset or a Z after it is not read here, but by std.offsetdatetime.

>>> List.map (\s -> maybe "" show (parse s)) ["2024-02-29T15:04:05", "2024-02-29t15:04:05.250", "0000-01-01T00:00:00.000000001"]
["2024-02-29T15:04:05", "2024-02-29T15:04:05.25", "0000-01-01T00:00:00.000000001"]
>>> (List.map (\s -> maybe True (\_ -> False) (parse s)) ["2024-02-29 15:04:05", "2024-02-30T15:04:05", "2024-02-29T24:00:00", "2024-02-29T15:04:05Z", "2024-02-29T15:04", "2024-02-29", "2024-02-29T"] : List Bool)
[True, True, True, True, True, True, True]
>>> List.map (\p -> compare (fst p) (snd p)) [(parse "2024-02-29T23:00:00", parse "2024-03-01T01:00:00"), (parse "2024-02-29T15:04:05.5", parse "2024-02-29T15:04:05")]
[LT, GT]