Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set timestamp and timestamptz parameters in erlang:now() format #12

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README
Expand Up @@ -106,6 +106,8 @@ Erlang PostgreSQL Database Client


record = {int2, time, text, ...} (decode only) record = {int2, time, text, ...} (decode only)


timestamp and timestamptz parameters can take erlang:now() format {MegaSeconds, Seconds, MicroSeconds}

* Errors * Errors


Errors originating from the PostgreSQL backend are returned as {error, #error{}}, Errors originating from the PostgreSQL backend are returned as {error, #error{}},
Expand Down
6 changes: 6 additions & 0 deletions src/pgsql_fdatetime.erl
Expand Up @@ -7,6 +7,7 @@
-define(int32, 1/big-signed-unit:32). -define(int32, 1/big-signed-unit:32).


-define(postgres_epoc_jdate, 2451545). -define(postgres_epoc_jdate, 2451545).
-define(postgres_epoc_secs, 946684800).


-define(mins_per_hour, 60). -define(mins_per_hour, 60).
-define(secs_per_day, 86400.0). -define(secs_per_day, 86400.0).
Expand All @@ -23,7 +24,9 @@ decode(interval, <<N:1/big-float-unit:64, D:?int32, M:?int32>>) -> {f2time(N), D
encode(date, D) -> <<4:?int32, (date2j(D) - ?postgres_epoc_jdate):1/big-signed-unit:32>>; encode(date, D) -> <<4:?int32, (date2j(D) - ?postgres_epoc_jdate):1/big-signed-unit:32>>;
encode(time, T) -> <<8:?int32, (time2f(T)):1/big-float-unit:64>>; encode(time, T) -> <<8:?int32, (time2f(T)):1/big-float-unit:64>>;
encode(timetz, {T, TZ}) -> <<12:?int32, (time2f(T)):1/big-float-unit:64, TZ:?int32>>; encode(timetz, {T, TZ}) -> <<12:?int32, (time2f(T)):1/big-float-unit:64, TZ:?int32>>;
encode(timestamp, TS = {_, _, _}) -> <<8:?int32, (now2f(TS)):1/big-float-unit:64>>;
encode(timestamp, TS) -> <<8:?int32, (timestamp2f(TS)):1/big-float-unit:64>>; encode(timestamp, TS) -> <<8:?int32, (timestamp2f(TS)):1/big-float-unit:64>>;
encode(timestamptz, TS = {_, _, _}) -> <<8:?int32, (now2f(TS)):1/big-float-unit:64>>;
encode(timestamptz, TS) -> <<8:?int32, (timestamp2f(TS)):1/big-float-unit:64>>; encode(timestamptz, TS) -> <<8:?int32, (timestamp2f(TS)):1/big-float-unit:64>>;
encode(interval, {T, D, M}) -> <<16:?int32, (time2f(T)):1/big-float-unit:64, D:?int32, M:?int32>>. encode(interval, {T, D, M}) -> <<16:?int32, (time2f(T)):1/big-float-unit:64, D:?int32, M:?int32>>.


Expand Down Expand Up @@ -94,6 +97,9 @@ timestamp2f({Date, Time}) ->
D = date2j(Date) - ?postgres_epoc_jdate, D = date2j(Date) - ?postgres_epoc_jdate,
D * ?secs_per_day + time2f(Time). D * ?secs_per_day + time2f(Time).


now2f({MegaSecs, Secs, MicroSecs}) ->
MegaSecs * 1000000 + Secs + MicroSecs / 1000000.0 - ?postgres_epoc_secs.

tmodulo(T, U) -> tmodulo(T, U) ->
case T < 0 of case T < 0 of
true -> Q = ceiling(T / U); true -> Q = ceiling(T / U);
Expand Down
6 changes: 6 additions & 0 deletions src/pgsql_idatetime.erl
Expand Up @@ -8,6 +8,7 @@
-define(int64, 1/big-signed-unit:64). -define(int64, 1/big-signed-unit:64).


-define(postgres_epoc_jdate, 2451545). -define(postgres_epoc_jdate, 2451545).
-define(postgres_epoc_usecs, 946684800000000).


-define(mins_per_hour, 60). -define(mins_per_hour, 60).
-define(secs_per_minute, 60). -define(secs_per_minute, 60).
Expand All @@ -27,7 +28,9 @@ decode(interval, <<N:?int64, D:?int32, M:?int32>>) -> {i2time(N), D, M}.
encode(date, D) -> <<4:?int32, (date2j(D) - ?postgres_epoc_jdate):?int32>>; encode(date, D) -> <<4:?int32, (date2j(D) - ?postgres_epoc_jdate):?int32>>;
encode(time, T) -> <<8:?int32, (time2i(T)):?int64>>; encode(time, T) -> <<8:?int32, (time2i(T)):?int64>>;
encode(timetz, {T, TZ}) -> <<12:?int32, (time2i(T)):?int64, TZ:?int32>>; encode(timetz, {T, TZ}) -> <<12:?int32, (time2i(T)):?int64, TZ:?int32>>;
encode(timestamp, TS = {_, _, _}) -> <<8:?int32, (now2i(TS)):?int64>>;
encode(timestamp, TS) -> <<8:?int32, (timestamp2i(TS)):?int64>>; encode(timestamp, TS) -> <<8:?int32, (timestamp2i(TS)):?int64>>;
encode(timestamptz, TS = {_, _, _}) -> <<8:?int32, (now2i(TS)):?int64>>;
encode(timestamptz, TS) -> <<8:?int32, (timestamp2i(TS)):?int64>>; encode(timestamptz, TS) -> <<8:?int32, (timestamp2i(TS)):?int64>>;
encode(interval, {T, D, M}) -> <<16:?int32, (time2i(T)):?int64, D:?int32, M:?int32>>. encode(interval, {T, D, M}) -> <<16:?int32, (time2i(T)):?int64, D:?int32, M:?int32>>.


Expand Down Expand Up @@ -89,6 +92,9 @@ timestamp2i({Date, Time}) ->
D = date2j(Date) - ?postgres_epoc_jdate, D = date2j(Date) - ?postgres_epoc_jdate,
D * ?usecs_per_day + time2i(Time). D * ?usecs_per_day + time2i(Time).


now2i({MegaSecs, Secs, MicroSecs}) ->
(MegaSecs * 1000000 + Secs) * 1000000 + MicroSecs - ?postgres_epoc_usecs.

tmodulo(T, U) -> tmodulo(T, U) ->
case T div U of case T div U of
0 -> {T, 0}; 0 -> {T, 0};
Expand Down
7 changes: 6 additions & 1 deletion test_src/pgsql_tests.erl
Expand Up @@ -415,7 +415,8 @@ date_time_type_test() ->
check_type(timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60}, check_type(timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
[{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]), [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
check_type(timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}}, check_type(timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
[{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}]), [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}, {1322,334285,440966}]),
check_type(timestamptz, "'2011-01-02 03:04:05+3'", {{2011, 1, 2}, {0, 4, 5.0}}, [{1324,875970,286983}]),
check_type(interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0}, check_type(interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
[{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}]) [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
end). end).
Expand Down Expand Up @@ -647,6 +648,10 @@ check_type(Type, In, Out, Values, Column) ->
compare(_Type, null, null) -> true; compare(_Type, null, null) -> true;
compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001; compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001; compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
compare(Type, V1 = {_, _, MS}, {D2, {H2, M2, S2}}) when Type == timestamp;
Type == timestamptz ->
{D1, {H1, M1, S1}} = calendar:now_to_universal_time(V1),
({D1, H1, M1} =:= {D2, H2, M2}) and (abs(S1 + MS/1000000 - S2) < 0.000000000000001);
compare(_Type, V1, V2) -> V1 =:= V2. compare(_Type, V1, V2) -> V1 =:= V2.


%% flush mailbox %% flush mailbox
Expand Down