Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#!/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< timegm >; use POSIX qw< strftime >; my @Mons = qw< jan feb mar apr may jun jul aug sep oct nov dec >; 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)

Time::Local is a core module. The only reason I used POSIX was to get the timezone abbreviation ("CST"); otherwise, that last step could have been easily done with just localtime and sprintf).

- tye        


In reply to Re^3: Convert timezone using offset and posix perl module (easy) by tye
in thread Convert timezone using offset and posix perl module by adambot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (14)
    As of 2015-07-28 11:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (254 votes), past polls