makes ezic:local_to_utc errors into values; updates tests accordingly
Like in d98eed, errors are now returned as values rather than crashing processes. This is a short-term solution for consistency, and can be refactored to a saner model later.
This commit is contained in:
18
src/ezic.erl
18
src/ezic.erl
@@ -27,13 +27,8 @@ utc_to_local(UTCDatetime, TzName) ->
|
|||||||
%% returns utc time for corresponding time in given timezone utc
|
%% returns utc time for corresponding time in given timezone utc
|
||||||
local_to_utc(LocalDatetime, TzName) ->
|
local_to_utc(LocalDatetime, TzName) ->
|
||||||
NormalDatetime= ezic_date:normalize(LocalDatetime, w),
|
NormalDatetime= ezic_date:normalize(LocalDatetime, w),
|
||||||
#flatzone{offset=Offset, dstoffset=DSTOffset}= ezic_db:flatzone(NormalDatetime, TzName),
|
local_to_utc_handleFlatzone(LocalDatetime, ezic_db:flatzone(NormalDatetime, TzName)).
|
||||||
|
|
||||||
ezic_date:add_offset(
|
|
||||||
ezic_date:add_offset(
|
|
||||||
LocalDatetime
|
|
||||||
, Offset, {0,0,0})
|
|
||||||
, DSTOffset, {0,0,0}).
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -49,3 +44,14 @@ utc_to_local_handleFlatzone(UTCDatetime, #flatzone{offset=Offset, dstoffset=DSTO
|
|||||||
UTCDatetime
|
UTCDatetime
|
||||||
, Offset)
|
, Offset)
|
||||||
, DSTOffset).
|
, DSTOffset).
|
||||||
|
|
||||||
|
|
||||||
|
local_to_utc_handleFlatzone(_, X={error, _}) ->
|
||||||
|
X;
|
||||||
|
local_to_utc_handleFlatzone(LocalDatetime, #flatzone{offset=Offset, dstoffset=DSTOffset}) ->
|
||||||
|
ezic_date:add_offset(
|
||||||
|
ezic_date:add_offset(
|
||||||
|
LocalDatetime
|
||||||
|
, Offset, {0,0,0})
|
||||||
|
, DSTOffset, {0,0,0}).
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,11 @@
|
|||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
local_to_utc_errors_test_() ->
|
local_to_utc_errors_test_() ->
|
||||||
ezic_db:init(),
|
|
||||||
[
|
[
|
||||||
?_assertError(ambiguous_zone, ezic:local_to_utc({{2010,11,7},{1,0,0}}, "America/Los_Angeles"))
|
?_assertMatch({error, {ambiguous_zone, _}}, ezic:local_to_utc({{2010,11,7},{1,0,0}}, "America/Los_Angeles"))
|
||||||
, ?_assertError(no_zone, ezic:local_to_utc({{2010,3,14},{2,30,0}}, "America/Los_Angeles"))
|
, ?_assertEqual({error, no_zone}, ezic:local_to_utc({{2010,3,14},{2,30,0}}, "America/Los_Angeles"))
|
||||||
|
].
|
||||||
|
|
||||||
|
utc_to_local_smoke_test_() ->
|
||||||
|
[?_assertEqual({{2012,12,17},{13,20,0}}, ezic:utc_to_local({{2012,12,17},{4,20,0}}, "Asia/Tokyo"))
|
||||||
].
|
].
|
||||||
|
|||||||
Reference in New Issue
Block a user