From 7bcd1596b04a6a2c971569c5fdfdc96ba9e61c8c Mon Sep 17 00:00:00 2001 From: arkdro Date: Thu, 21 Feb 2013 00:07:14 +0400 Subject: [PATCH 1/3] add api to check whether the dst is active --- src/ezic.erl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/ezic.erl b/src/ezic.erl index 18ce7f0..c6612f0 100644 --- a/src/ezic.erl +++ b/src/ezic.erl @@ -5,6 +5,8 @@ localtime/1 , utc_to_local/2 , local_to_utc/2 + , has_dst_utc/2 + , has_dst_local/2 ]). @@ -30,7 +32,11 @@ local_to_utc(LocalDatetime, TzName) -> local_to_utc_handleFlatzone(LocalDatetime, ezic_db:flatzone(NormalDatetime, TzName)). +has_dst_utc(Datetime, TzName) -> + has_dst(Datetime, TzName, u). +has_dst_local(Datetime, TzName) -> + has_dst(Datetime, TzName, w). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % PRIVATE @@ -55,3 +61,14 @@ local_to_utc_handleFlatzone(LocalDatetime, #flatzone{offset=Offset, dstoffset=DS , Offset, {0,0,0}) , DSTOffset, {0,0,0}). +has_dst(Datetime, TzName, Flag) -> + NormalDatetime = ezic_date:normalize(Datetime, Flag), + case ezic_db:flatzone(NormalDatetime, TzName) of + #flatzone{dstoffset={0,0,0}} -> + false; + #flatzone{dstoffset={_,_,_}} -> + true; + _ -> + false + end. + From 511c7815dc7acafef31aa8aeef44d6580b95ed46 Mon Sep 17 00:00:00 2001 From: arkdro Date: Sat, 2 Mar 2013 14:16:55 +0400 Subject: [PATCH 2/3] remove catch-all branch --- src/ezic.erl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ezic.erl b/src/ezic.erl index c6612f0..4aec5b1 100644 --- a/src/ezic.erl +++ b/src/ezic.erl @@ -67,8 +67,6 @@ has_dst(Datetime, TzName, Flag) -> #flatzone{dstoffset={0,0,0}} -> false; #flatzone{dstoffset={_,_,_}} -> - true; - _ -> - false + true end. From 9be6cde77b445e0c68ba5d9e9a497bd348b74d04 Mon Sep 17 00:00:00 2001 From: arkdro Date: Sat, 2 Mar 2013 14:17:38 +0400 Subject: [PATCH 3/3] add tests for has_dst_{utc,local} --- test/ezic_tests.erl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/ezic_tests.erl b/test/ezic_tests.erl index 62b6a46..0e23bae 100644 --- a/test/ezic_tests.erl +++ b/test/ezic_tests.erl @@ -43,3 +43,30 @@ local_to_utc_funkyDST_test_() -> , ?_assertMatch({{1951,9,7},{17,0,0}}, ezic:local_to_utc({{1951,9,8},{2,0,0}}, "Asia/Tokyo")) %% DST off for the last time , ?_assertMatch({{1952,5,3},{17,0,0}}, ezic:local_to_utc({{1952,5,4},{2,0,0}}, "Asia/Tokyo")) %% ensure DST not in effect for 1952 ]. + +has_dst_local_test_() -> + [ + ?_assertMatch(false, ezic:has_dst_local({{1998,3,1},{1,30,0}}, "Europe/Paris")), + ?_assertMatch(true, ezic:has_dst_local({{1998,9,30},{1,30,0}}, "Europe/Paris")), + ?_assertMatch(true, ezic:has_dst_local({{1998,8,30},{3,30,0}}, "America/Denver")), + ?_assertMatch(false, ezic:has_dst_local({{1998,10,30},{3,30,0}}, "America/Denver")), + %% weird date + ?_assertMatch(false, ezic:has_dst_local({{1998,2,31},{10,30,0}}, "Europe/Paris")), + %% invalid zone + ?_assertException(error, {case_clause, {error, no_zone}}, ezic:has_dst_local({{1998,10,20},{12,30,0}}, "non_existent")), + %% ambiguous zone + ?_assertException(error, {case_clause, {error, {ambiguous_zone, _}}}, ezic:has_dst_local({{1998,10,25},{1,30,0}}, "America/Denver")) + ]. + +has_dst_utc_test_() -> + [ + ?_assertMatch(false, ezic:has_dst_utc({{1998,3,28},{13,30,0}}, "Europe/Paris")), + ?_assertMatch(true, ezic:has_dst_utc({{1998,3,29},{1,30,0}}, "Europe/Paris")), + ?_assertMatch(true, ezic:has_dst_utc({{1998,10,25},{7,59,59}}, "America/Denver")), + ?_assertMatch(false, ezic:has_dst_utc({{1998,10,25},{8,0,0}}, "America/Denver")), + %% weird date + ?_assertMatch(false, ezic:has_dst_utc({{1998,2,31},{13,30,0}}, "Europe/Paris")), + %% invalid zone + ?_assertException(error, {case_clause, {error, no_zone}}, ezic:has_dst_utc({{1998,10,25},{1,30,0}}, "non_existent")) + ]. +