Current section
Files
Jump to
Current section
Files
src/tempo@instant.erl
-module(tempo@instant).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/tempo/instant.gleam").
-export([now/0, since/1, format_since/1, as_utc_datetime/1, as_local_datetime/1, as_unix_seconds/1, as_unix_milli/1, as_unix_micro/1, as_timestamp/1, as_utc_date/1, as_local_date/1, as_local_calendar_date/1, as_utc_calendar_date/1, as_utc_time/1, as_local_time/1, as_local_calendar_time_of_day/1, as_utc_calendar_time_of_day/1, format_utc/2, format_local/2, difference/2, compare/2, is_earlier/2, is_earlier_or_equal/2, is_equal/2, is_later/2, is_later_or_equal/2, to_unique_int/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" The `Instant` type is the most complete representation of system time. It is\n"
" a monotonic type that represents a unique point in time on the host system.\n"
" It is the only way to get the current time on the host system as a value, \n"
" and means very little on other systems. Try to keep `Instant` values as\n"
" this type as long as possible in your programs. It can be safely used for \n"
" timing tasks, sorting times, and recording times.\n"
" \n"
" If you would like to send this value outside of Gleam, it will have \n"
" to be converted to a `DateTime` value first.\n"
" \n"
" ## Timing Tasks\n"
" \n"
" ```gleam\n"
" import tempo/instant\n"
" \n"
" pub fn main() {\n"
" let monotonic_timer = instant.now()\n"
" // Do long task ...\n"
" instant.since(monotonic_timer)\n"
" // -> duration.minutes(42)\n"
" \n"
" instant.format_since(monotonic_timer)\n"
" // -> \"42 minutes\"\n"
" }\n"
" ```\n"
" \n"
" ## Sorting Times\n"
" \n"
" ```gleam\n"
" import tempo/instant\n"
" \n"
" pub fn main() {\n"
" let completed_async_tasks = // ...\n"
" \n"
" let task_by_completion_time = \n"
" list.sort(\n"
" completed_async_tasks,\n"
" fn(a, b) { instant.compare(a.completion_time, b.completion_time) },\n"
" )\n"
" }\n"
" ```\n"
" \n"
" ## Recording Times\n"
"\n"
" ```gleam\n"
" import tempo/instant\n"
" \n"
" pub type Record {\n"
" Record(name: String, created_time: instant.Instant)\n"
" }\n"
" \n"
" pub fn new_record(name) {\n"
" Record(name: name, created_time: instant.now())\n"
" }\n"
" \n"
" pub fn render_record_created_time(record: Record) {\n"
" instant.format(record.created_time, tempo.ISO8601Milli)\n"
" }\n"
" ```\n"
).
-file("src/tempo/instant.gleam", 71).
?DOC(" The current instant on the host system.\n").
-spec now() -> tempo:instant().
now() ->
tempo:now().
-file("src/tempo/instant.gleam", 84).
?DOC(
" Gets the duration between the current system time and the provided instant.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let monotonic_timer = instant.now()\n"
" // Do long task ...\n"
" instant.since(monotonic_timer)\n"
" // -> duration.minutes(42)\n"
).
-spec since(tempo:instant()) -> gleam@time@duration:duration().
since(Start) ->
tempo:instant_since(Start).
-file("src/tempo/instant.gleam", 98).
?DOC(
" Formats the duration between the current system time and the provided \n"
" instant.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let monotonic_timer = instant.now()\n"
" // Do long task ...\n"
" instant.format_since(monotonic_timer)\n"
" // -> \"42 minutes\"\n"
).
-spec format_since(tempo:instant()) -> binary().
format_since(Start) ->
tempo:instant_since_formatted(Start).
-file("src/tempo/instant.gleam", 112).
?DOC(
" Converts an instant to a UTC datetime value. Do not use this with \n"
" `datetime.difference` to time tasks!\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" instant.now()\n"
" |> instant.as_utc_datetime\n"
" // -> datetime.literal(\"2024-12-26T16:32:34Z\")\n"
" ```\n"
).
-spec as_utc_datetime(tempo:instant()) -> tempo:date_time().
as_utc_datetime(Instant) ->
tempo:instant_as_utc_datetime(Instant).
-file("src/tempo/instant.gleam", 126).
?DOC(
" Converts an instant to a local datetime value. Do not use this with \n"
" `datetime.difference` to time tasks!\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant._as_local_datetime\n"
" // -> datetime.literal(\"2024-12-26T12:32:34-04:00\")\n"
" ```\n"
).
-spec as_local_datetime(tempo:instant()) -> tempo:date_time().
as_local_datetime(Instant) ->
tempo:instant_as_local_datetime(Instant).
-file("src/tempo/instant.gleam", 131).
?DOC(false).
-spec as_unix_seconds(tempo:instant()) -> integer().
as_unix_seconds(Instant) ->
tempo:instant_as_unix_seconds(Instant).
-file("src/tempo/instant.gleam", 136).
?DOC(false).
-spec as_unix_milli(tempo:instant()) -> integer().
as_unix_milli(Instant) ->
tempo:instant_as_unix_milli(Instant).
-file("src/tempo/instant.gleam", 141).
?DOC(false).
-spec as_unix_micro(tempo:instant()) -> integer().
as_unix_micro(Instant) ->
tempo:instant_as_unix_micro(Instant).
-file("src/tempo/instant.gleam", 146).
?DOC(" Converts an instant to a `gleam_time` timestamp type.\n").
-spec as_timestamp(tempo:instant()) -> gleam@time@timestamp:timestamp().
as_timestamp(Instant) ->
Seconds = tempo:instant_as_unix_micro(Instant) div 1000000,
Nanoseconds = (tempo:instant_as_unix_micro(Instant) rem 1000000) * 1000,
gleam@time@timestamp:from_unix_seconds_and_nanoseconds(Seconds, Nanoseconds).
-file("src/tempo/instant.gleam", 162).
?DOC(
" Converts an instant to a UTC date value.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_utc_date\n"
" // -> date.literal(\"2024-12-27\")\n"
" ```\n"
).
-spec as_utc_date(tempo:instant()) -> tempo:date().
as_utc_date(Instant) ->
tempo:instant_as_utc_date(Instant).
-file("src/tempo/instant.gleam", 175).
?DOC(
" Converts an instant to a local date value.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_local_date\n"
" // -> date.literal(\"2024-12-26\")\n"
" ```\n"
).
-spec as_local_date(tempo:instant()) -> tempo:date().
as_local_date(Instant) ->
tempo:instant_as_local_date(Instant).
-file("src/tempo/instant.gleam", 188).
?DOC(
" Converts an instant to a local `gleam_time` calendar date type.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_local_calendar_date\n"
" // -> calendar.Date(2025, calendar.February, 8)\n"
" ```\n"
).
-spec as_local_calendar_date(tempo:instant()) -> gleam@time@calendar:date().
as_local_calendar_date(Instant) ->
_pipe = tempo:instant_as_local_date(Instant),
tempo@date:to_calendar_date(_pipe).
-file("src/tempo/instant.gleam", 201).
?DOC(
" Converts an instant to a UTC `gleam_time` calendar date type.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_utc_calendar_date\n"
" // -> calendar.Date(2025, calendar.February, 8)\n"
" ```\n"
).
-spec as_utc_calendar_date(tempo:instant()) -> gleam@time@calendar:date().
as_utc_calendar_date(Instant) ->
_pipe = tempo:instant_as_utc_date(Instant),
tempo@date:to_calendar_date(_pipe).
-file("src/tempo/instant.gleam", 214).
?DOC(
" Converts an instant to a UTC time value.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_utc_time\n"
" // -> time.literal(\"16:32:34\")\n"
" ```\n"
).
-spec as_utc_time(tempo:instant()) -> tempo:time().
as_utc_time(Instant) ->
tempo:instant_as_utc_time(Instant).
-file("src/tempo/instant.gleam", 227).
?DOC(
" Converts an instant to a local time value.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_local_time\n"
" // -> time.literal(\"12:32:34\")\n"
" ```\n"
).
-spec as_local_time(tempo:instant()) -> tempo:time().
as_local_time(Instant) ->
tempo:instant_as_local_time(Instant).
-file("src/tempo/instant.gleam", 240).
?DOC(
" Converts an instant to a local `gleam_time` calendar time of day type.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_local_calendar_time_of_day\n"
" // -> calendar.TimeOfDay(12, 32, 34, 0)\n"
" ```\n"
).
-spec as_local_calendar_time_of_day(tempo:instant()) -> gleam@time@calendar:time_of_day().
as_local_calendar_time_of_day(Instant) ->
_pipe = tempo:instant_as_local_time(Instant),
tempo@time:to_calendar_time_of_day(_pipe).
-file("src/tempo/instant.gleam", 255).
?DOC(
" Converts an instant to a UTC `gleam_time` calendar time of day type.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.as_utc_calendar_time_of_day\n"
" // -> calendar.TimeOfDay(12, 32, 34, 0)\n"
" ```\n"
).
-spec as_utc_calendar_time_of_day(tempo:instant()) -> gleam@time@calendar:time_of_day().
as_utc_calendar_time_of_day(Instant) ->
_pipe = tempo:instant_as_utc_time(Instant),
tempo@time:to_calendar_time_of_day(_pipe).
-file("src/tempo/instant.gleam", 268).
?DOC(
" Formats an instant as a UTC datetime value.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.format_utc(tempo.ISO8601Milli)\n"
" // -> \"2024-12-26T16:32:34.254Z\"\n"
" ```\n"
).
-spec format_utc(tempo:instant(), tempo:date_time_format()) -> binary().
format_utc(Instant, Format) ->
_pipe = tempo:instant_as_utc_datetime(Instant),
tempo@datetime:format(_pipe, Format).
-file("src/tempo/instant.gleam", 284).
?DOC(
" Formats an instant as a local datetime value.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" tempo.now()\n"
" |> instant.format_local(tempo.ISO8601Micro)\n"
" // -> \"2024-12-26T12:32:34.534223-04:00\"\n"
" ```\n"
).
-spec format_local(tempo:instant(), tempo:date_time_format()) -> binary().
format_local(Instant, Format) ->
_pipe = tempo:instant_as_local_datetime(Instant),
tempo@datetime:format(_pipe, Format).
-file("src/tempo/instant.gleam", 302).
?DOC(
" Gets the difference between two instants.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let start = tempo.now()\n"
" let end = tempo.now()\n"
" \n"
" instant.difference(from: start, to: end)\n"
" // -> duration.microseconds(1)\n"
" ```\n"
).
-spec difference(tempo:instant(), tempo:instant()) -> gleam@time@duration:duration().
difference(A, B) ->
tempo:instant_difference(A, B).
-file("src/tempo/instant.gleam", 320).
?DOC(
" Compares two instants.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let start = tempo.now()\n"
" let end = tempo.now()\n"
" \n"
" instant.compare(start, end)\n"
" // -> order.Lt\n"
" ```\n"
).
-spec compare(tempo:instant(), tempo:instant()) -> gleam@order:order().
compare(A, B) ->
tempo:instant_compare(A, B).
-file("src/tempo/instant.gleam", 335).
?DOC(
" Checks if the first instant is earlier than the second instant.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let start = tempo.now()\n"
" let end = tempo.now()\n"
" \n"
" instant.is_earlier(start, than: end)\n"
" // -> True\n"
" ```\n"
).
-spec is_earlier(tempo:instant(), tempo:instant()) -> boolean().
is_earlier(A, B) ->
tempo:instant_is_earlier(A, B).
-file("src/tempo/instant.gleam", 350).
?DOC(
" Checks if the first instant is earlier or equal to the second instant.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let start = tempo.now()\n"
" let end = tempo.now()\n"
" \n"
" instant.is_earlier_or_equal(start, to: end)\n"
" // -> True\n"
" ```\n"
).
-spec is_earlier_or_equal(tempo:instant(), tempo:instant()) -> boolean().
is_earlier_or_equal(A, B) ->
tempo:instant_is_earlier_or_equal(A, B).
-file("src/tempo/instant.gleam", 365).
?DOC(
" Checks if the first instant is equal to the second instant.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let start = tempo.now()\n"
" let end = tempo.now()\n"
" \n"
" instant.is_equal(start, to: end)\n"
" // -> False\n"
" ```\n"
).
-spec is_equal(tempo:instant(), tempo:instant()) -> boolean().
is_equal(A, B) ->
tempo:instant_is_equal(A, B).
-file("src/tempo/instant.gleam", 380).
?DOC(
" Checks if the first instant is later than the second instant.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let start = tempo.now()\n"
" let end = tempo.now()\n"
" \n"
" instant.is_later(start, than: end)\n"
" // -> False\n"
" ```\n"
).
-spec is_later(tempo:instant(), tempo:instant()) -> boolean().
is_later(A, B) ->
tempo:instant_is_later(A, B).
-file("src/tempo/instant.gleam", 395).
?DOC(
" Checks if the first instant is later or equal to the second instant.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" let start = tempo.now()\n"
" let end = tempo.now()\n"
" \n"
" instant.is_later_or_equal(start, to: end)\n"
" // -> False\n"
" ```\n"
).
-spec is_later_or_equal(tempo:instant(), tempo:instant()) -> boolean().
is_later_or_equal(A, B) ->
tempo:instant_is_later_or_equal(A, B).
-file("src/tempo/instant.gleam", 405).
?DOC(
" Gets the unique integer value associated with the provided instant.\n"
" \n"
" Intant values are each unique to a host. This function exposes an instant's\n"
" uniqueness so it can be used for other things. Note that this value is only\n"
" unique on the host system and only within the lifetime of the BEAM VM / \n"
" JavaScript context on the host.\n"
).
-spec to_unique_int(tempo:instant()) -> integer().
to_unique_int(Instant) ->
tempo:instant_get_unique(Instant).