# -------------------------------------------------------------------------------------
# flo::Template::HTTPDate
# -------------------------------------------------------------------------------------
#
#    Author   : Jean-Michel Hiver                                (c) MKDoc Holding Ltd.
#
#    Usage <% http_date %>                      # current date
#       or <% http_date name="date_variable" %> # mysql date contained in date_variable
#
# -------------------------------------------------------------------------------------
package flo::Template::HTTPDate;
use lib::Template::AbstractTag;
use lib::Exception;
use lib::Template;
use flo::HTTP::Date;
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});
    
    # if $varname contains nothing, then we'll use the current time
    my $varname = $token_hash->{name} || undef;
    my $time = undef;
    if (defined $varname)
    {
	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--;
	$time = timelocal ($lsecond, $lminute, $lhour, $lday, $lmonth, $lyear);
    }
    else
    {
	$time = time;
    }
    
    push @{$result}, flo::HTTP::Date::http_date ($time);
}


1;







