Time-Duration-Object

 view release on metacpan or  search on metacpan

lib/Time/Duration/Object/Infinite.pm  view on Meta::CPAN

use strict;
use warnings;
package Time::Duration::Object::Infinite 0.302;
# ABSTRACT: Time::Duration::Object, but infinite

sub isa {
  return 1 if $_[1] eq 'Time::Duration::Object';
  return $_[0]->UNIVERSAL::isa($_[1]);
}

#pod =head1 SYNOPSIS
#pod
#pod  use Time::Duration::Object::Infinite;
#pod
#pod  my $duration = Time::Duration::Object::Infinite->new_future;
#pod
#pod  # It will happen forever from now.
#pod  print "It will happen ", $duration->from_now;
#pod
#pod =head1 DESCRIPTION
#pod
#pod This is a class for Time::Duration::Object-like objects representing infinite
#pod durations.
#pod
#pod =method new
#pod
#pod =method new_positive
#pod
#pod These methods return a new Time::Duration::Object::Infinite for a positive
#pod duration.
#pod
#pod =cut

sub new_positive {
	my ($class) = @_;
  my $duration = 1;
	bless \$duration => $class;
}

sub new { shift->new_positive }

#pod =method new_negative
#pod
#pod This returns a new Time::Duration::Object::Infinite for a negative duration.
#pod
#pod =cut

sub new_negative {
	my ($class) = @_;
  my $duration = -1;
	bless \$duration => $class;
}

sub _is_pos { ${$_[0]} == -1 }

#pod =method new_seconds
#pod
#pod This method returns either C<+inf> or C<-inf> using Math::BigInt.  (I don't
#pod recommend calling it.)
#pod
#pod =cut

sub seconds {
  require Math::BigInt;
  return Math::BigInt->binf(shift->_is_pos ? '-' : ());
}

#pod =method duration
#pod
#pod =method duration_exact
#pod
#pod These methods both return "forever."
#pod
#pod =cut

sub duration { 'forever' }

# This is to make it easy to implement the matched pair methods.
sub _flop {
  my ($self, $flop, $pair) = @_;
  my $is_pos = $flop ? ! $self->_is_pos : $self->_is_pos;
  my $index = $is_pos ? 0 : 1;
  my $str = $pair->[$index];
  bless \$str => 'Time::Duration::_Result::_Infinite';
}

my $ago_from_now;
my $earlier_later;
BEGIN {
  $ago_from_now  = [ 'forever ago', 'forever from now' ];
  $earlier_later = [ 'infinitely earlier', 'infinitely later' ];
}

#pod =method ago
#pod
#pod =method ago_exact
#pod
#pod These methods return "forever ago" for positive durations and "forever from
#pod now" for negative durations.
#pod
#pod =cut

sub ago       { $_[0]->_flop(1, $ago_from_now); }
sub ago_exact { $_[0]->_flop(1, $ago_from_now); }

#pod =method from_now
#pod
#pod =method from_now_exact
#pod
#pod These methods do the opposite of the C<ago> methods.
#pod

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.039 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )