Prefer underscores in timezone names, but accept spaces.

This commit is contained in:
David Hull 2014-01-09 16:46:15 -08:00
parent 4ad81b03fd
commit 6d4985d5f2
2 changed files with 81 additions and 50 deletions

View file

@ -220,10 +220,41 @@ fmt_shift({'-', H, M}) ->
fmt_shift(Any) ->
throw(Any).
tr_char(String, From, To) ->
case string:chr(String, From) of
0 -> String; % Optimize for String does not contain From.
_ -> tr_char(String, From, To, [])
end.
tr_char([], _From, _To, Acc) ->
lists:reverse(Acc);
tr_char([H|T], From, To, Acc) ->
case H of
From -> tr_char(T, From, To, [To|Acc]);
_ -> tr_char(T, From, To, [H|Acc])
end.
-define(SPACE_CHAR, 32).
get_timezone(TimeZone) ->
case dict:find(TimeZone, ?tz_index) of
TimeZoneNoSpaces = tr_char(TimeZone, ?SPACE_CHAR, $_),
case dict:find(TimeZoneNoSpaces, ?tz_index) of
error ->
TimeZone;
TimeZoneNoSpaces;
{ok, [TZName | _]} ->
TZName
end.
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
tr_char_test() ->
?assertEqual("ABCDE", tr_char("ABCDE", ?SPACE_CHAR, $_)),
?assertEqual("AB_DE", tr_char("AB DE", ?SPACE_CHAR, $_)),
?assertEqual("A_C_E", tr_char("A C E", ?SPACE_CHAR, $_)).
get_timezone_test() ->
?assertEqual("America/Los_Angeles", get_timezone("America/Los Angeles")).
tz_shift_test() ->
?assertEqual({'+',3,0}, tz_shift({{2014,1,1},{12,0,0}}, "America/Los_Angeles", "America/New_York")).
-endif. % TEST