datetime manipulation is now much clearer. tests were added, and expectations stated explicitly in many places. rules got lots of attention.

This commit is contained in:
aj
2010-11-10 01:52:13 -08:00
parent 216a8d9ff8
commit d84a0c1822
11 changed files with 245 additions and 116 deletions

View File

@@ -4,19 +4,46 @@
for_rule_test_() ->
BR= #rule{at=#tztime{}},
[
% last days of months
?_assertEqual({2010, 03, 28}, ezic_date:for_rule(#rule{in=3, on={last, "Sun"}}, 2010))
, ?_assertEqual({2010, 11, 30}, ezic_date:for_rule(#rule{in=11, on={last, "Tue"}}, 2010))
, ?_assertEqual({2010, 11, 24}, ezic_date:for_rule(#rule{in=11, on={last, "Wed"}}, 2010))
?_assertEqual({{2010, 03, 28}, #tztime{}}, ezic_date:for_rule(BR#rule{in=3, on={last, "Sun"}}, 2010))
, ?_assertEqual({{2010, 11, 30}, #tztime{}}, ezic_date:for_rule(BR#rule{in=11, on={last, "Tue"}}, 2010))
, ?_assertEqual({{2010, 11, 24}, #tztime{}}, ezic_date:for_rule(BR#rule{in=11, on={last, "Wed"}}, 2010))
% days =< or >= absolute dates in given month
, ?_assertEqual({2010, 11, 14}, ezic_date:for_rule(#rule{in=11, on=#tzon{day="Sun", filter={geq, 9}}}, 2010))
, ?_assertEqual({2010, 11, 7}, ezic_date:for_rule(#rule{in=11, on=#tzon{day="Sun", filter={leq, 9}}}, 2010))
, ?_assertEqual({{2010, 11, 14}, #tztime{}}, ezic_date:for_rule(BR#rule{in=11, on=#tzon{day="Sun", filter={geq, 9}}}, 2010))
, ?_assertEqual({{2010, 11, 7}, #tztime{}}, ezic_date:for_rule(BR#rule{in=11, on=#tzon{day="Sun", filter={leq, 9}}}, 2010))
% these should fail
, ?_assertError(no_previous_day, ezic_date:for_rule(#rule{in=11, on=#tzon{day="Sun", filter={leq, 1}}}, 2010))
, ?_assertError(no_next_day, ezic_date:for_rule(#rule{in=11, on=#tzon{day="Sun", filter={geq, 29}}}, 2010))
, ?_assertError(no_previous_day, ezic_date:for_rule(BR#rule{in=11, on=#tzon{day="Sun", filter={leq, 1}}}, 2010))
, ?_assertError(no_next_day, ezic_date:for_rule(BR#rule{in=11, on=#tzon{day="Sun", filter={geq, 29}}}, 2010))
].
for_rule_utc_dst_test_() ->
WRule= #rule{in=11, on=11, at=#tztime{time={0,0,0}}, save={0,0,0}},
SRule= #rule{in=11, on=11, at=#tztime{time={0,0,0}, flag=s}, save={0,0,0}},
URule= #rule{in=11, on=11, at=#tztime{time={0,0,0}, flag=u}, save={0,0,0}},
DST= {1,0,0},
%%% given a DST savings of 1 in a -7 zone, and given rules in each of the relative timezones:
%%% - wall time should respect the given DST and offset
%%% - std time should respect the offset, but not DST
%%% - utc should respect neither
[
%% same "time" with different relative contexts should return different UTC results
?_assertEqual({{2010,11,11}, {6,0,0}}, ezic_date:for_rule_utc(WRule, {-7,0,0}, DST, 2010))
, ?_assertEqual({{2010,11,11}, {7,0,0}}, ezic_date:for_rule_utc(SRule, {-7,0,0}, DST, 2010))
, ?_assertEqual({{2010,11,11}, {0,0,0}}, ezic_date:for_rule_utc(URule, {-7,0,0}, DST, 2010))
%% some absolute time in all contexts should return the same UTC results
, ?_assertEqual({{2010,11,11}, {0,0,0}},
ezic_date:for_rule_utc(WRule#rule{on=10, at=#tztime{flag=w, time={18,0,0}}}, {-7,0,0}, DST, 2010))
, ?_assertEqual({{2010,11,11}, {0,0,0}},
ezic_date:for_rule_utc(SRule#rule{on=10, at=#tztime{flag=s, time={17,0,0}}}, {-7,0,0}, DST, 2010))
, ?_assertEqual({{2010,11,11}, {0,0,0}},
ezic_date:for_rule_utc(URule, {-7,0,0}, DST, 2010))
].

37
test/ezic_rule_tests.erl Normal file
View File

@@ -0,0 +1,37 @@
-module(ezic_rule_tests).
-include("include/ezic.hrl").
-include_lib("eunit/include/eunit.hrl").
project_next_test_() ->
[
% simple UTC example
?_assertEqual({{2010,11,10},{16,20,0}},
ezic_rule:project_next(
#rule{from=1984, to=2032, in=11, on=10, at=#tztime{time={16,20,0}, flag=u}}
, {-5,0,0}, {-1,0,0}, {{2010,11,10},{0,0,0}}
))
% skip a year UTC example
, ?_assertEqual({{2011,11,10},{16,20,0}},
ezic_rule:project_next(
#rule{from=1984, to=2032, in=11, on=10, at=#tztime{time={16,20,0}, flag=u}}
, {-5,0,0}, {-1,0,0}, {{2010,11,10},{16,21,0}}
))
% skip 3 years UTC example
, ?_assertEqual({{2013,11,10},{16,20,0}},
ezic_rule:project_next(
#rule{from=1984, to=2032, in=11, on=10, at=#tztime{time={16,20,0}, flag=u}}
, {-5,0,0}, {-1,0,0}, {{2012,11,11},{16,20,0}}
))
% no solution
, ?_assertEqual(none,
ezic_rule:project_next(
#rule{from=1984, to=2010, in=11, on=10, at=#tztime{time={16,20,0}, flag=u}}
, {-5,0,0}, {-1,0,0}, {{2012,11,11},{16,20,0}}
))
].

View File

@@ -12,3 +12,23 @@ split_by_name_test_() ->
, ?_assertEqual({BZones, AZones}, ezic_zone:split_by_name(B, AllZones))
, ?_assertEqual({[], AllZones}, ezic_zone:split_by_name(#zone{name=c}, AllZones))
].
project_end_test_() ->
[
?_assertEqual({{2010,11,10},{16,20,0}},
ezic_zone:project_end(
#zone{until={{2010,11,10},#tztime{time={10,20,0}, flag=w}}, gmtoff={-7,0,0}}
, {1,0,0}))
, ?_assertEqual({{2010,11,10},{16,20,0}},
ezic_zone:project_end(
#zone{until={{2010,11,10},#tztime{time={9,20,0}, flag=s}}, gmtoff={-7,0,0}}
, {1,0,0}))
, ?_assertEqual({{2010,11,10},{16,20,0}},
ezic_zone:project_end(
#zone{until={{2010,11,10},#tztime{time={16,20,0}, flag=u}}, gmtoff={-7,0,0}}
, {1,0,0}))
].

View File

@@ -7,5 +7,6 @@ all() ->
, eunit:test(ezic_date)
, eunit:test(ezic_zone)
, eunit:test(ezic_parse)
, eunit:test(ezic_rule)
,ok.