# -------------------------------------------------------------------------------------
# lib::Template::ISODate
# -------------------------------------------------------------------------------------
#
#    Author   : Jean-Michel Hiver                                (c) MKDoc Holding Ltd.
#
# -------------------------------------------------------------------------------------
package flo::Template::ISODate;
use lib::Template::AbstractTag;
use lib::Exception;
use lib::Template;
use Time::Local;
use strict;
use vars qw /@ISA/;
@ISA = qw /lib::Template::AbstractTag/;


##
# $obj->main;
# -----------
##
sub main
{
    my $self   = shift;
    my $result = $self->result;
    my $tokens = $self->tokens;
    my ($token_name, $token_hash) = $self->parse_tag (shift @{$tokens});
    
    # retrieve the variable that needs to replace the tag
    $token_hash->{name} or throw (new lib::Exception ( code => 'VARIABLE_NOT_SET',
						       info => "$token_name should have a 'name' attribute" ) );
    my $varname = $token_hash->{name};
    my $value   = $self->get ($varname);
    
    # $value should look like 2001-01-20 00:00:00
    my ($lyear, $lmonth, $lday, $lhour, $lminute, $lsecond) =
	$value =~ /([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+)[^0-9]+([0-9]+)/;
    
    $lyear -= 1900;
    $lmonth--;
    my $time = timelocal ($lsecond, $lminute, $lhour, $lday, $lmonth, $lyear);
    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime ($time);
    my @weekday = qw /Sun Mon Tue Wed Thu Fri Sat/;
    my @month   = qw /Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
    $wday = $weekday[$wday];
    $mon  = $month[$mon];
    $year += 1900;
    $mday = '0' . $mday if (length $mday == 1);
    $hour = '0' . $hour if (length $hour == 1);
    $min  = '0' . $min  if (length $min == 1);
    $sec  = '0' . $sec  if (length $sec == 1);
      
    push @{$result}, "$year-$mon-$mday" . 'T' . "$hour:$min:$sec" . 'Z';
}


1;







