Am I on the right track - timezones

J

Jon

Hello,

I am trying to take a date (day/month/year) and time (hour:min) from
the user, and also a timezone which that date/time is based on. I
then need to pull all logs out of a database which happens on that
supplied date and time.

The log stores everything by its timestamp, which I take from time().
So all the timestamps should be based in UTC time. So first step I
need to do is take the supplied date/time and turn it into a UTC
timestamp.

I do that by setting the $ENV{TZ} to the timezone and then use
timegm(), I remembered to take away -1 from the month.

So I now have what I think is the UTC timestamp of the supplied
time/date. Now here is the problem, I thought I could reverse this,
and turn that timestamp back into the original date/time (in its
timezone).

Again I set the ENV{TZ} and this time use localtime() on the
timestamp. This would return a different date/time compared to the
original. An example of what code I used is as follows.

$ENV{TZ} = ':/usr/share/zoneinfo/Europe/Paris';
$localseconds = timegm(27,11,10,15,8,2003);
print "$localseconds\n";

(11 minutes past 10am, and 27 seconds on the 15th August 2003)

$localseconds is 1063620687.

$ENV{TZ} = ':/usr/share/zoneinfo/Europe/Paris';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime('1063620687');

Now, it returns 12:11:27 on the 15th. Which is 2 hours out of the
original date and time. Have I got the functions mixed up, or am I
just not on the right track here?

Thank you in advance for any help,

Jon.
 
J

James E Keenan

Jon said:
Hello,

I am trying to take a date (day/month/year) and time (hour:min) from
the user, and also a timezone which that date/time is based on. I
then need to pull all logs out of a database which happens on that
supplied date and time.

The log stores everything by its timestamp, which I take from time().
So all the timestamps should be based in UTC time. So first step I
need to do is take the supplied date/time and turn it into a UTC
timestamp.

The answer can probably be found in one of the following 3 modules from
CPAN:
Date::Format
Date::Calc
Date::Manip

jimk
 
M

Mothra

Hi Jon,

Jon said:
Hello,

I am trying to take a date (day/month/year) and time (hour:min) from
the user, and also a timezone which that date/time is based on. I
then need to pull all logs out of a database which happens on that
supplied date and time.

[snipped]
Please take a look at the DateTime project.
http://datetime.perl.org/
Thank you in advance for any help,

Jon.

Hope this helps

Mothra
 
D

Darren Dunham

Jon said:
I am trying to take a date (day/month/year) and time (hour:min) from
the user, and also a timezone which that date/time is based on. I
then need to pull all logs out of a database which happens on that
supplied date and time.
The log stores everything by its timestamp, which I take from time().
So all the timestamps should be based in UTC time. So first step I
need to do is take the supplied date/time and turn it into a UTC
timestamp.
I do that by setting the $ENV{TZ} to the timezone and then use
timegm(), I remembered to take away -1 from the month.

Uhh, no. If you want to feed in a timezone-offset time, then you want
timelocal.

What you get out of either one isn't really "UTC timestamps", it's
"seconds since the epoch". The gm vs local specifies the *input*. I
think your statement of "timestamps should be based in UTC time" is
confusing you.

Just pretend the timestamps are opaque and have nothing to do with GMT
or UTC or anything. You set the TZ and create the stamps with
timelocal, and you set the TZ and read the stamps with localtime.
$ENV{TZ} = ':/usr/share/zoneinfo/Europe/Paris';
$localseconds = timegm(27,11,10,15,8,2003);
print "$localseconds\n";
(11 minutes past 10am, and 27 seconds on the 15th August 2003)
$localseconds is 1063620687.
$ENV{TZ} = ':/usr/share/zoneinfo/Europe/Paris';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime('1063620687');

Expecting to have timegm and localtime be each others reverse does not
make sense to me.

timegm/localgm timelocal/localtime.
 
J

Jon

Mothra said:
[snipped]
Please take a look at the DateTime project.
http://datetime.perl.org/
Thank you in advance for any help,

Jon.

Hope this helps

Mothra

Thanks for that, DateTime seems just what I need. However, I noticed
a possible reason why stuff I was doing was not working. When I set
the TZ to the timezone, any other changes I make do not take effect.

$ENV{TZ} = ':/usr/share/zoneinfo/Europe/London';

$time = time();

($sec,$min,$hour,$day,$mon,$this_year,$wday,$yday,$isdst) =
localtime($time);
print "$hour:$min\n";

$ENV{TZ} = ':/usr/share/zoneinfo/Europe/Paris';

($sec,$min,$hour,$day,$mon,$this_year,$wday,$yday,$isdst) =
localtime($time);
print "$hour:$min\n";

So this should return 2 times, within an hour of each other, however
it returns the same time.

1:57
1:57

So I done some more digging, and run strace on the script, it turns
out it only opens the first zoneinfo file. I tried the same script on
another server, and it worked correctly. The one it failed on is
running Perl v5.8.0, on Linux kernel 2.4.20.

The working servers run a older version of Perl and of the kernel. I
did read about localtime caching the timezone, but didn't think that
happen now days.

Jon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top