Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > time zone offset calc with localtime and gmtime

Reply
Thread Tools

time zone offset calc with localtime and gmtime

 
 
Leendert Bottelberghs
Guest
Posts: n/a
 
      04-04-2005
Hi there,

I want to calculate the time zone offset (as an integer), and I don't want
to depend on other modules.
I want to verify if the following code is correct:

<code>
my @lctime = localtime();
my @gmtime = gmtime();
# index 2 represent the hours
# index 8 represent isdst (daylight saving time boolean (0/1))

$tzoffset = (($lctime[2] - $lctime[8])-($gmtime[2] - $gmtime[8]))%24);

</code>

I figured that if DST is applied locally, the clock is one hour ahead of
"normal" GMT (which hasn't DST applied), so this hour has to be deducted
from the time difference. If the GMT has DST set on, this hour has to be
added to the time difference, hence the double deduction.

Can anyone tell me if my my assumptions are correct?

TIA,

-leendert bottelberghs


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      04-04-2005
Leendert Bottelberghs wrote:
> I want to calculate the time zone offset (as an integer), and I don't want
> to depend on other modules.


Why not?

> I figured that if DST is applied locally, the clock is one hour ahead of
> "normal"


That may be true in many countries, but can you rely on it?

> If the GMT has DST set on,


AFAIK, that's never happening.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Leendert Bottelberghs
Guest
Posts: n/a
 
      04-04-2005
On Mon, 04 Apr 2005 17:34:31 +0200, Gunnar Hjalmarsson wrote:
>> I want to calculate the time zone offset (as an integer), and I don't
>> want to depend on other modules.

>
> Why not?


For different reasons:
- I use this in a module that will be used in mod_perl. I want to keep the
number of loaded modules as concise as possible, because of the server
memory overhead; - I want to be able to distribute this module without
people having to install other modules;


>> I figured that if DST is applied locally, the clock is one hour ahead
>> of "normal"

>
> That may be true in many countries, but can you rely on it?


I truly don't know. That's part of the reason I posted this Anyone...?

>> If the GMT has DST set on,

>
> AFAIK, that's never happening.


I thought so. Thanx for your response,

-leendert bottelberghs
 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      04-04-2005
On Mon, 04 Apr 2005 17:07:04 +0200, "Leendert Bottelberghs"
<(E-Mail Removed)> wrote:

>Hi there,
>
>I want to calculate the time zone offset (as an integer), and I don't want
>to depend on other modules.
>I want to verify if the following code is correct:
>
><code>
>my @lctime = localtime();
>my @gmtime = gmtime();
># index 2 represent the hours
># index 8 represent isdst (daylight saving time boolean (0/1))
>
>$tzoffset = (($lctime[2] - $lctime[8])-($gmtime[2] - $gmtime[8]))%24);
>
></code>
>
>I figured that if DST is applied locally, the clock is one hour ahead of
>"normal" GMT (which hasn't DST applied), so this hour has to be deducted
>from the time difference. If the GMT has DST set on, this hour has to be
>added to the time difference, hence the double deduction.
>
>Can anyone tell me if my my assumptions are correct?
>
>TIA,
>
>-leendert bottelberghs
>


$tzoffset, if you are going to make it an integer should be in units
of 1 second, not hours. There a places in the world where the offset
is not a whole hour. (e.g., Newfoundland, Tehran, Kabul, Rangoon,
Kathmandu, Darwin)
 
Reply With Quote
 
Mothra
Guest
Posts: n/a
 
      04-04-2005

"Leendert Bottelberghs" <(E-Mail Removed)> wrote in
message news(E-Mail Removed) nowledge.net...
> I truly don't know. That's part of the reason I posted this Anyone...?


You may want to bring this up on the DateTime mailing list
http://www.velocityreviews.com/forums/(E-Mail Removed).

Mothra


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-05-2005
Leendert Bottelberghs wrote:

> - I use this in a module that will be used in mod_perl. I want to keep the
> number of loaded modules as concise as possible, because of the server
> memory overhead; - I want to be able to distribute this module without
> people having to install other modules;


Well, is it safe to use the modules that come bundled with perl, so
you might as well use them.
-Joe
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-05-2005
Leendert Bottelberghs wrote:

> I want to calculate the time zone offset (as an integer)


The international standard for time zone offset is a five-character
string. A plus or minus sign, two digits for hours, two digits for
minutes.

Date: Mon, 04 Apr 2005 17:07:04 +0200
NNTP-Posting-Date: Mon, 4 Apr 2005 15:06:45 +0000 (UTC)
US-Pacific-Date: Mon, 04 Apr 2005 08:06:45 -0800 (PDT)

The last two digits are not always "00".

grep ' [-+][0-9][0-9][1-9]' /usr/lib/perl5/site_perl/5.8.3/Date/Manip.pm
#"nst -0330 ". # Newfoundland Standard nst=North Sumatra +0630
"nft -0330 ". # Newfoundland
"ndt -0230 ". # Newfoundland Daylight
"it +0330 ". # Iran
"ist +0530 ". # Indian Standard
"nst +0630 ". # North Sumatra nst=Newfoundland Std -0330
"acst +0930 ". # Australian Central Standard
"cast +0930 ". # Central Australian Standard
"acdt +1030 ". # Australian Central Daylight
"cadt +1030 ". # Central Australian Daylight

-Joe
 
Reply With Quote
 
Leendert Bottelberghs
Guest
Posts: n/a
 
      04-05-2005
On Tue, 05 Apr 2005 00:35:32 -0700, Joe Smith wrote:
> Leendert Bottelberghs wrote:
>
>> - I use this in a module that will be used in mod_perl. I want to keep the
>> number of loaded modules as concise as possible, because of the server
>> memory overhead; - I want to be able to distribute this module without
>> people having to install other modules;

>
> Well, is it safe to use the modules that come bundled with perl, so
> you might as well use them.


You're absolutely right. Which standard module would you recommend?

-leendert
 
Reply With Quote
 
Leendert Bottelberghs
Guest
Posts: n/a
 
      04-05-2005
On Tue, 05 Apr 2005 00:46:45 -0700, Joe Smith wrote:
> Leendert Bottelberghs wrote:
>> I want to calculate the time zone offset (as an integer)

> The international standard for time zone offset is a five-character
> string. A plus or minus sign, two digits for hours, two digits for
> minutes.


Youre right. So I have to use POSIX to be able to calculate the difference
in seconds. I now have the following to calculate and format the time
offset:

<code>
# calculate the time difference in seconds;
my $secoffset = timelocal(localtime()) - timelocal(gmtime());

# translate it to minutes and apply the DLT difference
my $minoffset = ($secoffset / 60) + ((gmtime)[8] - (localtime)[8])*60;

# translate it to "hour-format", so that 90 will be 130,
# and -90 will be -130
my $tzoffset = int($minoffset/60)*100 +
($minoffset/abs($minoffset))*($minoffset%60);

# apply final formatting, including +/- sign and 4 digits.
my $tzstr = sprintf "%+05d", $tzoffset;

</code>

Maybe I'll use Date::Manip after all.

-leendert bottelberhs
 
Reply With Quote
 
Mothra
Guest
Posts: n/a
 
      04-05-2005

"Leendert Bottelberghs" <(E-Mail Removed)> wrote in
message news(E-Mail Removed) nowledge.net...

> Maybe I'll use Date::Manip after all.
>
> -leendert bottelberhs


Yor are reinventing what we already have done in the DateTime Project

use strict;
use warnings;
use diagnostics;
use DateTime;


my $dt = DateTime->now(time_zone => 'America/Chicago');

print $dt->offset();


printed results
me.pl
-18000

Hope this helps





 
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
ruby calc.rb or just calc.rb Kaye Ng Ruby 7 07-28-2010 05:21 AM
Time zone offset and daylight saving easleydp@gmail.com Ruby 4 02-01-2006 10:36 PM
update: timezone offset calc and date formatting Leendert Bottelberghs Perl Misc 0 04-08-2005 10:32 AM
Time problem....mktime(), gmtime(), localtime() KW C Programming 1 12-08-2004 03:20 PM
Converting for localtime() or gmtime() to time() Stephen Perl 1 05-19-2004 03:51 PM



Advertisments