From 41ddcea5812c51ab42bdec8fc8db078010755ad6 Mon Sep 17 00:00:00 2001 From: Bip Thelin Date: Mon, 5 May 2014 11:52:41 +0200 Subject: [PATCH] support decimal seconds --- src/iso8601.erl | 6 +++++- test/iso8601_tests.erl | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/iso8601.erl b/src/iso8601.erl index a7e5774..80bd637 100644 --- a/src/iso8601.erl +++ b/src/iso8601.erl @@ -16,7 +16,7 @@ -type maybe(A) :: undefined | A. -type timestamp() :: tuple(MegaSecs::integer(), Secs::integer(), - MicroSecs::integer()). + MicroSecs::integer() | float()). %% API @@ -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]), diff --git a/test/iso8601_tests.erl b/test/iso8601_tests.erl index 209ea32..7972b65 100644 --- a/test/iso8601_tests.erl +++ b/test/iso8601_tests.erl @@ -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, @@ -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, @@ -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,