package MKDoc::Util::Date;
use strict;
use vars qw /$LANG/;

$LANG = {
    en => {
	wday      => [ qw /Sunday Monday Tuesday Wednesday Thursday Friday Saturday/ ],
	wday_abbr => [ qw /Sun Mon Tue Wed Thu Fri Sat/ ],
	mon       => [ qw /January February March April May June July August September October November December/ ],
	mon_abbr  => [ qw /Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/ ],
    },
    fr => {
	wday      => [ qw /Dimanche Lundi Mardi Mercredi Jeudi Vendredi Samedi/ ],
	wday_abbr => [ qw /Dim Lun Mar Mer Jeu Ven Sam/ ],
	mon       => [ qw /Janvier Février Mars Avril Mai Juin Juillet Août Septembre Octobre Novembre Decembre/ ],
	mon_abbr  => [ qw /Jan Fev Mar Avr Mai Jui Jul Aug Sep Oct Nov Dec/ ],
    },
    es => {
	wday      => [ qw /Domingo Lunes Martes Miércoles Jueves Viernes Sábado/ ],
	wday_abbr => [ qw /Dom Lun Mar Mié Jue Vie Sáb/ ],
	mon       => [ qw /Enero Febrero Marcha Abril Puede Junio Julio Agosto Septiembre Octubre Noviembre Diciembre/ ],
	mon_abbr  => [ qw /Ene Feb Mar Abr Pue Jun Jul Ago Sep Oct Nov Dic/ ],
    },
    de => {
	wday      => [ qw /Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag/ ],
	wday_abbr => [ qw /Son Mon Die Mit Don Fre Sam/ ],
	mon       => [ qw /Januar Februar März April Mai Juni Juli August September Oktober November Dezember/ ],
	mon_abbr  => [ qw /Jan Feb Mär Apr Mai Jun Jul Aug Sep Oct Nov Dez/ ],
    },
    it => {
	wday      => [ qw /Domenica Lunedi Martedi Mercoledi Giovedi Venerdi Sabato/ ],
	wday_abbr => [ qw /Dom Lun Mar Mer Gio Ven Sab/ ],
	mon       => [ qw /Gennaio Febbraio Marzo Aprile Maggio Giugno Luglio Agosto Settembre Ottobre Novembre Dicembre/ ],
	mon_abbr  => [ qw /Gen Feb Mar Apr Mag Giu Lug Ago Set Ott Nov Dic/ ],
    },
};


##
# $class->new (time);
# -------------------
#   Instanciates a new MKDoc::Date object
##
sub new
{
    my $class = shift;
    $class = ref $class || $class;
    
    my $self = bless { time => shift || time }, $class;
    return $self;
}


##
# $self->time();
# --------------
#   Returns the current Date 'time' in seconds since the epoch
##
sub time
{
    my $self = shift;
    return $self->{time};
}


##
# $self->set_time ($time);
# ------------------------
#   Sets the current Date 'time' in seconds since the epoch
##
sub set_time($)
{
    my $self = shift;
    $self->{time} = shift;
}


##
# $self->filter ($template, $iso_lang);
# -------------------------------------
#   Processes and returns this date depending on what's in $template
##
sub filter
{
    my $self = shift;
    my $tmpl = shift;
    my $iso_lang_1 = shift;
    my $time = $self->time;
    my $iso_lang_2 = $iso_lang_1;
    $iso_lang_2 =~ s/-.*//;
    my $ref_lang = $LANG->{$iso_lang_1} || $LANG->{$iso_lang_2} || $LANG->{'en'};
    
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime ($time);
    $year += 1900;
    my $am_pm = ($hour > 12) ? 'pm' : 'am';
    
    my $hour_padded = $hour;
    $hour_padded = '0' . $hour if (length ($hour) == 1);
    
    my $hour_12 = ($hour > 12) ? ($hour - 12) : $hour;
    my $hour_12_padded = $hour_12;
    $hour_12_padded = '0' . $hour_12_padded if (length ($hour_12) == 1);
    
    my $hash = {
	AM_PM          => $am_pm,
	SEC            => $sec,
	MIN            => $min,
	HOUR_24        => $hour,
	HOUR_24_PADDED => $hour_padded,
	HOUR_12        => $hour_12,
	HOUR_12_PADDED => $hour_12_padded,
	MDAY           => $mday,
	MON            => $mon,
	MON_LANG       => $ref_lang->{mon}->[$mon],
	MON_LANG_ABBR  => $ref_lang->{mon_abbr}->[$mon],
	YEAR           => $year,
	WDAY           => $wday,
	WDAY_LANG      => $ref_lang->{wday}->[$wday],
	WDAY_LANG_ABBR => $ref_lang->{wday_abbr}->[$wday],
	YDAY           => $yday,
	ISDST          => $isdst,
    };
    
    my %tags = map { $_ => 1 } $tmpl =~ /<(.+?)>/g;
    my @tags = keys %tags;
    foreach my $tag (@tags)
    {
	my $val = $hash->{$tag} || next;
	$tag = quotemeta ('<' . $tag . '>');
	$tmpl =~ s/$tag/$val/g;
    }
    
    return $tmpl;
}


1;
