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 Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/