#!/usr/local/bin/perl -w # # $YahooCvsId: tz2tz.pl,v 1.1.1.1 2009/02/03 18:51:34 Exp $ # $Source: /CVSROOT/.../tz2tz/src/tz2tz.pl,v $ # # Copyright (c) 2009 Yahoo! Inc. # # Originally written by Jan Schaumann in February # 2009. # # This program reads lines on stdin and converts the timestamp found at # the beginning of the line from one timezone to another. The timestamp # is expected to be in the format "%b %e %H:%M:%S". use strict; use Date::Manip; use Getopt::Long; Getopt::Long::Configure("bundling"); use POSIX; ### ### Globals ### my $TS_REGEX = qr/^([a-z]{3} ?[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}) (.*)/i; my %OPTS = ( TZ_FROM => strftime("%Z", localtime()), TZ_TO => "UTC", ); ### ### Subroutines ### # function : setOptions # purpose : initialize OPTS based on ARGV # inputs : none # returns : nothing; may exit under certain conditions sub setOptions() { my ($ok); $ok = GetOptions("help|h" => \$OPTS{'HELP'}, "from|f=s" => \$OPTS{'TZ_FROM'}, "to|t=s" => \$OPTS{'TZ_TO'}, ); if (scalar(@ARGV)) { die("Spurious arguments after flags. Try -h."); # NOTREACHED } if ($OPTS{'HELP'} || !$ok) { usage(!$ok); exit(!$ok); # NOTREACHED } } # function : tz2tz # purpose : convert the leading timestamp of the input line to the # desired second timezone representation # inputs : a line of text # returns : a line of text; if a timestamp was found, it is converted, # otherwise the same line is returned unaltered sub tz2tz($) { my ($line) = @_; if ($line =~ /$TS_REGEX/) { my $date_in = ParseDate($1); my $msg = $2; my $date_out = Date_ConvTZ($date_in, $OPTS{'TZ_FROM'}, $OPTS{'TZ_TO'}, 1); if ($date_out) { $line = UnixDate($date_out, "%b %e %H:%M:%S $msg\n"); } } return $line; } # function : usage # purpose : print usage statement # inputs : an integer; if 0, print output to STDERR, else to STDOUT # returns : nothing sub usage($) { my ($err) = @_; my $progname = basename($0); my $fh = $err ? \*STDERR : \*STDOUT; print $fh <) { print tz2tz($line); }