<?xml version="1.0" encoding="windows-1252"?>
<node id="1012596" title="Re^3: Convert timezone using offset and posix perl module (easy)" created="2013-01-10 01:28:12" updated="2013-01-10 01:28:12">
<type id="11">
note</type>
<author id="22609">
tye</author>
<data>
<field name="doctext">
&lt;c&gt;
#!/usr/bin/perl -w
use strict;

# Emulate:
#  $ date -d "Tue, 01 Jan 2013 16:05:53 +0000"
# Tue Jan  1 10:05:53 CST 2013

use Time::Local qw&lt; timegm &gt;;
use POSIX       qw&lt; strftime &gt;;

my @Mons = qw&lt; jan feb mar apr may jun jul aug sep oct nov dec &gt;;
my %Mons;  @Mons{@Mons} = 1..@Mons;

print datetime_offset2local( "@ARGV" ), $/;
exit;

sub datetime_offset2local {
    my( $date ) = @_;

    # Parse out the date parts:
    my( $dow, $day, $mon, $yr, $time, $off, $ex ) = split /,? /, $date, -1;
    die "Too many fields ($ex) in input datetime ($date)\n"
        if  defined $ex;
    my( $hr, $min, $sec ) = split /:/, $time;
    $mon = $Mons{ lc $mon }
        ||  die "Invalid month name ($mon): $date\n";

    # Convert input date to epoch seconds (ignoring GMT offset):
    $sec = timegm( $sec, $min, $hr, $day, $mon-1, $yr );

    # Parse the GMT offset and apply it:
    ( my $sign, $hr, $min ) = $off =~ /^([-+])([0-9][0-9])([0-9]*)$/
        or  die "Invalid offset ($off): $date\n";
    $off = $hr*60;
    $off += $min || 0;
    $off *= 60;
    $off *= -1
        if  '-' eq $sign;

    # Convert epoch seconds to string using local time zone and locale:
    return strftime( "%a, %b %d %Y %T %Z (%z)", localtime( $sec - $off ) );
}

__END__

 $ TZ=US/Central perl offset2local.pl Tue, 01 Jan 2013 16:05:53 +0000
Tue, Jan 01 2013 10:05:53 CST (-0600)

 $ TZ=US/Central perl offset2local.pl Tue, 01 Jan 2013 16:05:53 -0600
Tue, Jan 01 2013 16:05:53 CST (-0600)

 $ perl offset2local.pl Tue, 01 Jan 2013 16:05:53 +0000
Tue, Jan 01 2013 08:05:53 PST (-0800)
&lt;/c&gt;&lt;p&gt;
[mod://Time::Local] is a core module.  The only reason I used [mod://POSIX] was to get the timezone abbreviation ("CST"); otherwise, that last step could have been easily done with just [doc://localtime] and [doc://sprintf]).
&lt;/p&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-22609"&gt;&lt;p align="right"&gt;
- [tye]&lt;tt&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/tt&gt;
&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1012285</field>
<field name="parent_node">
1012522</field>
</data>
</node>
