Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Am I on the right track - timezones

Reply
Thread Tools

Am I on the right track - timezones

 
 
Jon
Guest
Posts: n/a
 
      09-28-2003
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,$isd st) =
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.
 
Reply With Quote
 
 
 
 
James E Keenan
Guest
Posts: n/a
 
      09-29-2003

"Jon" <> wrote in message
news: om...
> 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



 
Reply With Quote
 
 
 
 
Mothra
Guest
Posts: n/a
 
      09-29-2003
Hi Jon,

"Jon" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
Darren Dunham
Guest
Posts: n/a
 
      09-29-2003
Jon <> wrote:

> 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,$isd st) =
> localtime('1063620687');


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

timegm/localgm timelocal/localtime.

--
Darren Dunham
Unix System Administrator Taos - The SysAdmin Company
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >
 
Reply With Quote
 
Jon
Guest
Posts: n/a
 
      09-30-2003
"Mothra" <> wrote in message news:<3f76f5ff$>...
> [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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
insert track reference in mp3 track D@Z Computer Support 1 05-02-2008 03:32 PM
Old certification track vs new certification track. Barkley Hughes MCAD 5 11-12-2006 02:41 PM
Timezones Mark ASP .Net 3 04-05-2006 07:16 PM
Help with DateTime and TimeZones / DayLight savings time Ryan Ternier ASP .Net 1 10-14-2005 05:20 PM
Timezones for asp.net John Giblin ASP .Net 0 11-04-2003 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57