happy path tested! current times correctly reported for LA, NY, Tokyo, and Jamaica!

Japan doesn't observer DST. I didn't know that.
This commit is contained in:
aj
2010-11-04 04:20:38 -07:00
parent d48d32ddbb
commit 7dd76519a0
10 changed files with 236 additions and 23 deletions

View File

@@ -2,19 +2,61 @@
-include("include/ezic.hrl").
-export([]).
-export([current/2, relevant/2, sort/2]).
-export([from_time/1, dst_sec/1]).
% returns a list of all {datetime,offset} sets that must be
% projected for this Rule.
project(Rule=#rule{from=F, to=only}) ->
not_done;
project(Rule=#rule{from=F, to=T}) ->
Dates= lists:map(fun(Y)-> ezic_date:for(Rule, Y) end, lists:seq(F, T)).
current(_, []) ->
none;
current(Now, Rules) ->
RelevantRules= lists:filter(fun(R)-> relevant(Now, R) end, Rules),
% ?debug("RelevantRules: ~p", [RelevantRules]),
SRules= lists:sort(fun sort/2, RelevantRules),
CRule= choose_rule(SRules),
CRule.
sort(R1, R2) ->
T1= from_time(R1),
T2= from_time(R2),
ezic_date:compare_datetimes(T1, T2).
relevant(Now, Rule=#rule{from=F, to=T}) ->
{{Y,_,_},_} = Now,
case ezic_date:date_between(Y, {F,T}) of
false -> false;
true -> relevant2(Now, Rule)
end.
relevant2(Now, Rule=#rule{in=Month, on=Day, at=Time}) ->
{{Y,_,_},_} = Now,
RTime= {{Y, Month, Day}, Time},
ezic_date:compare_datetimes(RTime, Now).
from_time(#rule{from=F, in=M, on=O, at=A}) ->
{{F,M,O},A}.
choose_rule([]) ->
none;
choose_rule([H|_]) ->
H.
dst_sec(none) ->
0;
dst_sec(Rule) ->
calendar:time_to_seconds((Rule#rule.save)#tztime.time).
% returns all the years inbetween
years(_, Rule=#rule{from=F, to=only}) ->
[F];
years(Max, Rule=#rule{from=F, to=maximum}) ->
fudge.