Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Create aliases for quick timezone checks' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create aliases for quick timezone checks
Authored by: mojo6771 on Aug 19, '03 11:25:44PM
Using the hint above , I wrote up a perl script that does something similar. It will work only on cities in /usr/share/zoneinfo.

#!/usr/bin/env perl

use File::Find;
use File::Basename;

my $zoneDir="/usr/share/zoneinfo";
if ( $#ARGV != 0 ) { print "Usage : $0 \n"; exit ; }
my $city = $ARGV[0];

sub findWanted {
local $time;
local $timezone;
if ( -f $_ && $_ eq $city ){
$timezone= basename($File::Find::dir)."/$city";
$ENV{'TZ'}=$timezone;
$time=localtime();
print "$timezone $time \n";
}
}

find(\&findWanted, $zoneDir) ;
exit;


Save it in a file (e.g. itime).
On terminal window:
$ chmod +x itime
$ ./itime Calcutta
$./itime Eastern




[ Reply to This | # ]
Create aliases for quick timezone checks
Authored by: kevco on Aug 20, '03 11:32:01PM

I'm not sure what happened when you posted your code but 3 backslashes were stripped. One is fairly important as the script won't run without it. It's pretty handy though, thanks.

Here is the code with missing backslashes:

#!/usr/bin/env perl

use File::Find;
use File::Basename;

my $zoneDir="/usr/share/zoneinfo";
if ( $#ARGV != 0 ) { print "Usage : $0 \n"; exit ; }
my $city = $ARGV[0];

sub findWanted {
local $time;
local $timezone;
if ( -f $_ && $_ eq $city ){
$timezone= basename($File::Find::dir)."/$city";
$ENV{'TZ'}=$timezone;
$time=localtime();
print "$timezone $time \n";
}
}

find(\&findWanted, $zoneDir) ;
exit;



[ Reply to This | # ]
Create aliases for quick timezone checks
Authored by: ilovja on Aug 28, '03 11:42:50PM
Thanks for the original posting and Perl solution. I made the perl script a little more Perlish. The first reason I changed was when I did "itime hongkong", I got something like "zoneinfo/hongkong blah ...", which wasn't quite pretty. It happens when timezone data files are right under /usr/share/zonefile, such as Turkey, Poland, etc. This new version prints only city name and its local time. The second change was now it can accept case-insensitive city name. In other words, you get the same result whether you type "seoul" or "Seoul".
#!/usr/bin/perl -w

use strict;
use File::Find;
use File::Basename;

my $zoneDir="/usr/share/zoneinfo";
my $city = shift || die "Usage: itime city\n";
find(\&wanted, $zoneDir);

sub wanted {
    if ( -f $_ &&   $_ =~ /$city/i ) {  
            $ENV{'TZ'}=basename($File::Find::dir)."/$_";
            my $time=localtime();
            print "$_ $time\n";
    }
}


[ Reply to This | # ]
Create aliases for quick timezone checks
Authored by: ilovja on Aug 28, '03 11:46:24PM

dang... geeklog ate up backslashes when I posted as HTML, though it was okay when I previewed it.

#!/usr/bin/perl -w

use strict;
use File::Find;
use File::Basename;

my $zoneDir="/usr/share/zoneinfo";
my $city = shift || die "Usage: itime city\n";
find(\&wanted, $zoneDir);

sub wanted {
if ( -f $_ && $_ =~ /$city/i ) {
$ENV{'TZ'}=basename($File::Find::dir)."/$_";
my $time=localtime();
print "$_ $time\n";
}
}



[ Reply to This | # ]