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

Re: Re: How to get TZ difference?

by sgifford (Prior)
on Jun 28, 2003 at 16:56 UTC ( #269902=note: print w/replies, xml ) Need Help??

in reply to Re: How to get TZ difference?
in thread How to get TZ difference?

This doesn't work when localtime and gmtime are on different days (ie, in zone GMT+4 between 8:00pm and 11:59pm). Here's a fixed version:
#!/usr/bin/perl @local=(localtime); @gmt=(gmtime); $diff=$gmt[2]-$local[2]; if ($gmt[5] > $local[5] || $gmt[7] > $local[7]) { $diff += 24; } elsif ($gmt[5] < $local[5] || $gmt[7] < $local[7]) { $diff -= 24; }

Replies are listed 'Best First'.
Re: Re: Re: How to get TZ difference?
by sgifford (Prior) on Jun 28, 2003 at 17:55 UTC
    This is a little shorter and more confusing.
    @local=(localtime(time+$off_h*60*60)); @gmt=(gmtime(time+$off_h*60*60)); $diff=$gmt[2]-$local[2] + ($gmt[5] <=> $local[5] || $gmt[7] <=> $local[7])*24;

      Thanks for your idea. But let me ask you what is $off_h here? And I found that correct result occurs when I subtract $gmt[2] from $local[2]:

      $diff=$local[2]-$gmt[2] + ($gmt[5] <=> $local[5] || $gmt[7] <=> $local[7])*24;


Re: Re: Re: How to get TZ difference?
by hsweet (Pilgrim) on Jun 28, 2003 at 22:51 UTC
    thanks for the correction

    Time flies like an arrow, fruit flies like banannas

Re^3: How to get TZ difference?
by Anonymous Monk on Sep 08, 2014 at 09:42 UTC
    you forgot to compare the months in case when we have last day of the month, around midnight it will return inccorect results
      perl -e 'print ( ( (localtime)[2] - (gmtime)[2] + 12 ) % 24 - 12 )' across midnight, end of month, etc without all those pesky conditionals. Works because perl has a useful definition of modulo operator (%) for negative operands, but beware this can be compromised in certain situations. See perlop#Multiplicative-Operators.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://269902]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (1)
As of 2022-10-01 14:47 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (0 votes). Check out past polls.