Determining my daughter's age

I imagine that just about every parent who programs has written something like this at one time or another.

use DateTime;

my $birthday = shift || 20110205;
$birthday =~ s/-//g;

my ( $year, $month, $day ) = ( $birthday =~ /^(\d\d\d\d)(\d\d)(\d\d)$/ );
$birthday = DateTime->new(
    year  => $year,
    month => $month,
    day   => $day,
);
my $dur = DateTime->now->delta_days($birthday);

my ( $weeks, $days ) = ( $dur->weeks, $dur->days );
given ($days) {
    when (0) { $days = "exactly" }
    when (1) { $days = " and 1 day" }
    default  { $days = " and $_ days" }
}

say "$weeks weeks $days";

Me and my daughter

In other news, my daughter is "29 weeks exactly" today (though the picture above was actually taken in late June in Haarlem, a small town near Amsterdam).

2 Comments

She's a cutie! :)

Leave a comment

About Ovid

user-pic Have Perl; Will Travel. Freelance Perl/Testing/Agile consultant. Photo by http://www.circle23.com/. Warning: that site is not safe for work. The photographer is a good friend of mine, though, and it's appropriate to credit his work.