Skip to content

Commit 7d19d9e

Browse files
committed
Merge pull request drfloob#11 from arkdro/master
add api to check whether the dst is active
2 parents 4b4bbcc + 9be6cde commit 7d19d9e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/ezic.erl

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
localtime/1
66
, utc_to_local/2
77
, local_to_utc/2
8+
, has_dst_utc/2
9+
, has_dst_local/2
810
]).
911

1012

@@ -30,7 +32,11 @@ local_to_utc(LocalDatetime, TzName) ->
3032
local_to_utc_handleFlatzone(LocalDatetime, ezic_db:flatzone(NormalDatetime, TzName)).
3133

3234

35+
has_dst_utc(Datetime, TzName) ->
36+
has_dst(Datetime, TzName, u).
3337

38+
has_dst_local(Datetime, TzName) ->
39+
has_dst(Datetime, TzName, w).
3440

3541
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3642
% PRIVATE
@@ -55,3 +61,12 @@ local_to_utc_handleFlatzone(LocalDatetime, #flatzone{offset=Offset, dstoffset=DS
5561
, Offset, {0,0,0})
5662
, DSTOffset, {0,0,0}).
5763

64+
has_dst(Datetime, TzName, Flag) ->
65+
NormalDatetime = ezic_date:normalize(Datetime, Flag),
66+
case ezic_db:flatzone(NormalDatetime, TzName) of
67+
#flatzone{dstoffset={0,0,0}} ->
68+
false;
69+
#flatzone{dstoffset={_,_,_}} ->
70+
true
71+
end.
72+

test/ezic_tests.erl

+27
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,30 @@ local_to_utc_funkyDST_test_() ->
4343
, ?_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
4444
, ?_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
4545
].
46+
47+
has_dst_local_test_() ->
48+
[
49+
?_assertMatch(false, ezic:has_dst_local({{1998,3,1},{1,30,0}}, "Europe/Paris")),
50+
?_assertMatch(true, ezic:has_dst_local({{1998,9,30},{1,30,0}}, "Europe/Paris")),
51+
?_assertMatch(true, ezic:has_dst_local({{1998,8,30},{3,30,0}}, "America/Denver")),
52+
?_assertMatch(false, ezic:has_dst_local({{1998,10,30},{3,30,0}}, "America/Denver")),
53+
%% weird date
54+
?_assertMatch(false, ezic:has_dst_local({{1998,2,31},{10,30,0}}, "Europe/Paris")),
55+
%% invalid zone
56+
?_assertException(error, {case_clause, {error, no_zone}}, ezic:has_dst_local({{1998,10,20},{12,30,0}}, "non_existent")),
57+
%% ambiguous zone
58+
?_assertException(error, {case_clause, {error, {ambiguous_zone, _}}}, ezic:has_dst_local({{1998,10,25},{1,30,0}}, "America/Denver"))
59+
].
60+
61+
has_dst_utc_test_() ->
62+
[
63+
?_assertMatch(false, ezic:has_dst_utc({{1998,3,28},{13,30,0}}, "Europe/Paris")),
64+
?_assertMatch(true, ezic:has_dst_utc({{1998,3,29},{1,30,0}}, "Europe/Paris")),
65+
?_assertMatch(true, ezic:has_dst_utc({{1998,10,25},{7,59,59}}, "America/Denver")),
66+
?_assertMatch(false, ezic:has_dst_utc({{1998,10,25},{8,0,0}}, "America/Denver")),
67+
%% weird date
68+
?_assertMatch(false, ezic:has_dst_utc({{1998,2,31},{13,30,0}}, "Europe/Paris")),
69+
%% invalid zone
70+
?_assertException(error, {case_clause, {error, no_zone}}, ezic:has_dst_utc({{1998,10,25},{1,30,0}}, "non_existent"))
71+
].
72+

0 commit comments

Comments
 (0)