Current section

Files

Jump to
postgleam src postgleam@codec@timestamp.erl
Raw

src/postgleam@codec@timestamp.erl

-module(postgleam@codec@timestamp).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/codec/timestamp.gleam").
-export([encode/1, decode/1, matcher/0]).
-file("src/postgleam/codec/timestamp.gleam", 25).
-spec encode(postgleam@value:value()) -> {ok, bitstring()} | {error, binary()}.
encode(Val) ->
case Val of
{timestamp, Usec} ->
{ok, <<Usec:64/big>>};
pos_infinity ->
{ok, <<16#7F, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF>>};
neg_infinity ->
{ok, <<16#80, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>>};
_ ->
{error, <<"timestamp codec: expected Timestamp value"/utf8>>}
end.
-file("src/postgleam/codec/timestamp.gleam", 34).
-spec decode(bitstring()) -> {ok, postgleam@value:value()} | {error, binary()}.
decode(Data) ->
case Data of
<<16#7F, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF, 16#FF>> ->
{ok, pos_infinity};
<<16#80, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00>> ->
{ok, neg_infinity};
<<Usec:64/big-signed>> ->
{ok, {timestamp, Usec}};
_ ->
{error, <<"timestamp codec: expected 8 bytes"/utf8>>}
end.
-file("src/postgleam/codec/timestamp.gleam", 21).
-spec build(integer()) -> postgleam@codec:codec().
build(Type_oid) ->
{codec, <<"timestamp"/utf8>>, Type_oid, binary, fun encode/1, fun decode/1}.
-file("src/postgleam/codec/timestamp.gleam", 11).
-spec matcher() -> postgleam@codec:codec_matcher().
matcher() ->
{codec_matcher, <<"timestamp"/utf8>>, [1114], none, binary, fun build/1}.