[Canberra-pm] configuring ISO 8601 date output using Template module

Nick dos Remedios nick at cambia.org
Mon Feb 5 22:10:23 PST 2007


If you can't change the output format in Oracle (preferred option),  
then you can write a simple date conversion subroutine and pass its  
reference to the TT2 template (inside the $data data structure).

Here is a first go at the conversion sub...

sub convert_date {
     #
     # Convert date from DD-MON-YYYY to YYYY-MM-DD
     #
     my ($input_date) = @_;
     my ($day, $month, $year, $iso_date);

     if ($input_date !~ /\d{2}\-\w{3}\-\d{4}/) {
         return $input_date; # or output error message
     }

     my %months = ('JAN' => '01', 'FEB' => '02', 'MAR' => '03'); # etc
     my @parts  = split(/\-/, $input);
     $day   = $parts[0];
     $month = $months{uc($parts[1])};  #  uc() to ensure upper case
     $year  = $parts[2];
     $iso_date = "$year-$month-$day";
     return $iso_date;
}

Next add a reference to this sub to the $data hash (might need fixing  
as I don't know what $data looks like inside).

sub processTemplate {
   my ($data, $filename) = @_;

   my ($outfile) = $outdir . "/" . $filename . ".xml";
   open (my $fh, "> $outfile") or die "Can't open the output file  
$outfile: $!^?\n";

   $data->{"convert_date"} = \&convert_date; # or $data 
{"convert_date} ...

   my $config = {  INCLUDE_PATH    =>      $outdir,
                   TAG_STYLE       =>      'html',
                   PRE_CHOMP       =>      1,
                };

   my $tt = Template->new($config);
   my $templatefile = "anztemp.xml";

   $tt->process("$templatefile", $data, $fh) || die $tt->error;
   close $fh;

}

Then in your template file, replace the date declaration with:

<!-- convert_date(LASTUPDATE) -->

Hope this helps.

Nick

On 06/02/2007, at 1:07 PM, <John.Hockaday at ga.gov.au>  
<John.Hockaday at ga.gov.au> wrote:
> Hi All,
>
> I have a script that uses the Template PERL module to create an XML  
> document
> from the hash table pulled out of a RDBMS.  Part of the template  
> processing
> code is:
> ###########################################
> sub processTemplate {
>   my ($data, $filename) = @_;
>
>   my ($outfile) = $outdir . "/" . $filename . ".xml";
>   open (my $fh, "> $outfile") or die "Can't open the output file  
> $outfile: $!
> ^?\n";
>
>   my $config = {  INCLUDE_PATH    =>      $outdir,
>                   TAG_STYLE       =>      'html',
>                   PRE_CHOMP       =>      1,
>                };
>
>   my $tt = Template->new($config);
>   my $templatefile = "anztemp.xml";
>
>   $tt->process("$templatefile", $data, $fh) || die $tt->error;
>   close $fh;
>
> } #End processTemplate
> #########################################
>
> Part of the template to generate the XML is:
>
> #########################################
>   <metainfo>
>     <metd>
>       <date>
> <!-- LASTUPDATE -->
>       </date>
>     </metd>
>   </metainfo>
> #########################################
>
> The trouble is that the LASTUPDATE date (Oracle RDBMS) is in the form
> DD-MON-YYYY  whereas I want ISO 8601 format like YYYY-MM-DD.  Can I  
> insert a
> filter or something to change the format of the date?  If so what  
> should the
> code look like.
>
> Thanks in advance for your help.
>
>
> John Hockaday
> Geoscience Australia
> GPO Box 378
> Canberra ACT 2601
> (02) 6249 9735
> http://www.ga.gov.au/
> john.hockaday\@ga.gov.au
> _______________________________________________
> Canberra-pm mailing list
> Canberra-pm at pm.org
> http://mail.pm.org/mailman/listinfo/canberra-pm

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/canberra-pm/attachments/20070206/6d13a997/attachment.html 


More information about the Canberra-pm mailing list