DateTime::Duration - Duration objects for date math


NAME

DateTime::Duration - Duration objects for date math

Back to Top


SYNOPSIS

  use DateTime::Duration;
  $d = DateTime::Duration->new( years   => 3,
                                months  => 5,
                                weeks   => 1,
                                days    => 1,
                                hours   => 6,
                                minutes => 15,
                                seconds => 45, 
                                nanoseconds => 12000 );
  # Convert to different units
  $d->in_units('days', 'hours', 'seconds');
  # The important parts for date math
  $d->delta_months
  $d->delta_days
  $d->delta_minutes
  $d->delta_seconds
  $d->delta_nanoseconds
  my %deltas = $d->deltas
  $d->is_wrap_mode
  $d->is_limit_mode
  $d->is_preserve_mode
  print $d->end_of_month_mode;
  # Multiple all deltas by -1
  my $opposite = $d->inverse;
  my $bigger  = $dur1 + $dur2;
  my $smaller = $dur1 - $dur2; # the result could be negative
  my $bigger  = $dur1 * 3;
  my $base_dt = DateTime->new( year => 2000 );
  my @sorted =
      sort { DateTime::Duration->compare( $a, $b, $base_dt ) } @durations;
  # Human-readable accessors, always positive, but use
  # DateTime::Format::Duration instead
  $d->years;
  $d->months;
  $d->weeks;
  $d->days;
  $d->hours;
  $d->minutes;
  $d->seconds;
  $d->nanoseconds;
  if ( $d->is_positive ) { ... }
  if ( $d->is_zero )     { ... }
  if ( $d->is_negative ) { ... }

Back to Top


DESCRIPTION

This is a simple class for representing duration objects. These objects are used whenever you do date math with DateTime.pm.

See the How Date Math is Done section of the DateTime.pm documentation for more details. The short course: One cannot in general convert between seconds, minutes, days, and months, so this class will never do so. Instead, create the duration with the desired units to begin with, for example by calling the appropriate subtraction/delta method on a DateTime.pm object.

Back to Top


METHODS

Like DateTime itself, DateTime::Duration returns the object from mutator methods in order to make method chaining possible.

DateTime::Duration has the following methods:

Overloading

This class overloads addition, subtraction, and mutiplication.

Comparison is not overloaded. If you attempt to compare durations using <=> or cmp, then an exception will be thrown! Use the compare() class method instead.

Back to Top


SUPPORT

Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details.

Back to Top


AUTHOR

Dave Rolsky <autarch@urth.org>

However, please see the CREDITS file for more details on who I really stole all the code from.

Back to Top


COPYRIGHT

Copyright (c) 2003-2010 David Rolsky. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Portions of the code in this distribution are derived from other works. Please see the CREDITS file for more details.

The full text of the license can be found in the LICENSE file included with this module.

Back to Top


SEE ALSO

datetime@perl.org mailing list

http://datetime.perl.org/

Back to Top

 DateTime::Duration - Duration objects for date math