Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_last_day_of_quarter and is_last_day_of_year methods #72

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Changes
@@ -1,5 +1,10 @@
{{$NEXT}}

1.47 2018-02-18

- Added is_last_day_of_quarter() and is_last_day_of_year() methods.


1.46 2018-02-11

- Fixed the formatting for the CLDR "S" symbol. It could in some cases round
Expand Down
18 changes: 18 additions & 0 deletions lib/DateTime.pm
Expand Up @@ -983,6 +983,14 @@ sub is_last_day_of_month {
$_[0]->day == $_[0]->_month_length( $_[0]->year, $_[0]->month );
}

sub is_last_day_of_quarter {
$_[0]->day_of_quarter == $_[0]->quarter_length;
}

sub is_last_day_of_year {
$_[0]->day_of_year == $_[0]->year_length;
}

sub week {
my $self = shift;

Expand Down Expand Up @@ -2984,6 +2992,16 @@ datetime object is in a leap year.
This method returns a true or false value indicating whether or not the
datetime object is the last day of the month.

=head3 $dt->is_last_day_of_quarter()

This method returns a true or false value indicating whether or not the
datetime object is the last day of the quarter.

=head3 $dt->is_last_day_of_year()

This method returns a true or false value indicating whether or not the
datetime object is the last day of the year.

=head3 $dt->month_length()

This method returns the number of days in the current month.
Expand Down
41 changes: 39 additions & 2 deletions t/03components.t
Expand Up @@ -80,8 +80,10 @@ use DateTime;
'->iso8601 ignores arguments'
);

ok( !$d->is_leap_year, '->is_leap_year' );
ok( !$d->is_last_day_of_month, '->is_last_day_of_month' );
ok( !$d->is_leap_year, '->is_leap_year' );
ok( !$d->is_last_day_of_month, '->is_last_day_of_month' );
ok( !$d->is_last_day_of_quarter, '->is_last_day_of_quarter' );
ok( !$d->is_last_day_of_year, '->is_last_day_of_year' );

is( $d->month_length, 31, '->month_length' );
is( $d->quarter_length, 92, '->quarter_length' );
Expand Down Expand Up @@ -128,6 +130,41 @@ use DateTime;
}
}

{
my @tests = (
{ year => 2017, month => 8, day => 19, expect => 0 },
{ year => 2017, month => 3, day => 31, expect => 1 },
{ year => 2017, month => 6, day => 30, expect => 1 },
{ year => 2017, month => 9, day => 30, expect => 1 },
{ year => 2017, month => 12, day => 31, expect => 1 },
);

for my $t (@tests) {
my $expect = delete $t->{expect};

my $dt = DateTime->new($t);

my $is = $dt->is_last_day_of_quarter;
ok( ( $expect ? $is : !$is ), '->is_last_day_of_quarter' );
}
}

{
my @tests = (
{ year => 2017, month => 8, day => 19, expect => 0 },
{ year => 2017, month => 12, day => 31, expect => 1 },
);

for my $t (@tests) {
my $expect = delete $t->{expect};

my $dt = DateTime->new($t);

my $is = $dt->is_last_day_of_year;
ok( ( $expect ? $is : !$is ), '->is_last_day_of_year' );
}
}

{
my @tests = (
{ year => 2016, month => 2, day => 1, expect => 29 },
Expand Down