#!/bin/perl -w

#############################################################
#                                                           #
#  This example updates the dates in freshmeat.xml          #
#  freshmeat dates look like  Feb 17th 2000, 21:03 or       #
#  February 17th 2000, 21:03                                #
#                                                           #
#  This tool converts them to ISO (yyyy-mm-dd) format       #
#                                                           #
#  It updates the info as it's parsing, and flushes the     #
#  twig so memory usage remains minimal                     #
#                                                           #
#############################################################

use strict;
use XML::Twig;

# just a 
my %mm= ( Jan => "01", Feb => "02", Mar => "03", Apr => "04", 
          May => "05", Jun => "06", Jul => "07", Aug => "08", 
          Sep => "09", Oct => "10", Nov => "11", Dec => "12"); 


my $twig= new XML::Twig(                    # create the twig
              TwigHandlers => 
                { created => \&upd_date,    # the 2 elements will be processed
                  updated => \&upd_date     # the same way
                }  
                        );                                                   

if( my $file= $ARGV[0]) { $twig->parsefile( $file); } # process the twig
else                    { $twig->parse( \*STDIN);   }

$twig->flush;                                         # don't forget this one
                                                      # or the end of the document
                                                      # won't be flush'ed

exit;

sub upd_date
  { my( $twig, $date)= @_; 
    my $fancy_date= $date->text;                       # get the date
    if ($fancy_date=~ /\A(\w\w\w)\w* (\d+)\w\w (\d\d\d\d)/)
      { my( $month, $day, $year)= ($mm{$1}, $2, $3);
        $date->set_text( "$year-$month-$day");         # convert it
      }
    else
      { die "can't parse date $fancy_date"; }

    $twig->flush;                                      # print the twig so far
                                                       # and free memory
  }

