Skip to content

Instantly share code, notes, and snippets.

@mjdominus
Created March 29, 2011 19:03
Show Gist options
  • Save mjdominus/893016 to your computer and use it in GitHub Desktop.
Save mjdominus/893016 to your computer and use it in GitHub Desktop.
Obsessive kid-age-calculating program
#!/usr/bin/perl
use Date::Calc ':all';
my @ARG;
if (@ARGV) {
if ($ARGV[0] =~ /^(\d{4})-?(\d{2})-?(\d{2})$/) {
@ARG = ($1, $2, $3);
} else {
my $d = time() + shift() * 86400;
my @d = localtime $d;
@ARG = ($d[5] + 1900, $d[4] + 1, $d[3]);
}
} else {
@ARG = Today();
}
my $bday_18 = 6574; # days to 18th birthday
my $dda = Delta_Days(2008,3,30, @ARG);
print "Today Lila is $dda days old.\n";
my ($dY, $dM, $dD) = my_Delta_YMD(2008,3,30, @ARG);
my $years = $dY == 1 ? "year" : "years" ;
my $months = $dM == 1 ? "month" : "months";
my $days = $dD == 1 ? "day" : "days" ;
my $frac = y2d($dda);
print " That's $dY $years, $dM $months, and $dD $days ($frac years).\n";
my $pct = 100 * $dda/$bday_18;
printf " You are %.2f%% done.\n", $pct;
my $dd = Delta_Days(2004,6,14, @ARG);
print "Today Iris is $dd days old.\n";
my ($dY, $dM, $dD) = my_Delta_YMD(2004,6,14, @ARG);
my $years = $dY == 1 ? "year" : "years" ;
my $months = $dM == 1 ? "month" : "months";
my $days = $dD == 1 ? "day" : "days" ;
my $frac = y2d($dd);
print " That's $dY $years, $dM $months, and $dD $days ($frac years).\n";
printf " That's %.2f Lilas.\n", $dd / $dda;
my $pct = 100 * $dd/$bday_18;
printf " You are %.2f%% done.\n", $pct;
my $mjdd = Delta_Days(1969,4,2, @ARG);
my $frac = y2d($mjdd);
print "You are $mjdd days old ($frac years).\n";
printf " That's %.2f Irises.\n", $mjdd / $dd;
printf " That's %.2f Lilas.\n", $mjdd / $dda;
my $mlkd = Delta_Days(1968,8,22, @ARG);
my $frac = y2d($mlkd);
print "Lorrie is $mlkd days old ($frac years).\n";
printf " That's %.2f Irises.\n", $mlkd / $dd;
printf " That's %.2f Lilas.\n", $mlkd / $dda;
my $mmd = Delta_Days(1997, 7, 20, @ARG);
my ($mY, $mM, $mD) = my_Delta_YMD(1997,7,20, @ARG);
print "You have been married for $mmd days.\n";
$years = $mY == 1 ? "year" : "years" ;
$months = $mM == 1 ? "month" : "months";
$days = $mD == 1 ? "day" : "days" ;
print " That's $mY $years, $mM $months, and $mD $days.\n";
printf " That's %5.2f%% of your life.\n", 100*$mmd/$mjdd;
printf " And %5.2f%% of Lorrie's life.\n", 100*$mmd/$mlkd;
sub my_Delta_YMD {
my ($ay, $am, $ad,
$by, $bm, $bd) = @_;
my $negate;
my ($dY, $dM, $dD) = Delta_YMD($ay, $am, $ad, $by, $bm, $bd);
if ($dY < 0) {
($dY, $dM, $dD) = (-$dY, -$dM, -$dD);
$negate=1;
}
if ($dD < 0) {
my ($py, $pm) = ($by, $bm); # previous month
$pm--;
if ($pm == 0) { $pm = 12; $py-- }
$dD += Days_in_Month($py, $pm);
$dM --;
}
if ($dM < 0) {
$dY--;
$dM += 12;
}
($dY, $dM, $dD) = (-$dY, -$dM, -$dD) if $negate;
($dY, $dM, $dD);
}
sub y2d {
my $d = shift;
sprintf "%.2f", $d / (146097/400); # days per year
}
@mjdominus
Copy link
Author

Sample output:

Today Lila is 1094 days old.
That's 2 years, 11 months, and 27 days (3.00 years).
You are 16.64% done.
Today Iris is 2479 days old.
That's 6 years, 9 months, and 15 days (6.79 years).
That's 2.27 Lilas.
You are 37.71% done.
You are 15336 days old (41.99 years).
That's 6.19 Irises.
That's 14.02 Lilas.
Lorrie is 15559 days old (42.60 years).
That's 6.28 Irises.
That's 14.22 Lilas.
You have been married for 5000 days.
That's 13 years, 8 months, and 9 days.
That's 32.60% of your life.
And 32.14% of Lorrie's life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment