From John.Hockaday at ga.gov.au Mon Feb 5 18:07:28 2007 From: John.Hockaday at ga.gov.au (John.Hockaday at ga.gov.au) Date: Tue, 6 Feb 2007 13:07:28 +1100 Subject: [Canberra-pm] configuring ISO 8601 date output using Template module Message-ID: <8BD19F29B0E16E4F88277A997CD872C228CEAA@mail.agso.gov.au> 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: ######################################### ######################################### 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 From grail at goldweb.com.au Mon Feb 5 19:10:49 2007 From: grail at goldweb.com.au (Alex Satrapa) Date: Tue, 6 Feb 2007 14:10:49 +1100 Subject: [Canberra-pm] configuring ISO 8601 date output using Template module In-Reply-To: <8BD19F29B0E16E4F88277A997CD872C228CEAA@mail.agso.gov.au> References: <8BD19F29B0E16E4F88277A997CD872C228CEAA@mail.agso.gov.au> Message-ID: <078AF158-378C-4550-83D3-3244F1273EB3@goldweb.com.au> On 06/02/2007, at 13:07 , wrote: > 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. By the time you get it, you've got a string like '12-Mar-2004', is that right? My suggestion would be to find out how to get the data from Oracle in the form that you want - if this is under your control, all well and good :) I don't know Oracle (despite being trained on in about 15 years ago), but under PostgreSQL you'd do this: select to_char(lastupdate, 'YYYY-MM-DD'), ... from ... HTH alex -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.pm.org/pipermail/canberra-pm/attachments/20070206/eab7d79f/attachment.html From nick at cambia.org Mon Feb 5 22:10:23 2007 From: nick at cambia.org (Nick dos Remedios) Date: Tue, 6 Feb 2007 17:10:23 +1100 Subject: [Canberra-pm] configuring ISO 8601 date output using Template module In-Reply-To: <8BD19F29B0E16E4F88277A997CD872C228CEAA@mail.agso.gov.au> References: <8BD19F29B0E16E4F88277A997CD872C228CEAA@mail.agso.gov.au> Message-ID: <0D2EBD28-A132-4271-87BA-2362C7172167@cambia.org> 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: Hope this helps. Nick On 06/02/2007, at 1:07 PM, 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: > > ######################################### > > > > > > > > ######################################### > > 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 From John.Hockaday at ga.gov.au Tue Feb 6 14:41:22 2007 From: John.Hockaday at ga.gov.au (John.Hockaday at ga.gov.au) Date: Wed, 7 Feb 2007 09:41:22 +1100 Subject: [Canberra-pm] configuring ISO 8601 date output using Template module Message-ID: <8BD19F29B0E16E4F88277A997CD872C228CEAF@mail.agso.gov.au> Alex and Nick, I tried what Alex suggested and it worked. I processed the date into ISO 8601 using Date::Calc and then pushed the results back into the hash that I got from my SQL to the database. This hash was then the data source for the Template package. Thanks to both of your for your prompt and very useful responses. Thanks. John -----Original Message----- From: canberra-pm-bounces+john.hockaday=ga.gov.au at pm.org [mailto:canberra-pm-bounces+john.hockaday=ga.gov.au at pm.org] On Behalf Of Alex Satrapa Sent: Tuesday, 6 February 2007 2:11 PM To: canberra-pm at pm.org Subject: Re: [Canberra-pm] configuring ISO 8601 date output using Templatemodule On 06/02/2007, at 13:07 , wrote: 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. By the time you get it, you've got a string like '12-Mar-2004', is that right? My suggestion would be to find out how to get the data from Oracle in the form that you want - if this is under your control, all well and good :) I don't know Oracle (despite being trained on in about 15 years ago), but under PostgreSQL you'd do this: select to_char(lastupdate, 'YYYY-MM-DD'), ... from ... HTH alex