Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Convert GMT timestamp to EST/EDT

by gtk (Acolyte)
on Jul 25, 2015 at 05:45 UTC ( [id://1136264]=perlquestion: print w/replies, xml ) Need Help??

gtk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a simple way to convert GMT timestamp to EST/EDT(any timezone) timestamp without using new additional Perl Libraries? Sample of my file(Time in GMT and I wanted to view the time in EDT or EST):
 
20150619-17:30:43.616, 26
20150619-17:30:33.442, 23
20150619-17:30:40.376, 26
20150619-17:30:38.863, 26
20150619-17:30:56.936, 26
20150619-17:30:34.952, 24
20150619-17:30:45.889, 26
20150619-17:30:53.940, 23
20150619-17:30:51.154, 25
20150619-17:30:48.699, 26

Replies are listed 'Best First'.
Re: Convert GMT timestamp to EST/EDT (Time::Local)
by tye (Sage) on Jul 25, 2015 at 06:10 UTC

    Time::Local is ancient and included in the Perl core:

    #!/usr/bin/perl -w use strict; use Time::Local 'timegm'; my $stamp = '20150619-17:30:43.616'; my @nn = $stamp =~ /([.][0-9]+|[0-9]{2})/g; my $f = pop @nn; # Fractional seconds my $cc = shift @nn; # Century my $yy = shift @nn; # Year $nn[0] -= 1; # Make month 0-based (ugh) unshift @nn, "$cc$yy"; # Put 4-digit year back in front. $ENV{TZ} = "US/Eastern"; @nn = localtime( timegm( reverse @nn ) ); $nn[4]++; # Make month 1-based (ugh) $nn[5] += 1900; # (ugh) $stamp = sprintf "%d%02d%02d-%02d:%02d:%02d%s", @nn[reverse 0..5], $f; print "$stamp\n"; __END__ 20150619-13:30:43.616

    - tye        

      :) Time::Piece in perls v5.9.5-v5.23.1
      ## @nn = localtime( timegm( reverse @nn ) ); ##$nn[4]++; # Make month 1-based (ugh) ##$nn[5] += 1900; # (ugh) ##$stamp = sprintf "%d%02d%02d-%02d:%02d:%02d%s", use Time::Piece qw/ localtime ; $stamp = localtime( timegm( reverse @nn ) )->strptime('%Y%m%d-%H:%M:% +S').$f;
        This only works if your local time zone happens to be EST/EDT.
        "The more you know!" *stars* --edit wow.. rough crowd.
      Wooooow.. Thanks a lot tye. It worked.
Re: Convert GMT timestamp to EST/EDT ( DateTime::Format::Strptime set_time_zone( 'America/New_York' )
by Anonymous Monk on Jul 25, 2015 at 08:45 UTC
    #!/usr/bin/perl -- use strict; use warnings; use DateTime::Format::Strptime; my $timestamp = q{20150619-17:30:43.616}; my $fmt = DateTime::Format::Strptime->new( pattern => q{%Y%m%d-%T.%3N}, time_zone => 'UTC', ); my $dt = $fmt->parse_datetime( $timestamp ); printf "%20s %s\n", $dt->time_zone_long_name, $fmt->format_datetime( $ +dt ); $dt->set_time_zone( 'EST5EDT' ); printf "%20s %s\n", $dt->time_zone_long_name, $fmt->format_datetime( $ +dt ); $dt->set_time_zone( 'EST' ); printf "%20s %s\n", $dt->time_zone_long_name, $fmt->format_datetime( $ +dt ); $dt->set_time_zone( 'America/New_York' ); printf "%20s %s\n", $dt->time_zone_long_name, $fmt->format_datetime( $ +dt ); __END__ UTC 20150619-17:30:43.616 EST5EDT 20150619-13:30:43.616 EST 20150619-12:30:43.616 America/New_York 20150619-13:30:43.616
      Thanks a lot. By the way I think DateTime::Format::Strptime module is not coming with the Perl default package.
Re: Convert GMT timestamp to EST/EDT
by ambrus (Abbot) on Jul 27, 2015 at 13:55 UTC

    Despite that you're asking for "without using new additional Perl Libraries", I'd suggest using the Date-Manip library.

    There's one complication. Date-Manip won't handle the fractional seconds for you, other than ignoring it when parsing a date. If you don't need to keep the fractional seconds, then this should work:

    use warnings; use 5.016; use Date::Manip::Date; my$db = Date:: Manip::Date->new(undef, ["setdate" => "now,utc"]); # parse as UTC date +s while (<>) { chomp; my($datestr, $count) = split /,/; my$d = $db->new; if (my $e = $d->parse($datestr)) { warn qq(cannot parse date: $datestr); } else { $d->convert("America/New_York"); say $d->printf("%Y%m%d-%H:%M:%S %Z"); # print in similar forma +t, but with timezone name } }

    If you also have to handle the fractional seconds, then either use a different module that handles them, or copy the fractional seconds by hand, like this.

    use warnings; use 5.016; use Date::Manip::Date; my$db = Date::Manip::Date->new(undef, [q(setdate) => q(now,utc)]); while (<>) { /^([-0-9:]{17})(\.[0-9]{3}),(.*)/ or die qq(cannot parse input lin +e); my($datestr, $frac, $count) = ($1, $2, $3); my$d = $db->new; if (my $e = $d->parse($datestr)) { warn qq(cannot parse date: $datestr); } else { $d->convert(q(America/New_York)); say $d->printf(q(%Y%m%d-%H:%M:%S)) . $frac . $d->printf(q( %Z) +); } }

    Output from the latter code follows.

    20150619-13:30:43.616 EDT 20150619-13:30:33.442 EDT 20150619-13:30:40.376 EDT 20150619-13:30:38.863 EDT 20150619-13:30:56.936 EDT 20150619-13:30:34.952 EDT 20150619-13:30:45.889 EDT 20150619-13:30:53.940 EDT 20150619-13:30:51.154 EDT 20150619-13:30:48.699 EDT

    Update: for similar questions, see also GMT to PST format

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1136264]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found