Hello Anonymous Monk,
Something "similar" has been asked before How to find business days?.
But also to point out that there is a beautiful module Date::Manip that contains a function called Date::Manip::Recur. There also some examples provided Date::Manip::Examples/RECURRING EVENTS which can retrieve the frequency from until specified date.
The beauty of this module is that you can change days, months, years, timezone(s) more or less what ever comes to your mind and you will get what you want.
Update For better and more accurate calculation use it like this:
my $start = ParseDate('now');
my $end = "2017123123:59:59";
# Instead of
my ($start, $end) = qw(2017-01-01 2017-12-31);
Sample of code:
#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;
use feature 'say';
my ($start, $end) = qw(2017-01-01 2017-12-31);
# y:m:w:d:h:m:s
my @days = ParseRecur('0:0:0:1:0:0:0', $start, $start, $end);
my @weeks = ParseRecur('0:0:1::0:0:0', $start, $start, $end);
my @months = ParseRecur('0:1:0::0:0:0', $start, $start, $end);
my @years = ParseRecur('1:0:0::0:0:0', $start, $start, $end);
my @datesEvery15th = ParseRecur('0:0:0:15:0:0:0', $start, $start, $end
+);
say "Number of days: " . scalar @days;
say "Number of weeks: " . scalar @weeks;
say "Number of months: " . scalar @months;
say "Number of years: " . scalar @years;
say "Number of payments: " . scalar @datesEvery15th;
=print payment dates
for my $date (@dates15th) {
say UnixDate($date, "%Y-%m-%d");
}
=cut
__END__
$ perl test.pl
Number of days: 365
Number of weeks: 53
Number of months: 12
Number of years: 1
Number of payments: 25
Update2: To find out if the payment day is a working day or it will be shifted to the next working day. Read the POD for more information Date::Manip::DM5:
for my $date (@datesEvery15th) {
if (Date_IsWorkDay($date)) {
say UnixDate($date, "%Y-%m-%d");
}
else {
my $nearestWorkingData = Date_NearestWorkDay($date);
say UnixDate($nearestWorkingData, "%Y-%m-%d");
}
}
__END__
2017-10-09
2017-10-24
2017-11-08
2017-11-23
2017-12-08
2017-12-22
Update3: An alternative way to know the exact time from today to what ever day you want that can easily be updated based on timezone. Sample of code below:
#!/usr/bin/perl
use strict;
use warnings;
use Date::Manip;
use feature 'say';
use Data::Dumper;
my $dateLocal = ParseDate('now');
# say $dateLocal;
my $end = "2017123123:59:59";
my $dateStartLocal = ParseDate($dateLocal);
my $dateEnd = ParseDate($end);
my $deltaLocal = DateCalc( $dateStartLocal,
$dateEnd,
1 );
say $deltaLocal;
my @dataLocal = split(/:/, $deltaLocal);
my @values = qw(year(s) month(s) week(s) day(s) hour(s) minute(s));
my %hashLocal;
@hashLocal{@values} = @dataLocal;
print Dumper \%hashLocal;
###### Different Timezone(s) ######
# From timeZone To timeZone
my $dateTimeZone = Date_ConvTZ($dateLocal,"GMT","CST");
my $dateStartTimezone = ParseDate($dateLocal);
my $deltaTimezone = DateCalc( $dateStartTimezone,
$dateEnd,
1 );
say $deltaTimezone;
my @dataTimezone = split(/:/, $deltaTimezone);
my %hashTinezone;
@hashTinezone{@values} = @dataTimezone;
print Dumper \%hashTinezone;
__END__
$ perl test.pl
0:2:3:1:9:13:31
$VAR1 = {
'week(s)' => '3',
'month(s)' => '2',
'minute(s)' => '13',
'hour(s)' => '9',
'day(s)' => '1',
'year(s)' => '0'
};
0:2:3:1:9:13:31
$VAR1 = {
'year(s)' => '0',
'day(s)' => '1',
'minute(s)' => '13',
'hour(s)' => '9',
'week(s)' => '3',
'month(s)' => '2'
};
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
|