Skip to content
heff

std.localtime

Times of day, to the nanosecond, with no date and no zone: a time on a clock on the wall, 15:04:05, which is no instant, as it comes every day, at a different one in each place. NodaTime’s LocalTime is the model. A day here has 24 hours of 60 minutes of 60 seconds — no leap second — from midnight, 00:00:00, to 23:59:59.999999999. A date (std.localdate) and a time of day make a std.localdatetime; a length of time is std.duration’s, and std.period’s addToTime moves a time round the clock. Import it by a name, import std.localtime as LocalTime, as its names are short.

import std.localtime as LocalTime
-- | whether the time is in the afternoon or the evening
-- | >>> maybe False afterNoon (LocalTime.parse "15:04:05")
-- | True
afterNoon : LocalTime.LocalTime -> Bool
afterNoon t = t >= LocalTime.noon

LocalTime

data LocalTime

A time of day: the nanoseconds from midnight to it, 0 to 86399999999999.

make

make : Int -> Int -> Int -> Maybe LocalTime

The time of the hour 0 to 23, the minute 0 to 59 and the second 0 to 59, on the second; nothing if one is out of its range.

>>> make 15 4 5
Just (MkLocalTime 54245000000000)
>>> (List.map (\t -> maybe True (\_ -> False) t) [make 24 0 0, make 0 60 0, make 0 0 60, make (-1) 0 0, make 0 (-1) 0] : List Bool)
[True, True, True, True, True]

hour

hour : LocalTime -> Int

Its hour, 0 to 23.

minute

minute : LocalTime -> Int

Its minute of the hour, 0 to 59.

second

second : LocalTime -> Int

Its second of the minute, 0 to 59.

nanosecond

nanosecond : LocalTime -> Int

Its nanosecond of the second, 0 to 999999999.

nanosecondOfDay

nanosecondOfDay : LocalTime -> Int

The nanoseconds from midnight to it, 0 to 86399999999999.

withNanosecond

withNanosecond : LocalTime -> Int -> Maybe LocalTime

The same time with its nanosecond of the second n, 0 to 999999999; nothing for one out of that range.

>>> maybe Nothing (\t -> withNanosecond t 500000000) (make 15 4 5)
Just (MkLocalTime 54245500000000)
>>> maybe Nothing (\t -> withNanosecond t 1000000000) (make 15 4 5)
Nothing

midnight

midnight : LocalTime

00:00:00, the start of the day.

noon

noon : LocalTime

12:00:00.

fromNanosecondOfDay

fromNanosecondOfDay : Int -> Maybe LocalTime

The time n nanoseconds after midnight, if it is in the day, 0 to 86399999999999; nothing if not.

>>> List.map fromNanosecondOfDay [0, 86399999999999, 86400000000000, -1]
[Just (MkLocalTime 0), Just (MkLocalTime 86399999999999), Nothing, Nothing]

instanceEqualityLocalTime

instance instanceEqualityLocalTime : Equality LocalTime

The same time.

instanceOrderLocalTime

instance instanceOrderLocalTime : Order LocalTime

The earlier in the day first.

>>> midnight < noon && Just noon < make 12 0 1
True

instanceDebugLocalTime

instance instanceDebugLocalTime : Debug LocalTime

As show writes it, 15:04:05, as NodaTime’s and Rust’s chrono’s debug form is.

>>> debug noon
"12:00:00"

instanceShowLocalTime

instance instanceShowLocalTime : Show LocalTime

As ISO 8601 writes it, 15:04:05, and a fraction of the second only where there is one, without the 0s it ends with, as Go’s RFC3339Nano writes it.

>>> List.map (\n -> maybe "" show (fromNanosecondOfDay n)) [0, 54245000000000, 54245500000000, 54245000000001, 86399999999999]
["00:00:00", "15:04:05", "15:04:05.5", "15:04:05.000000001", "23:59:59.999999999"]

parse

parse : String -> Maybe LocalTime

The time ISO 8601 writes as s, extended: 15:04:05, each field in 2 digits, and perhaps a fraction of the second after a ., of 1 to 9 digits (15:04:05.1). Nothing if it writes none, or a field is out of its range (the hour 24, the second 60: no leap second).

>>> List.map (\s -> maybe "" show (parse s)) ["15:04:05", "15:04:05.1", "00:00:00.000000001", "23:59:59.999999999", "12:00:00.500"]
["15:04:05", "15:04:05.1", "00:00:00.000000001", "23:59:59.999999999", "12:00:00.5"]
>>> (List.map (\s -> maybe True (\_ -> False) (parse s)) ["24:00:00", "12:60:00", "12:00:60", "12:00", "1:00:00", "12:00:00.", "12:00:00.1234567890", "12:00:00Z", "12-00-00", "12:00:00.1a", ""] : List Bool)
[True, True, True, True, True, True, True, True, True, True, True]
>>> List.all (\n -> maybe False (\t -> parse (show t) == Just t) (fromNanosecondOfDay n)) [0, 1, 54245500000000, 86399999999999]
True