Permalink
Browse files

encode timestamps in erlang:now() format

  • Loading branch information...
1 parent 6dd2d26 commit acc0171c4991f05516c40af38c05b56924d40ddd @mabrek committed Nov 26, 2011
Showing with 6 additions and 0 deletions.
  1. +6 −0 src/pgsql_idatetime.erl
View
6 src/pgsql_idatetime.erl
@@ -8,6 +8,7 @@
-define(int64, 1/big-signed-unit:64).
-define(postgres_epoc_jdate, 2451545).
+-define(postgres_epoc_usecs, 946674000000000).
-define(mins_per_hour, 60).
-define(secs_per_minute, 60).
@@ -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(time, T) -> <<8:?int32, (time2i(T)):?int64>>;
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(timestamptz, TS = {_, _, _}) -> <<8:?int32, (now2i(TS)):?int64>>;
encode(timestamptz, TS) -> <<8:?int32, (timestamp2i(TS)):?int64>>;
encode(interval, {T, D, M}) -> <<16:?int32, (time2i(T)):?int64, D:?int32, M:?int32>>.
@@ -89,6 +92,9 @@ timestamp2i({Date, Time}) ->
D = date2j(Date) - ?postgres_epoc_jdate,
D * ?usecs_per_day + time2i(Time).
+now2i({MegaSecs, Secs, MicroSecs}) ->
+ (MegaSecs * 1000000 + Secs) * 1000000 + MicroSecs - ?postgres_epoc_usecs.
+
tmodulo(T, U) ->
case T div U of
0 -> {T, 0};

0 comments on commit acc0171

Please sign in to comment.