Skip to content

Commit 6c60d50

Browse files
anuthanAliaksey Artamonau
authored andcommitted
Introduce fun timestamp_local_iso8601/1 which takes ...
... erlang:timestamp() type as argument. Change-Id: I98b72bbb6acf1479f631c6d68ffd424fe00e5b4b Reviewed-on: http://review.couchbase.org/106413 Tested-by: Abhijeeth Nuthan <[email protected]> Well-Formed: Build Bot <[email protected]> Reviewed-by: Aliaksey Artamonau <[email protected]>
1 parent 1640a46 commit 6c60d50

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/misc.erl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ shuffle(List) when is_list(List) ->
5555
get_days_list() ->
5656
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].
5757

58+
get_utc_offset(LocalTime, UTCTime) ->
59+
Diff = calendar:datetime_to_gregorian_seconds(LocalTime) -
60+
calendar:datetime_to_gregorian_seconds(UTCTime),
61+
{TZHours, TZMin, _} = calendar:seconds_to_time(abs(Diff)),
62+
case Diff >= 0 of
63+
true -> {TZHours, TZMin};
64+
false -> {-TZHours, TZMin}
65+
end.
66+
5867
% formats time (see erlang:localtime/0) as ISO-8601 text
5968
iso_8601_fmt({{Year,Month,Day},{Hour,Min,Sec}}, Millis, UTCOffset) ->
6069
TimeS =
@@ -80,6 +89,14 @@ iso_8601_fmt({{Year,Month,Day},{Hour,Min,Sec}}, Millis, UTCOffset) ->
8089
timestamp_utc_iso8601() ->
8190
iso_8601_fmt(erlang:universaltime(), undefined, {0, 0}).
8291

92+
%% Time is as per erlang:timestamp/0
93+
timestamp_local_iso8601(Time) ->
94+
LocalTime = calendar:now_to_local_time(Time),
95+
UTCTime = calendar:now_to_universal_time(Time),
96+
UTCOffset = get_utc_offset(LocalTime, UTCTime),
97+
Millis = erlang:element(3, Time) div 1000,
98+
iso_8601_fmt(LocalTime, Millis, UTCOffset).
99+
83100
-ifdef(EUNIT).
84101
iso_8601_fmt_test() ->
85102
?assertEqual("2007-04-05T14:30:00.040Z",

0 commit comments

Comments
 (0)