Daisuke Maki > DateTimeX-Lite-0.00004 > DateTimeX::Lite::Duration

Download:
DateTimeX-Lite-0.00004.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Source  

my %p = validate( @_, { years => { type => SCALAR, default => 0 }, months => { type => SCALAR, default => 0 }, weeks => { type => SCALAR, default => 0 }, days => { type => SCALAR, default => 0 }, hours => { type => SCALAR, default => 0 }, minutes => { type => SCALAR, default => 0 }, seconds => { type => SCALAR, default => 0 }, nanoseconds => { type => SCALAR, default => 0 }, end_of_month => { type => SCALAR, default => undef, regex => qr/^(?:wrap|limit|preserve)$/ }, } ); ^

NAME ^

DateTimeX::Lite::Duration - Duration objects for date math

SYNOPSIS ^

  use DateTimeX::Lite::Duration;

  $d = DateTimeX::Lite::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 = DateTimeX::Lite->new( year => 2000 );
  my @sorted =
      sort { DateTimeX::Lite::Duration->compare( $a, $b, $base_dt ) } @durations;

  # Human-readable accessors, always positive, but use
  # DateTimeX::Lite::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 ) { ... }

DESCRIPTION ^

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

See the How Date Math is Done section of the DateTimeX::Lite.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 DateTimeX::Lite.pm object.

METHODS ^

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

DateTimeX::Lite::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.

SUPPORT ^

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

AUTHOR ^

Dave Rolsky <autarch@urth.org>

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

COPYRIGHT ^

Copyright (c) 2003-2006 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.

SEE ALSO ^

datetime@perl.org mailing list

http://datetime.perl.org/

syntax highlighting: