Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Commit

Permalink
Fixed UNTIL rules not being properly applied in all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodogg committed Feb 21, 2012
1 parent 8b1576f commit 5a1490f
Showing 1 changed file with 77 additions and 25 deletions.
102 changes: 77 additions & 25 deletions modules/DP-iCalendar/lib/DP/iCalendar.pm
Expand Up @@ -90,9 +90,14 @@ sub get_monthinfo {
return if _nf_assert(defined $Month, 'Month missing');
$this->_GenerateCalendar($Year);
my @DAYS;
if(defined($this->{OrderedCalendar}{$Year}{$Month})) {
foreach(keys(%{$this->{OrderedCalendar}{$Year}{$Month}})) {
if(defined($this->{OrderedCalendar}{$Year}{$Month}))
{
foreach(keys(%{$this->{OrderedCalendar}{$Year}{$Month}}))
{
if(keys %{$this->{OrderedCalendar}{$Year}{$Month}{$_}})
{
push(@DAYS, $_);
}
}
}
return(\@DAYS);
Expand All @@ -110,7 +115,8 @@ sub get_dateinfo {
$this->_GenerateCalendar($Year);

my @TIME;
if(defined($this->{OrderedCalendar}{$Year}{$Month}) and defined($this->{OrderedCalendar}{$Year}{$Month}{$Day})) {
if(_deepValues($this->{OrderedCalendar}, $Year, $Month))
{
foreach(keys(%{$this->{OrderedCalendar}{$Year}{$Month}{$Day}})) {
push(@TIME, $_);
}
Expand All @@ -131,7 +137,8 @@ sub get_timeinfo {

$this->_GenerateCalendar($Year);
my @UIDs;
if(defined($this->{OrderedCalendar}{$Year}{$Month}) and defined($this->{OrderedCalendar}{$Year}{$Month}{$Day}) and defined($this->{OrderedCalendar}{$Year}{$Month}{$Day}{$Time})) {
if( _deepValues($this->{OrderedCalendar},$Year,$Month,$Day,$Time))
{
foreach(@{$this->{OrderedCalendar}{$Year}{$Month}{$Day}{$Time}}) {
push(@UIDs, $_);
}
Expand Down Expand Up @@ -1103,6 +1110,42 @@ sub _GetAllUIDS
return $this->{dataManager}->listvalues();
}

sub _deepFetch
{
my $hay = shift;
foreach my $needle (@_)
{
if(defined $hay->{$needle})
{
$hay = $hay->{$needle};
}
else
{
return;
}
}
return $hay;
}

sub _deepValues
{
my $val = _deepFetch(@_);
if(defined $val)
{
if(ref($val))
{
if(ref($val) eq 'ARRAY' && @{$val})
{
return 1;
}
elsif(ref($val) eq 'HASH' && keys %{$val})
{
return 1;
}
}
}
}

# --- Internal RRULE calculation functions ---

# Purpose: Parse an RRULE
Expand Down Expand Up @@ -1357,20 +1400,23 @@ sub _RRULE_DAILY {
my $LoopYear = $YEAR;
while($LoopYear eq $YEAR) {
my $iCalTime = iCal_ConvertFromUnixTime($TimeString);

# Handle UNTIL.
if($UNTIL)
{
if($TimeString > $UNTIL)
{
last;
}
}

$Dates{$iCalTime} = 1;

# One day is 86400
$TimeString += 86400;
my $NextiCalTime = iCal_ConvertFromUnixTime($TimeString);
my ($evYear, $evMonth, $evDay, $evTime) = iCal_ParseDateTime($NextiCalTime);
$LoopYear = $evYear;
# Handle UNTIL.
if($UNTIL) {
if($TimeString > $UNTIL) {
last;
}
}

}
# The loop has enedd and we've done all required calculations for BYDAY.
return(\%Dates);
Expand Down Expand Up @@ -1504,19 +1550,23 @@ sub _RRULE_WEEKLY {
my $LoopYear = $YEAR;
while($LoopYear eq $YEAR) {
my $iCalTime = iCal_ConvertFromUnixTime($TimeString);

# Handle UNTIL.
if($UNTIL)
{
if($TimeString > $UNTIL)
{
last;
}
}

$Dates{$iCalTime} = 1;

# One day is 86400, thus one week is 86400 * 7 = 604800.
$TimeString += 604800;
my $NextiCalTime = iCal_ConvertFromUnixTime($TimeString);
my ($evYear, $evMonth, $evDay, $evTime) = iCal_ParseDateTime($NextiCalTime);
$LoopYear = $evYear;
# Handle UNTIL.
if($UNTIL) {
if($TimeString > $UNTIL) {
last;
}
}

}
# The loop has ended and we've done all required calculations
Expand Down Expand Up @@ -1588,21 +1638,23 @@ sub _RRULE_MONTHLY {
my $LoopYear = $YEAR;
while(1) {
my $iCalTime = iCal_GenDateTime($YEAR, $StartDate{Month}, $StartDate{Day});

# Handle UNTIL.
if($UNTIL)
{
if(iCal_ConvertToUnixTime($iCalTime) > $UNTIL)
{
last;
}
}

$Dates{$iCalTime} = 1;

# Bump month
$StartDate{Month}++;
if($StartDate{Month} > 12) {
last;
}
# Handle UNTIL.
if($UNTIL) {
my $NextiCalTime = iCal_GenDateTime($YEAR, $StartDate{Month}, $StartDate{Day});
$NextiCalTime = iCal_ConvertToUnixTime($NextiCalTime);
if($NextiCalTime > $UNTIL) {
last;
}
}

}
# The loop has ended and we've done all required calculations
Expand Down

0 comments on commit 5a1490f

Please sign in to comment.