Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Welcome to the Monastery
 
PerlMonks  

why are timegm and timelocal returning the same epoch seconds values

by kehansen (Novice)
on May 14, 2013 at 19:47 UTC ( #1033540=perlquestion: print w/ replies, xml ) Need Help??
kehansen has asked for the wisdom of the Perl Monks concerning the following question:

I have a script to post-process a data file that contains multiple timestamps in GMT. This script is to convert the timestamps from GMT to specific timezone equivalent. My logic is to convert the timestamp to epoch seconds, determine the seconds difference between GMT and local time, subtract this from the epoch seconds, and convert this back to a local timestamp. The problem I am seeing is that in trying to get the difference between GMT & local time the returned epoch second are the same. Here is my sub-routine to get the difference.

sub getGMDelta { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $ltime = timelocal($sec,$min,$hour,$mday,$mon,$year); $gtime = timegm($sec,$min,$hour,$mday,$mon,$year); $rv = $gtime - $ltime; return($rv); }

Comment on why are timegm and timelocal returning the same epoch seconds values
Download Code
Re: why are timegm and timelocal returning the same epoch seconds values
by vsespb (Hermit) on May 14, 2013 at 20:08 UTC
    For me this code works. Why do you think your system's timezone is not GMT+0 ?

      For me this code works. Why do you think your system's timezone is not GMT+0 ?

      Since the code is incomplete (  Undefined subroutine &main::timelocal called ) how is it possible that this code works for you?

        I googled for "perl timegm" and found that I need to
        use Time::Local;
        Perhaps OP use different module and that's the reason of problem?
Re: why are timegm and timelocal returning the same epoch seconds values
by Not_a_Number (Parson) on May 14, 2013 at 20:37 UTC

    Works for me too (with the addition of use Time::Local; at the top).

    However, I would simply the sub somewhat:

    sub getGMDelta { my @now = localtime; return timegm( @now ) - timelocal( @now ); }
Re: why are timegm and timelocal returning the same epoch seconds values
by karlgoethebier (Deacon) on May 14, 2013 at 20:40 UTC

    Your sub is OK. For me it returns 7200 - GMT+2.

    For calculating time delta you could take a look at Date::Calc and/or DateTime.

    Update:

    Perhaps calculating the time delta from the timestamps in your file(s), not from the current time? Or do i miss something?

    Update2:

    I'm still struggling a bit with this.

    As far as i understood your timezone is GMT.

    You have a file with some timestamps you think they are in another timezone, right?

    And you want to know the difference between this timestamps and GMT, right?

    So IMHO your sub needs the timestamps from your file as params...

    Update3:

    «why are timegm and timelocal returning the same epoch seconds values»

    Because your box is GMT?

    Update4:

    Just as a little addendum (assuming you are using bash):

    Karls-Mac-mini:monks karl$ date Di 14 Mai 2013 23:22:21 CEST Karls-Mac-mini:monks karl$ export TZ=UTC Karls-Mac-mini:monks karl$ date Di 14 Mai 2013 21:22:47 UTC Karls-Mac-mini:monks karl$ unset TZ Karls-Mac-mini:monks karl$ date Di 14 Mai 2013 23:23:11 CEST

    Update5:

    OK, this is the last one, i promise...

    #!/usr/bin/env perl # delta.pl use strict; use warnings + use Time::Local; print getGMDelta() . qq(\n); sub getGMDelta { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $ltime = timelocal($sec,$min,$hour,$mday,$mon,$year); $gtime = timegm($sec,$min,$hour,$mday,$mon,$year); $rv = $gtime - $ltime; return($rv); } __END__ Karls-Mac-mini:monks karl$ export TZ=UTC Karls-Mac-mini:monks karl$ ./delta.pl 0 Karls-Mac-mini:monks karl$ unset TZ Karls-Mac-mini:monks karl$ ./delta.pl 7200

    Please see also:

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: why are timegm and timelocal returning the same epoch seconds values
by kehansen (Novice) on May 14, 2013 at 21:35 UTC

    Thank you for the responses. Here is more information. I do have "use Time::Local" at the top of my main script. My host is running Solaris 10 and the timezone is set for "US/Pacific". The file I am processing contains multiple(24) timestamps that are in GMT time and we have a customer who wants these times converted to local time.

      1. Try extract this subroutine from you script and run it outside of your script.

      2. Is your TZ variable set?

      3. Try run with env TZ=US/Pacific

      4. Try run system tools (like 'date) which report time to make sure they recognize timezone correctly

      Update2:

      use DateTime; use DateTime::TimeZone; my $dt = DateTime->new( year => 2013, month => 4, day => 15, hour => 12, minute => 45, second => 47, time_zone => 'Europe/Berlin', ); my $tz = DateTime::TimeZone->new( name => 'US/Pacific' ); my $offset = $tz->offset_for_datetime($dt); print qq($offset\n); print DateTime::TimeZone->offset_as_string( $offset ) . qq(\n); print qq($dt->hour\n); $dt->set_time_zone( 'US/Pacific' ); print qq($dt->hour\n);

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

Re: why are timegm and timelocal returning the same epoch seconds values
by Laurent_R (Priest) on May 14, 2013 at 22:35 UTC

    The number of seconds since the epoch is independent of the time zone. What depends on the time zone is the way this number of se conds is interpreted.

    Maybe this session under ste Perl debugger will clarify what happens:

    DB<1> print scalar gmtime time Tue May 14 22:27:51 2013 DB<2> print scalar localtime time Wed May 15 00:27:59 2013 DB<3> print time 1368570496 DB<4> @c = gmtime 1368570496 DB<5> x @c 0 16 1 28 2 22 3 14 4 4 5 113 6 2 7 133 8 0 DB<6> @d = localtime 1368570496 DB<7> x @d 0 16 1 28 2 0 3 15 4 4 5 113 6 3 7 134 8 1 DB<8>

    My apologies if I did not really answer to your question, I am not entirely sure what your problem is.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-04-18 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (461 votes), past polls