From a4921710dc13e75bf85112baab799f2a0307183e Mon Sep 17 00:00:00 2001 From: herrmannj <> Date: Tue, 22 Aug 2023 23:36:18 +0000 Subject: [PATCH] Cron.pm: wip git-svn-id: https://svn.fhem.de/fhem/trunk@27876 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/lib/FHEM/Scheduler/Cron.pm | 37 +++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/fhem/lib/FHEM/Scheduler/Cron.pm b/fhem/lib/FHEM/Scheduler/Cron.pm index 69ef08b83..4ce5278a7 100644 --- a/fhem/lib/FHEM/Scheduler/Cron.pm +++ b/fhem/lib/FHEM/Scheduler/Cron.pm @@ -8,7 +8,7 @@ use strict; use warnings; use utf8; use POSIX qw( strftime ); -use List::Util qw ( any first ); +use List::Util qw ( any first min ); use Time::HiRes qw( time ); use Time::Local qw( timelocal ); @@ -408,20 +408,39 @@ sub _next_date { $inclusive?'at or after (inclusive)':'after (exclusive)', ($from_date =~ m/(\d{4})(\d{2})(\d{2})/)) if $ENV{EXTENDED_DEBUG}; + # set of possible dates, based on the different methods + my @set; # the mdays/months - if (scalar @{$self->{list_of_mdays}}) { - $self->{next_calender_date} = $self->_next_calendar_date($from_date, $inclusive); + if (scalar @{$self->{list_of_mdays}}) { + # check if cached value can be used + if (($self->{working_date} and $self->{next_calender_date}) and + (($inclusive and $self->{next_calender_date} >= $self->{working_date}) or + ($self->{next_calender_date} > $self->{working_date}))) { + $self->log(5, 'use cached calender date %04d-%02d-%02d', ($self->{next_calender_date} =~ m/(\d{4})(\d{2})(\d{2})/)) if $ENV{EXTENDED_DEBUG}; + push @set, $self->{next_calender_date}; + } else { + push @set, $self->{next_calender_date} = $self->_next_calendar_date($from_date, $inclusive); + } } if (scalar @{$self->{list_of_or_wdays}}) { - $self->{next_weekday_date} = $self->_next_weekday_date($from_date, $inclusive); + # check if cached value can be used + if (($self->{working_date} and $self->{next_weekday_date}) and + (($inclusive and $self->{next_weekday_date} >= $self->{working_date}) or + ($self->{next_weekday_date} > $self->{working_date}))) { + $self->log(5, 'use cached weekday date %04d-%02d-%02d', ($self->{next_weekday_date} =~ m/(\d{4})(\d{2})(\d{2})/)) if $ENV{EXTENDED_DEBUG}; + push @set, $self->{next_weekday_date}; + } else { + push @set, $self->{next_weekday_date} = $self->_next_weekday_date($from_date, $inclusive); + } } - - - $self->{current_work_date} = $self->{next_calender_date}; - #exit(0); - + # whatever comes first + my $result = min @set; + $self->log(5, 'found date %04d-%02d-%02d', + # $inclusive?'at or after (inclusive)':'after (exclusive)', + ($result =~ m/(\d{4})(\d{2})(\d{2})/)) if $ENV{EXTENDED_DEBUG}; + return $result; } sub _next_calendar_date {