#!/usr/bin/perl # Copyright 2004-7, Jason Thaxter # # Redistribution, with or without modification, is permitted provided that the # following condition is met: # # Redistribution of this code must retain the above copyright notice, this # condition, and the following disclaimer. # # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO # EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use strict; use DateTime::Format::ISO8601; use DateTime::Format::Duration; #use Smart::Comments; my ($birthday, $lifeday, $alpha, $omega, $full, $time_zone, $interval, $verbose, $output, $until ); use Getopt::Long; GetOptions( 'alpha|a=s' => \$alpha, 'birthday|b=s' => \$birthday, 'full|f' => \$full, 'help|h' => \&usage, 'lifeday|l=s' => \$lifeday, 'omega|z=s' => \$omega, 'time-zone|z=s' => \$time_zone, 'until|u:s' => \$until, 'verbose|v' => \$verbose, ) or usage(); if ( $alpha and $until ) { warn "--alpha and --until are mutually exclusive"; usage(); } # start $birthday ||= '1969-09-04T03:16:00-04'; ### $birthday my $start; if ( not $until ) { $start = DateTime::Format::ISO8601->parse_datetime( $alpha or $birthday ); } else { $start = DateTime->now; } $start->set_time_zone($time_zone) if $time_zone; #$start->truncate( to => 'day' ); ### start: $start->iso8601 print "Start time:\t$start\n" if $verbose; # end my $end; if ( $lifeday || $omega ) { $end = DateTime::Format::ISO8601->parse_datetime( $lifeday or $omega ); } elsif ( not $until ) { $end = DateTime->now; } else { ### $until $end = DateTime::Format::ISO8601->parse_datetime($until); } $end->set_time_zone($time_zone) if $time_zone; #$end->truncate( to => 'day' ); ### end: $end->iso8601 print "End time:\t$end\n" if $verbose; ### Calculate the difference string... my ( $dur, $fmtr, $output ); if ( not $full ) { $dur = $start->delta_days($end); ### $dur $fmtr = new DateTime::Format::Duration( pattern => '%j' ); $output = $fmtr->format_duration($dur); if ($until){ $output--; } } else { $dur = $end->subtract_datetime($start); ### $dur my $y = $dur->years; my $m = $dur->delta_months; my $d = $dur->delta_days; # ->days gives zero? $output = sprintf '%s years, %s months, %s days', $dur->years, $dur->months, $dur->{days}; # doesn't do years! #my $pattern = #'%Y years, %m months, %e days, ' . '%H hours, %M minutes, %S seconds'; #$fmtr = new DateTime::Format::Duration( pattern => $pattern); } if ( not $full ) { if ( not $lifeday and not $until ) { print "You are $output days old.\n"; } elsif ($alpha) { print "On $lifeday you were $output days old.\n"; } else { print $output; } } else { if ( not $lifeday and not $until) { print "You are $output old.\n"; } elsif ($alpha) { print "On $lifeday you were $output old.\n"; } else { print $output; } } sub usage { print STDERR <