Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] timeconv.pl



Here is a fun little Perl script that I hacked together (BSD licence,
so got nuts!):

#!/bin/env perl
#
# =========================================================================
# File: timeconv.pl
#
# Copyright (c) 2005 and onwards, Josh Glover <jglover@example.com>
#
# LICENCE:
#
#   This file is distributed under the terms of the BSD-2 License.
#   See the COPYING file, which should have been distributed with
#   this file, for details. If you did not receive the COPYING file,
#   see:
#
#   http://www.jmglov.net/opensource/licenses/bsd.txt
#
# DESCRIPTION:
#
#   Converts time from one zone to another
#
# USAGE:
#
#   See USAGE, below
#
# EXAMPLES:
#
#   timeconv.pl toyko los_angeles 2005/12/19 19:00
#
#   ln -s timeconv.pl tokyo2los_angeles
#   tokyo2los_angeles 2005/12/19 19:00
#
# TODO:
#
#   - Nothing, this code is perfect
#
# DEPENDENCIES:
#
#   Perl 5
#   CPAN modules: DateTime, DateTime::Format::Strptime, and
#                 DateTime::TimeZone
#
# MODIFICATIONS:
#
#   Josh Glover <jmglov@example.com> (2005/12/19): Initial revision
# =========================================================================

use strict;
use warnings;

use DateTime;
use DateTime::Format::Strptime;
use File::Basename;

use constant EXEC_NAME => File::Basename::basename( $0 );

use constant USAGE =>
  "Usage: ".&EXEC_NAME." [<from_zone> <to_zone>] \%Y/\%m/\%d \%H:\%M\n";

# See if we have from and to timezone names in the args
my $from_name = shift @example.com if $ARGV[0] and $ARGV[0] =~ /^[[:alpha:]_\/]+$/;
my $to_name   = shift @example.com if $ARGV[0] and $ARGV[0] =~ /^[[:alpha:]_\/]+$/;

# If not, the executable itself could contain the names
if (not $from_name and &EXEC_NAME =~ /(\w+)2(\w+)/) {

  $from_name = $1;
  $to_name   = $2;

} # if (locating zone names in)

my $str = join ' ', @example.com;

# Make sure we have timezone names
die &USAGE unless $from_name and $to_name;

# Search for timezones by name (we will need an object for this)
my $tz_obj  = DateTime::TimeZone->new( name => "floating" );
my $from_tz = $from_name if $tz_obj->is_olson( $from_name );
my $to_tz   = $to_name   if $tz_obj->is_olson( $to_name   );

# If either timezone could not be resolved by name, search for it
unless ($from_tz and $to_tz) {

  my $names = DateTime::TimeZone->all_names;

  foreach my $name (@$names) {

    $from_tz = $name if not $from_tz and $name =~ /\/$from_name$/i;
    $to_tz   = $name if not $to_tz   and $name =~ /\/$to_name$/i;

  } # foreach (searching for timezones by name)

} # unless (must search for a name)

# Make sure we have proper timezones
die( &EXEC_NAME.": could not find a matching from timezone: $from_name\n" )
  unless $from_tz;
die( &EXEC_NAME.": could not find a matching to timezone: $to_name\n" )
  unless $to_tz;

# Do the conversion already!
my $dt =
  DateTime::Format::Strptime->new( pattern => '%Y/%m/%d %H:%M',
                                   time_zone => $from_tz )
    ->parse_datetime( $str )
      or die &USAGE;

printf "%30s: %s %02d:%02d\n", $from_tz, $dt->ymd( '/' ), $dt->hour, $dt->min;

$dt->set_time_zone( $to_tz );

printf "%30s: %s %02d:%02d\n", $to_tz, $dt->ymd( '/' ), $dt->hour, $dt->min;


Here is some example usage:

: jglover@example.com; timeconv.pl tokyo los_angeles 2005/12/19 16:30
                    Asia/Tokyo: 2005/12/19 16:30
           America/Los_Angeles: 2005/12/18 23:30
: jglover@example.com; which timeconv.pl
/home/jglover/bin/timeconv.pl
: jglover@example.com; cd bin
: jglover@example.com; ln -s timeconv.pl tokyo2los_angeles
: jglover@example.com; ln -s timeconv.pl los_angeles2tokyo
: jglover@example.com; cd
: jglover@example.com; tokyo2los_angeles 2005/12/19 16:30
                    Asia/Tokyo: 2005/12/19 16:30
           America/Los_Angeles: 2005/12/18 23:30
: jglover@example.com; los_angeles2tokyo 2005/12/19 16:30
           America/Los_Angeles: 2005/12/19 16:30
                    Asia/Tokyo: 2005/12/20 09:30

Fun with the DateTime CPAN module. Feel free to suggest improvements,
etc. That is why I stuck it inline instead of attaching it. :)

Cheers,
Josh

Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links