Bulknews::Subtech RSSフィード

2006/09/11 (月)

[Perl] timezonecvt 14:27  [Perl] timezonecvt - Bulknews::Subtech を含むブックマーク はてなブックマーク -  [Perl] timezonecvt - Bulknews::Subtech

"we're planning to have it 9/12 5:30pm pacific time. Here's the meeting invite." などといわれたときとっさに日本時間何日の何時かわかるようなスクリプトを書いた。

#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use DateTime::TimeZone;
use Date::Parse;
use Getopt::Long;

my $from = "America/Los_Angeles";
my $to   = "Asia/Tokyo";

GetOptions('--from=s', \$from, '--to=s' => \$to);
Getopt::Long::Configure("bundling");

my $date = join ' ', @ARGV or die "Usage: timezonecvt [-f From] [-t To] date\n";
my @date = strptime($date);

my $dt = DateTime->new(
    second => $date[0],
    minute => $date[1],
    hour   => $date[2],
    day    => $date[3],
    month  => $date[4] + 1,
    year   => ($date[5] || (localtime)[5]) + 1900,
    time_zone => $from,
);

my $from_date = $dt->ymd('/') . ' ' . $dt->hms(':');
$dt->set_time_zone($to);
my $to_date   = $dt->ymd('/') . ' ' . $dt->hms(':');

print <<EOF;
$from_date ($from)
$to_date ($to)
EOF

以下のように実行。

% timezonecvt 9/12 5:30pm
2006/09/12 17:30:00 (America/Los_Angeles)
2006/09/13 09:30:00 (Asia/Tokyo)
% timezonecvt -f Asia/Taipei -t Europe/London 9/12 5:30pm
2006/09/12 17:30:00 (Asia/Taipei)
2006/09/12 10:30:00 (Europe/London)