Browse files

support binding timestamps in now() format for floating point timestamps

  • Loading branch information...
1 parent 8c75cd8 commit ede5e297ab9664cea27d83d0009ff6b4cdd3de6d @mabrek committed Dec 26, 2011
Showing with 6 additions and 0 deletions.
  1. +6 −0 src/pgsql_fdatetime.erl
View
6 src/pgsql_fdatetime.erl
@@ -7,6 +7,7 @@
-define(int32, 1/big-signed-unit:32).
-define(postgres_epoc_jdate, 2451545).
+-define(postgres_epoc_secs, 946684800).
-define(mins_per_hour, 60).
-define(secs_per_day, 86400.0).
@@ -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(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(timestamp, TS = {_, _, _}) -> <<8:?int32, (now2f(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(interval, {T, D, M}) -> <<16:?int32, (time2f(T)):1/big-float-unit:64, D:?int32, M:?int32>>.
@@ -94,6 +97,9 @@ timestamp2f({Date, Time}) ->
D = date2j(Date) - ?postgres_epoc_jdate,
D * ?secs_per_day + time2f(Time).
+now2f({MegaSecs, Secs, MicroSecs}) ->
+ MegaSecs * 1000000 + Secs + MicroSecs / 1000000.0 - ?postgres_epoc_secs.
+
tmodulo(T, U) ->
case T < 0 of
true -> Q = ceiling(T / U);

0 comments on commit ede5e29

Please sign in to comment.