Date-Utility

 view release on metacpan or  search on metacpan

lib/Date/Utility.pm  view on Meta::CPAN

=back

Returns a new L<Date::Utility> object plus the given months. If the day is greater than days in the new month, it will take the day of end month.
e.g.

    print Date::Utility->new('2000-01-31')->plus_months(1)->date_yyyymmdd;
    # will print 2000-02-28

=cut

sub plus_months {
    my ($self, $months) = @_;
    (looks_like_number($months) && $months == int($months)) || die "Need an integer months number";
    my $new_year  = $self->year;
    my $new_month = $self->month + $months;
    if ($new_month < 1 || $new_month > 12) {
        $new_year += floor($new_month / 12);
        $new_month = $new_month % 12;
        if ($new_month < 1) {    # when date is 2011-01-01, and $months is -13, then here $new_month will be 0, so hanndle this case here.
            $new_year--;
            $new_month += 12;
        }
    }
    my $new_day = $self->day_of_month;
    return $self->_create_trimmed_date($new_year, $new_month, $new_day);
}

*_plus_months = \&plus_months;

=head2 minus_months

Takes the following argument as named parameter:

=over 4

=item * C<years> - number of months to be subracted. (Integer)

=back

Returns a new L<Date::Utility> object minus the given months. If the day is greater than days in the new month, it will take the day of end month.
e.g.

    print Date::Utility->new('2000-03-31')->minus_months(1)->date_yyyymmdd;
    # will print 2000-02-28

=cut

sub minus_months {
    my ($self, $months) = @_;
    return $self->_plus_months(-$months);
}

*_minus_months = \&minus_months;

=head2 create_trimmed_date

Takes the following argument as named parameter:

=over 4

=item * C<year> - calendar year of the date (Integer)

=item * C<month> - calendar month of the date. (Integer)

=item * C<day> - day of the month of the date. (Integer)

=back

Returns a valid L<Date::Utility> object whose date part is same with the given year, month and day and time part is not changed. If the day is greater than the max day in that month , then use that max day as the day in the new object.

=cut

sub create_trimmed_date {
    my ($self, $year, $month, $day) = @_;
    my $max_day = __PACKAGE__->new(sprintf("%04d-%02d-01", $year, $month))->days_in_month;
    $day = $day < $max_day ? $day : $max_day;
    my $date_string = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year, $month, $day, $self->hour, $self->minute, $self->second);
    return __PACKAGE__->new($date_string);
}

*_create_trimmed_date = \&create_trimmed_date;

1;
__END__

=head1 DEPENDENCIES

=over 4

=item L<Moo>

=item L<DateTime>

=item L<POSIX>

=item L<Scalar::Util>

=item L<Time::Local>

=item L<Syntax::Keyword::Try>

=back


=head1 AUTHOR

Binary.com, C<< <support at binary.com> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-date-utility at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-Utility>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Date::Utility


You can also look for information at:



( run in 0.700 second using v1.01-cache-2.11-cpan-39bf76dae61 )