#!/usr/bin/perl -w
#
# time.pl
#
# J. Uckelman (uckelman@nomic.net)
# 28 October 2000
#


my @month = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

my $start = 970444800; # 2 October 2000, 00:00:00 UTC, in seconds since the epoch
my $now = time;
my $elapsed = $now - $start;

my $nweek = int($elapsed / (86400 * 10));
my $nday = int($elapsed / 86400) % 10;

my ($second, $minute, $hour, $day, $month, $year) = (gmtime($now))[0 .. 5];

printf "The current time is %.2d:%.2d:%.2d, %d %s %d.<br>\nToday is day %d of nweek %d.<br>\n", $hour, $minute, $second, $day, $month[$month], $year + 1900, $nday + 1, $nweek + 1;

my $vbegin = $start + ($nweek * 10 + 7) * 86400;
my $vend = $start + (($nweek + 1) * 10) * 86400;
my ($vbday, $vbmonth, $vbyear) = (gmtime($vbegin))[3 .. 5];
my ($veday, $vemonth, $veyear) = (gmtime($vend))[3 .. 5];

printf "Voting for nweek %d beg%s at 00:00:00, %d %s %d.<br>\n", $nweek + 1, $now > $vbegin ? "an" : "ins", $vbday, $month[$vbmonth], $vbyear + 1900;

printf "Voting for nweek %d ends at 00:00:00, %d %s %d.<br>\n", $nweek + 1, $veday, $month[$vemonth], $veyear + 1900;

my ($untilday, $untilhour, $untilminute, $untilsecond) = &timediff($now, $nday >= 7 ? $vend : $vbegin);

printf "Voting %s in %d day%s, %.2d:%.2d:%.2d hours.<br>\n", $nday >= 7 ? "ends" : "begins", $untilday, $untilday != 1 ? "s" : "", $untilhour, $untilminute, $untilsecond;

#
# timediff
#
sub timediff ($$) {
	my $diff = $_[1] - $_[0];

	return int($diff / 86400), int(($diff % 86400) / 3600), int(($diff % 3600) / 60), $diff % 60;
}
