Permalink
Browse files

Validate the offset values as well, provide a spec.

  • Loading branch information...
jlouis committed Oct 20, 2010
1 parent 7c08ec4 commit 911bb2da5a8b44d4d564800b7bbca6185d8bcd8e
Showing with 25 additions and 7 deletions.
  1. +25 −7 src/inet_time.erl
View
@@ -13,15 +13,20 @@
%%====================================================================
%% API
%%====================================================================
+-type date() :: {integer(), integer(), integer()}.
+-type time() :: {integer(), integer(), integer(), integer()}.
+-type offset() :: {'+' | '-', integer(), integer()}.
+
+-spec parse(string()) -> {date(), time(), offset()}.
parse(String) ->
{ok, Tokens, _} = inet_time_scanner:string(String),
{ok, {Date, {Time, Offset}}} = inet_time_parser_simple:parse(Tokens),
case calendar:valid_date(Date) of
true ->
- case valid_time(Time) of
- true -> {ok, {Date, Time, Offset}};
- false -> {error, {invalid_time, {Date, Time, Offset}}};
- leap_second -> {error, no_leap_second_handling}
+ case valid_time_offset(Time, Offset) of
+ ok -> {ok, {Date, Time, Offset}};
+ {error, Reason} ->
+ {error, {Reason, {Date, Time, Offset}}}
end;
false ->
{error, {invalid_date, {Date, Time, Offset}}}
@@ -30,6 +35,19 @@ parse(String) ->
%%====================================================================
%% Internal functions
%%====================================================================
+valid_time_offset(Time, Offset) ->
+ case valid_time(Time) of
+ ok -> valid_offset(Offset);
+ {error, Reason} -> {error, Reason}
+ end.
+
+valid_offset({_, H, M}) when
+ 0 =< H,
+ H =< 23,
+ 0 =< M,
+ M =< 59 -> ok;
+valid_offset(_) -> {error, invalid_offset}.
+
valid_time({Hour, Minute, Second, _Frac})
when 0 =< Hour,
Hour =< 23,
@@ -38,10 +56,10 @@ valid_time({Hour, Minute, Second, _Frac})
0 =< Second,
Second =< 60 ->
case Second of
- 60 -> leap_second;
- _ -> true
+ 60 -> {error, no_leap_second_handling};
+ _ -> ok
end;
-valid_time(_Otherwise) -> false.
+valid_time(_Otherwise) -> {error, invalid}.

0 comments on commit 911bb2d

Please sign in to comment.