Current section
Files
Jump to
Current section
Files
src/tempo@offset.erl
-module(tempo@offset).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([local/0, new/1, to_string/1, from_string/1, literal/1, from_duration/1, to_duration/1, describe_parse_error/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(
" Functions to use with the `Offset` type in Tempo. The offset values \n"
" represents the time difference between the current time and UTC time.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" import tempo/offset\n"
" \n"
" pub fn get_system_offset() {\n"
" offset.local()\n"
" |> offset.to_string\n"
" // -> \"+05:00\"\n"
" }\n"
" ```\n"
).
-file("src/tempo/offset.gleam", 32).
?DOC(
" Returns the local offset of the host.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" offset.local()\n"
" |> offset.to_string\n"
" // -> \"+05:00\"\n"
" ```\n"
).
-spec local() -> tempo:offset().
local() ->
_pipe = tempo_ffi:local_offset(),
tempo:offset(_pipe).
-file("src/tempo/offset.gleam", 37).
-spec new(gleam@time@duration:duration()) -> {ok, tempo:offset()} | {error, nil}.
new(Duration) ->
tempo:new_offset(Duration).
-file("src/tempo/offset.gleam", 78).
?DOC(
" Converts an offset to a string representation.\n"
" \n"
" Will not return \"Z\" for a zero offset because it is probably not what\n"
" the user wants without the context of a full datetime. Datetime modules\n"
" building on this should cover formatting for Z themselves.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" offset.literal(\"-00\")\n"
" |> offset.to_string\n"
" // -> \"-00:00\"\n"
" ```\n"
).
-spec to_string(tempo:offset()) -> binary().
to_string(Offset) ->
tempo:offset_to_string(Offset).
-file("src/tempo/offset.gleam", 92).
?DOC(
" Tries to create a new offset from a string. Accepted formats are \n"
" `(+-)hh:mm`, `(+-)hhmm`, `(+-)hh`, and `(+-)h`.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" offset.from_string(\"-04\")\n"
" |> result.map(offset.to_string)\n"
" // -> Ok(\"-04:00\")\n"
" ```\n"
).
-spec from_string(binary()) -> {ok, tempo:offset()} |
{error, tempo@error:offset_parse_error()}.
from_string(Offset) ->
tempo:offset_from_string(Offset).
-file("src/tempo/offset.gleam", 55).
?DOC(
" Creates a new offset from a string literal, but will panic if the string\n"
" is invalid. Accepted formats are `(+-)hh:mm`, `(+-)hhmm`, `(+-)hh`, and\n"
" `(+-)h`.\n"
" \n"
" Useful for declaring offset literals that you know are valid within your \n"
" program.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" offset.literal(\"-04:00\")\n"
" |> offset.to_string\n"
" // -> \"-04:00\"\n"
" ```\n"
).
-spec literal(binary()) -> tempo:offset().
literal(Offset) ->
case from_string(Offset) of
{ok, Offset@1} ->
Offset@1;
{error, {offset_invalid_format, _}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid offset literal format"/utf8>>,
module => <<"tempo/offset"/utf8>>,
function => <<"literal"/utf8>>,
line => 59});
{error, {offset_out_of_bounds, _}} ->
erlang:error(#{gleam_error => panic,
message => <<"Invalid offset literal value"/utf8>>,
module => <<"tempo/offset"/utf8>>,
function => <<"literal"/utf8>>,
line => 61})
end.
-file("src/tempo/offset.gleam", 113).
?DOC(
" Creates a new validated offset from a duration. Offsets are most commonly\n"
" expressed as a number of minutes or hours.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" offset.new(duration.minutes(-65))\n"
" |> result.map(offset.to_string)\n"
" // -> Ok(\"-01:05\")\n"
" ```\n"
" \n"
" ```gleam\n"
" offset.new(duration.hours(36))\n"
" // -> Error(Nil)\n"
" ```\n"
).
-spec from_duration(gleam@time@duration:duration()) -> {ok, tempo:offset()} |
{error, nil}.
from_duration(Duration) ->
tempo:new_offset(Duration).
-file("src/tempo/offset.gleam", 126).
?DOC(
" Converts an offset to a duration.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" offset.literal(\"-04:00\")\n"
" |> offset.to_duration\n"
" // -> duration.hours(4)\n"
" ```\n"
).
-spec to_duration(tempo:offset()) -> gleam@time@duration:duration().
to_duration(Offset) ->
tempo:offset_to_duration(Offset).
-file("src/tempo/offset.gleam", 139).
?DOC(
" Converts an offset parse error to a human readable error message.\n"
" \n"
" ## Example\n"
" \n"
" ```gleam\n"
" offset.from_string(\"bad offset\")\n"
" |> snag.map_error(with: offset.describe_parse_error)\n"
" // -> snag.error(\"Invalid offset format: \"bad offset\"\")\n"
" ```\n"
).
-spec describe_parse_error(tempo@error:offset_parse_error()) -> binary().
describe_parse_error(Error) ->
tempo@error:describe_offset_parse_error(Error).