Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/ivanos/erlang_iso8601 into …
Browse files Browse the repository at this point in the history
…ivanos-master

Conflicts:
	src/iso8601.erl
  • Loading branch information
oubiwann committed Sep 22, 2016
2 parents 00fd0df + b6f1c09 commit a5a9fdb
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/iso8601.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
parse/1,
parse_exact/1]).

-export_types([datetime/0,
timestamp/0]).
-export_types([timestamp/0]).

-define(MIDNIGHT, {0,0,0}).
-define(V, proplists:get_value).

-type datetime() :: {Date::calendar:date(),
Time::calendar:time()}.
-type datetime_plist() :: list({atom(), integer()}).
-type maybe(A) :: undefined | A.
-type timestamp() :: {MegaSecs::integer(),
Expand All @@ -21,12 +18,13 @@

%% API

-spec add_time (datetime(), integer(), integer(), integer()) -> datetime().
%% @doc Add some time to the supplied `datetime()'.
-spec add_time (calendar:datetime(), integer(), integer(), integer())
-> calendar:datetime().
%% @doc Add some time to the supplied `calendar:datetime()'.
add_time(Datetime, H, M, S) ->
apply_offset(Datetime, H, M, S).

-spec format (datetime() | timestamp()) -> binary().
-spec format (calendar:datetime() | timestamp()) -> binary().
%% @doc Convert a `util:timestamp()' or a calendar-style `{date(), time()}'
%% tuple to an ISO 8601 formatted string. Note that this function always
%% returns a string with no offset (i.e., ending in "Z").
Expand All @@ -41,15 +39,15 @@ format({{Y,Mo,D}, {H,Mn,S}}) ->
IsoStr = io_lib:format(FmtStr, [Y, Mo, D, H, Mn, S]),
list_to_binary(IsoStr).

-spec parse (string()) -> datetime().
%% @doc Convert an ISO 8601 formatted string to a `{date(), time()}' tuple
-spec parse (string()) -> calendar:datetime().
%% @doc Convert an ISO 8601 formatted string to a
parse(Bin) when is_binary(Bin) ->
parse(binary_to_list(Bin));
parse(Str) ->
{{Date, {H, M, S}}, Subsecond} = year(Str, []),
{Date, {H, M, S + round(Subsecond)}}.

-spec parse_exact (string()) -> datetime().
-spec parse_exact (string()) -> calendar:datetime().
%% @doc Convert an ISO 8601 formatted string to a `{date(), time()}'
%% tuple with seconds precision to 3 decimal palces
parse_exact(Bin) when is_binary(Bin) ->
Expand Down Expand Up @@ -264,7 +262,8 @@ date_at_w01_1(Year) ->
7 -> {Year, 1, 2}
end.

-spec apply_offset (datetime(), number(), number(), number()) -> datetime().
-spec apply_offset (calendar:datetime(), number(), number(), number())
-> calendar:datetime().
%% @doc Add the specified number of hours, minutes and seconds to `Datetime'.
apply_offset(Datetime, H, M, S) ->
OffsetS = S + (60 * (M + (60 * H))),
Expand Down

0 comments on commit a5a9fdb

Please sign in to comment.