
# DaylightOrStandard.pl
# Movable Type plugin tag for daylight savings time vs. standard time
# by Kevin Shay
# http://www.staggernation.com/mtplugins/
# last modified July 12, 2004

use strict;
package MT::Plugin::DaylightOrStandard;
use vars qw( $VERSION );
$VERSION = '1.1';

use MT;
use MT::Template::Context;

eval{ require MT::Plugin };
unless ($@) {
    my $plugin = {
        name => "DaylightOrStandard $VERSION",
        description => 'Display text depending on whether or not a date falls within Daylight Saving Time.',
        doc_link => 'http://www.staggernation.com/mtplugins/DaylightOrStandard'
    }; 
    MT->add_plugin(new MT::Plugin($plugin));
}

MT::Template::Context->add_tag('DaylightOrStandard' => sub{&_hdlr_daylight_or_standard;});
use MT::Util qw ( decode_html );

my $timelocal = 0;

sub _hdlr_daylight_or_standard {
# handler for MT tag to print a value depending on whether the current date
# falls within the range of dates for Daylight Savings Time
	my ($ctx, $args) = @_;
	defined(my $ts = $ctx->{'current_timestamp'}) || return $ctx->error('No date context');
	return $ctx->error('No "daylight" or "standard" value passed')
		unless (defined($args->{'daylight'}) || defined($args->{'standard'}));
	my ($y, $m, $d, $h, $min, $s) = unpack('A4A2A2A2A2A2', $ts);
		# timelocal expects a zero-based month
	$m--;
	$timelocal ||= load_timelocal();
	my $dst = (localtime(timelocal($s, $min, $h, $d, $m, $y)))[8];
	my $text = ($dst ? (defined($args->{'daylight'}) ? $args->{'daylight'} : '')
			: (defined($args->{'standard'}) ? $args->{'standard'} : ''));
	return decode_html($text);
}

sub load_timelocal {
# instead of use()ing Time::Local, load it the first time it's needed
# (so it'll only load if one of the tags is actually called)
	require Time::Local;
	import Time::Local qw( timelocal );
	return 1;
}

1;