Skip to content
This repository has been archived by the owner on Apr 30, 2018. It is now read-only.

Commit

Permalink
support decimal seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
bipthelin committed May 5, 2014
1 parent 0398913 commit 41ddcea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/iso8601.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-type maybe(A) :: undefined | A.
-type timestamp() :: tuple(MegaSecs::integer(),
Secs::integer(),
MicroSecs::integer()).
MicroSecs::integer() | float()).

%% API

Expand All @@ -31,6 +31,10 @@ add_time(Datetime, H, M, S) ->
%% returns a string with no offset (i.e., ending in "Z").
format({_,_,_}=Timestamp) ->
format(calendar:now_to_datetime(Timestamp));
format({{Y,Mo,D}, {H,Mn,S}}) when is_float(S) ->
FmtStr = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~9.6.0fZ",
IsoStr = io_lib:format(FmtStr, [Y, Mo, D, H, Mn, S]),
list_to_binary(IsoStr);
format({{Y,Mo,D}, {H,Mn,S}}) ->
FmtStr = "~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ",
IsoStr = io_lib:format(FmtStr, [Y, Mo, D, H, Mn, S]),
Expand Down
6 changes: 3 additions & 3 deletions test/iso8601_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ parse_hour_test_() ->
[{"parses YYYYMMDDTHH",
?_assertMatch({{2012,2,3},{4,0,0}}, F("20120203T04"))},
{"parses YYYY-MM-DDTHH",
?_assertMatch({{2012,2,3},{4,0,0}}, F("2012-02-03T04"))}].
?_assertMatch({{2012,2,3},{4,0,0}}, F("2012-02-03T04"))}].

parse_fractional_hour_test_() ->
F = fun iso8601:parse/1,
Expand All @@ -72,7 +72,7 @@ parse_minute_test_() ->
[{"parses YYYYMMDDTHHMM",
?_assertMatch({{2012,2,3},{4,5,0}}, F("20120203T0405"))},
{"parses YYYY-MM-DDTHH:MM",
?_assertMatch({{2012,2,3},{4,5,0}}, F("2012-02-03T04:05"))}].
?_assertMatch({{2012,2,3},{4,5,0}}, F("2012-02-03T04:05"))}].

parse_fractional_minute_test_() ->
F = fun iso8601:parse/1,
Expand All @@ -90,7 +90,7 @@ parse_second_test_() ->
[{"parses YYYYMMDDTHHMMSS",
?_assertMatch({{2012,2,3},{4,5,6}}, F("20120203T040506"))},
{"parses YYYY-MM-DDTHH:MM:SS",
?_assertMatch({{2012,2,3},{4,5,6}}, F("2012-02-03T04:05:06"))}].
?_assertMatch({{2012,2,3},{4,5,6}}, F("2012-02-03T04:05:06"))}].

parse_fractional_second_test_() ->
F = fun iso8601:parse/1,
Expand Down

0 comments on commit 41ddcea

Please sign in to comment.