Friday, October 29, 2010

Perl World clock

Heres another hacked up script that gives the time locally on the system(mine is NYT), the time in japan and the number of seconds fro NYT midnight..

Try running it(called datetime.pl) with these commands lines:
./datetime.pl 
./datetime.pl "7:00"
./datetime.pl "+ 1 day + 1 hours"
./datetime.pl "-10 seconds"

#!/usr/bin/env perl

use strict;
use Date::Manip;

sub string2date
{
    my ($str) = @_;
    return int(UnixDate($str, "%s")); 
}

sub date2string
{
    my ($time) = @_;
    return UnixDate("epoch $time", "%m/%d/%y %H:%M:%S"); 
}

sub date2timezone
{
    my ($time, $tz) = @_;

    return UnixDate(Date_ConvTZ(ParseDate("epoch $time"), '', $tz), '%m/%d/%y %H:%M:%S');
}

sub date2midnightSec
{
    my ($time) = @_;
    my @a = localtime($time); 
    return 3600*@a[2] + 60*@a[1] + @a[0];
}

my $intime = $ARGV[0];
$intime = 'now' unless defined $intime;

my $rawInTime  = string2date($intime);
print "NYT: " . date2string($rawInTime) . "\n";
print "JST: " .  date2timezone($rawInTime, "JST") . "\n";
print "Midnight Seconds: " .  date2midnightSec($rawInTime) . "\n";

No comments:

Post a Comment