Perl Advent Calendar 2007-12-07

Feliz Navidad, c. 2007

by Josh McAdams

Correction: An earlier version of this indicated that TimeDate subclassed DateTime, when it in fact predates it. The code also since been cleaned up by the management to fully take advantage of the distribution.

Everyone knows that nothing is a timeless as a keepsake /stamped|engraved|mongrammed/ with the date in which it was given. This Christmas millions of people will be waiting in anticipation for that ornament or shirt tagged with 2007, knowing that they only have five or six days to show it off before it is officially considered 'so last year'. After that, it becomes a relic of the past, hidden in a box or drawer waiting to be held long enough that it can tactfully be given to a second-hand store.

Aiming to please, Santa is prepared to label items with the Christmas date each year. As easy as we may think it is to slap a 'December 25, 2007' on everything, we forget that Santa serves more than just an English speaking audience. In order to label his gifts in the appropriate way for each country and language, Santa relies on the power of Date::Format and siblings Date::Language and Date::Parse from the TimeDate distribution, a crufty but lightweight alternative to some basic functions of DateTime. You can see Santa's complicity in the treadmill of planned obsolence below.

mod7.pl


   1 use Date::Parse;
   2 use Date::Language; #uses Date::Format;
   3 
   4 #Equivalent but much faster, even when casting to epoch outside the loop
   5 #my $xmas = DateTime->now->set( month => 12, day => 25 )->epoch;
   6 my $xmas = str2time('12/25');
   7 
   8 for my $language (
   9     qw(Austrian Czech Danish Dutch English Finnish
  10     French German Greek Italian Norwegian Swedish)
  11   )
  12 {
  13     my $df = Date::Language->new($language);
  14     print $df->time2str( "[$language] %e %B %Y\n", $xmas );
  15 }