Skip to content

Commit 16dfbdf

Browse files
anuthanAliaksey Artamonau
authored andcommitted
Change fun misc:iso_8601_fmt to accept milliseconds.
Change-Id: I7c476be1754da8bb6f7e046fc28d9afa239da1bd Reviewed-on: http://review.couchbase.org/105565 Tested-by: Abhijeeth Nuthan <[email protected]> Well-Formed: Build Bot <[email protected]> Reviewed-by: Aliaksey Artamonau <[email protected]>
1 parent 457752e commit 16dfbdf

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/misc.erl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ get_days_list() ->
5656
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"].
5757

5858
% formats time (see erlang:localtime/0) as ISO-8601 text
59-
iso_8601_fmt(Time) -> iso_8601_fmt(Time, undefined).
60-
iso_8601_fmt({{Year,Month,Day},{Hour,Min,Sec}}, UTCOffset) ->
59+
iso_8601_fmt({{Year,Month,Day},{Hour,Min,Sec}}, Millis, UTCOffset) ->
6160
TimeS =
6261
io_lib:format("~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B",
6362
[Year, Month, Day, Hour, Min, Sec]),
63+
MilliSecond = case Millis of
64+
undefined -> "";
65+
_ -> io_lib:format(".~3.10.0B", [Millis])
66+
end,
6467
OffsetS =
6568
case UTCOffset of
6669
undefined -> "";
@@ -72,23 +75,25 @@ iso_8601_fmt({{Year,Month,Day},{Hour,Min,Sec}}, UTCOffset) ->
7275
end,
7376
io_lib:format("~s~2.10.0B:~2.10.0B", [Sign, abs(H), M])
7477
end,
75-
lists:flatten(TimeS ++ OffsetS).
78+
lists:flatten(TimeS ++ MilliSecond ++ OffsetS).
7679

7780
timestamp_utc_iso8601() ->
78-
iso_8601_fmt(erlang:universaltime(), {0, 0}).
81+
iso_8601_fmt(erlang:universaltime(), undefined, {0, 0}).
7982

8083
-ifdef(EUNIT).
8184
iso_8601_fmt_test() ->
85+
?assertEqual("2007-04-05T14:30:00.040Z",
86+
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, 40, {0, 0})),
8287
?assertEqual("2007-04-05T14:30:00Z",
83-
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, {0, 0})),
88+
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, undefined, {0, 0})),
8489
?assertEqual("2007-04-05T14:30:00",
85-
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}})),
90+
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, undefined, undefined)),
8691
?assertEqual("2007-04-05T14:30:00+01:00",
87-
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, {1,0})),
92+
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, undefined, {1,0})),
8893
?assertEqual("2007-04-05T14:30:00-03:45",
89-
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, {-3,45})),
94+
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, undefined, {-3,45})),
9095
?assertEqual("2007-04-05T14:30:00-23:45",
91-
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, {-23,45})).
96+
iso_8601_fmt({{2007, 4, 5}, {14, 30, 00}}, undefined, {-23,45})).
9297
-endif.
9398

9499
%% applies (catch Fun(X)) for each element of list in parallel. If

0 commit comments

Comments
 (0)