Perl program that performs prorated calculation

I will explain how to perform daily calculation with a Perl program. In business, when you calculate money, you often do a prorated calculation. Calculate interest rates on a daily basis. When you rent a house, you pay only for the amount from the day you rent it to the end of the month.

Find the number of days from the specified date to the specified date

Let's find the number of days from the specified date to the specified date.

Number of days from July 16, 2019 to December 31, 2019 (not a leap year)

Let's find the number of days from July 16, 2019 to December 31, 2019. This year is not a leap year.

use strict;
use warnings;

use Time::Piece;

# Date object as of July 16, 2019
my $day1_tp = Time::Piece->strptime('2019-7-16','%Y-%m-%d');

# Date object as of December 31, 2019
my $day2_tp = Time::Piece->strptime('2019-12-31','%Y-%m-%d');

# Find the difference. Returns a Time::Seconds object that stores the difference information.
my $seconds = $day2_tp-$day1_tp;

# days gives the difference between dates, so adding 1 gives the correct number of days.
my $days = $seconds->days + 1;

print "$days\n";

Output result.

169

Time::Piece is a module for date and time that comes standard with Perl (Perl 5.10 or later). is.

You can use the strptime method to create a Time::Piece object with a specified date and date format.

The Time::Piece object can be subtracted and returns a Time::Seconds object that represents the difference between the Time::Piece objects.

Use the days method to find the date difference. Adding 1 to the date difference gives the correct number of days in a year.

First of all, with this, we were able to calculate the number of days in a year when calculating with the number of days in a year as the denominator.

Find the number of days in a year

Let's find the number of days in the year.

Find the number of days in a year (not a leap year)

Let's find the number of days in the year. The number of days from January 1, 2019 to December 31, 2019. This year is not a leap year.

use strict;
use warnings;

use Time::Piece;

#January 1, 2019 Date Object
my $day1_tp = Time::Piece->strptime('2019-1-1','%Y-%m-%d');

# Date object as of December 31, 2019
my $day2_tp = Time::Piece->strptime('2019-12-31','%Y-%m-%d');

# Find the difference. Returns a Time::Seconds object that stores the difference information.
my $seconds = $day2_tp - $day1_tp;

#days gives the difference between dates, so adding 1 gives the correct number of days.
my $days = $seconds->days + 1;

print "$days\n";

Output result.

365

The number of days in the year was output.

Find the number of days in a year (in a leap year)

Find the number of days in a leap year. This year is a leap year. Write exactly the same process as above, changing only the year.

use strict;
use warnings;

use Time::Piece;

# Date object as of January 1, 2020
my $day1_tp = Time::Piece->strptime('2020-1-1','%Y-%m-%d');

# Date object as of December 31, 2020
my $day2_tp = Time::Piece->strptime('2020-12-31','%Y-%m-%d');

# Find the difference. Returns a Time::Seconds object that stores the difference information.
my $seconds = $day2_tp - $day1_tp;

#days gives the difference between dates, so adding 1 gives the correct number of days.
my $days = $seconds->days + 1;

print "$days\n";

Output result.

366

Even in leap years, the number of days is correct.

Find the number of days in a month

The date always starts on the 1st, but the end may be the 31st, 30th, 29th, or 28th. To ask for it automatically in such cases, do the following:

First, get the first day of the next month. Let's get the number of days in February 2020. This year is a leap year.

use strict;
use warnings;

use Time::Piece;

# Year
my $year = 2020;

# Month
my $mon = 2;

# Next month
my $mon_next = $mon + 1;

# Time::Piece object on the first day of the next month
my $next_mon_tp = Time::Piece->strptime("$year-$mon_next-1", '%Y-%m-%d');

# Subtract 1 second to get information on the last day of the previous month
my $this_mon_last_tp = $next_mon_tp - 1;

# Get the last day. This is the same value as the number of months.
my $this_mon_days_count = $this_mon_last_tp->mday;

print "$this_mon_days_count\n";

Output result.

29

It's the number of days in February of a leap year.

It's OK if you know the number of days in the year, the number of days in the month, and the number of days from one day to another

If you know the number of days in a year, the number of days in a month, and the number of days from one day to another, you can calculate on a daily basis.

Daily amount = amount * (days / year or month)

Program sample for finding rent for the first month

This is a program sample for finding the rent for the first month. The monthly rent is 79,000 yen, and the rent for the first month when delivered on June 18, 2020.

use strict;
use warnings;

use Time::Piece;

# rent
my $yatin = 79_000;

# Year
my $year = 2020;

# Month
my $mon = 6;

# start date
my $start_mday = 18;

# Next month
my $mon_next = $mon + 1;

# Time::Piece object on the first day of the next month
my $next_mon_tp = Time::Piece->strptime("$year-$mon_next-1", '%Y-%m-%d');

# Subtract 1 second to get information on the last day of the previous month
my $this_mon_last_tp = $next_mon_tp - 1;

# Get the last day. This is the same value as the number of months.
my $this_mon_days_count = $this_mon_last_tp->mday;

# Date object on June 19, 2020
my $day1_tp = Time::Piece->strptime("2020-6-$start_mday", '%Y-%m-%d');

# Date object as of December 31, 2020
my $day2_tp = Time::Piece->strptime("2020-6-$this_mon_days_count", '%Y-%m-%d');

# Find the difference. Returns a Time::Seconds object that stores the difference information.
my $seconds = $day2_tp - $day1_tp;

# Days
my $days_count = $seconds->days + 1;

# Daily rent
my $hiwari_yatin = $yatin * ($days_count / $this_mon_days_count);

# Decimal point truncation
my $hiwari_yatin_cut = int $hiwari_yatin;

print "$hiwari_yatin_cut\n";

Output result.

34233

The rent for the first month was "34,233 yen".

After doing the prorated calculation, decimal point is truncated at the end.

You can copy and paste this program and try it immediately at PerlBanjo.

Associated Information