#!/usr/bin/perl -Tw use strict; $|++; use CGI qw(:standard); use GD; use constant CRLF => "\015\012"; use constant SLEEPYTIME => 5; use constant MAX_UPDATE => 60; ## perl -MDate::Manip -e 'print &UnixDate(ParseDate("1/1/2000 12:00am GMT"),"%s")' use constant Y2K_GMT => 946684800; my @TIME_UNITS = ( [ year => 365.25 * 24 * 60 * 60 ], [ week => 7 * 24 * 60 * 60 ], [ day => 24 * 60 * 60 ], [ hour => 60 * 60 ], [ minute => 60 ], [ second => 1 ], ); my $tz = param('tz') || 0; my $font = gdLargeFont; my $char_x = $font->width; my $char_y = $font->height; print "HTTP/1.0 200 OK" . CRLF; my $BOUNDARY = "the-time"; print map($_ . CRLF, "Content-Type: multipart/x-mixed-replace; boundary=$BOUNDARY", "", "--$BOUNDARY"); for (my $count = 1; $count < MAX_UPDATE; $count++) { my $gif = time_gif(); print header(-type => 'image/gif', -content_length => length($gif), -expires => 'now', ); print $gif; print CRLF . "--$BOUNDARY" . CRLF; sleep SLEEPYTIME; } exit 0; sub time_gif { my $time = time - 3600 * $tz; my $left = Y2K_GMT - $time; my $string = "no time at all"; if ($left >= 0) { my @items = map { my ($unit, $secs) = @$_; my $count = int($left/$secs); $left -= $count * $secs; $count ? $count == 1 ? "1 $unit" : "$count ${unit}s" : (); } @TIME_UNITS; $string = @items > 2 ? join(", ", @items[0..$#items-1], "and $items[-1]") : @items > 1 ? "$items[0] and $items[1]" : $items[0]; } my $picture_x = $char_x * length($string); my $picture_y = $char_y; my $image = GD::Image->new($picture_x, $picture_y); my $background = $image->colorAllocate(127,127,127); $image->transparent($background); $image->interlaced('true'); my $red = $image->colorAllocate(255,0,0); $image->string($font, 0, 0, $string, $red); $image->gif; }