Permalink
Browse files

Merge pull request #11 from arkdro/master

add api to check whether the dst is active
  • Loading branch information...
drfloob committed Mar 7, 2013
2 parents 4b4bbcc + 9be6cde commit 7d19d9e751f5867cd7351ba077963038a6c6384d
Showing with 42 additions and 0 deletions.
  1. +15 −0 src/ezic.erl
  2. +27 −0 test/ezic_tests.erl
View
@@ -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,12 @@ 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
+ end.
+
View
@@ -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"))
+ ].
+

0 comments on commit 7d19d9e

Please sign in to comment.