public
Last active

  • Download Gist
gistfile1.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#!/usr/bin/perl
 
use strict;
use warnings;
use 5.10.0;
 
{ # First day of next month
use DateTime qw[];
my $d = DateTime->today->truncate(to => 'month')->add(months => 1);
say $d->ymd;
}
 
{ # First day of next month
use Time::Piece qw[];
my $d = Time::Piece->new;
$d = $d + (86400 * $d->month_last_day) - (86400 * ($d->mday - 1));
say $d->ymd;
}
 
{ # First day of next month
use Date::Simple qw[];
my $d = Date::Simple->today;
$d = $d + Date::Simple::days_in_month($d->year, $d->month) - ($d->day - 1);
say $d;
}
 
use Date::Simple qw[days_in_month];
 
sub mkdate ($$$) {
my ($y, $m, $d) = @_;
 
if ($m < 1 || $m > 12) {
$y += $m / 12;
$m %= 12;
if ($m < 1) {
$m += 12;
$y--;
}
}
if ($d < 1) {
while ($d < 1) {
$m--;
if ($m < 1) {
$m = 12;
$y--;
}
$d += days_in_month($y, $m);
}
}
elsif ($d > 28) {
my $dim = days_in_month($y, $m);
while ($d > $dim) {
$d -= $dim;
$m++;
if ($m > 12) {
$m = 1;
$y++;
}
$dim = days_in_month($y, $m);
}
}
return ($y, $m, $d);
}
 
my $d = Date::Simple->today;
 
# First day of next month
say Date::Simple->new(mkdate($d->year, $d->month + 1, 1));
 
# Last day of next month
say Date::Simple->new(mkdate($d->year, $d->month + 2, 0));
 
# First day of previous month
say Date::Simple->new(mkdate($d->year, $d->month - 1, 1));
 
# Last day of previous month
say Date::Simple->new(mkdate($d->year, $d->month, 0));
 
# First day of current month
say Date::Simple->new(mkdate($d->year, $d->month, 1));
 
# Last day of current month
say Date::Simple->new(mkdate($d->year, $d->month + 1, 0));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.