end0tknr's kipple - 新web写経開発

http://d.hatena.ne.jp/end0tknr/ から移転しました

Date::Calc for perl で、GMT <-> Localtime 変換

こんな感じかな?

#!/usr/local/bin/perl
use strict;
use warnings;
use utf8;
use Date::Calc;
use Data::Dumper;

main(@ARGV);

# http://search.cpan.org/perldoc?Date%3A%3ACalc

sub main {

    gmt_to_local();
    local_to_gmt();
}

sub gmt_to_local {

    my $is_gmt = 1;
    my @now = Date::Calc::Today_and_Now($is_gmt);
    my @tzone = Date::Calc::Timezone();
    my @now_2 = Date::Calc::Add_Delta_YMDHMS(@now[0..5], @tzone[0..5]);

    print "GMT->LOCAL : ";
    print sprintf("%04d-%02d-%02d %02d:%02d:%02d",@now);
    print " -> ";
    print sprintf("%04d-%02d-%02d %02d:%02d:%02d",@now_2),"\n\n";
}

sub local_to_gmt {

    my @now = Date::Calc::Today_and_Now();
    my @tzone = Date::Calc::Timezone();
    my @now_2 = Date::Calc::Add_Delta_YMDHMS(@now[0..5],
                                             $tzone[0] * -1,
                                             $tzone[1] * -1,
                                             $tzone[2] * -1,
                                             $tzone[3] * -1,
                                             $tzone[4] * -1,
                                             $tzone[5] * -1);
    print "LOCAL->GMT : ";
    print sprintf("%04d-%02d-%02d %02d:%02d:%02d",@now);
    print " -> ";
    print sprintf("%04d-%02d-%02d %02d:%02d:%02d",@now_2),"\n";
}