Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confusion about time()

Status
Not open for further replies.

Kirsle

Programmer
Jan 21, 2006
1,179
US
Hey, I have this code that I wrote a few years back. Basically what it does is, given a time zone and a value from time() it can return the correct date/time for the time zone requested.

So for example, in some web blog software when it saves an entry, it would save the entry's time as the current value of time(), and then when being viewed by a logged-in user who set their time zone on their profile, it would take the epoch time value saved with the post and display it in the user's own local time.

The code works, but I'm a bit confused as to why it works.

Code:
sub timeFormat {
   my ($format,$epoch) = @_;

   # Get the time zone.
   my $tz = $root->{conf}->{site}->{timezone};
   if ($root->{auth}) {
      $tz = $root->{users}->{$root->{me}}->{timezone};
   }

   use Time::Format qw(time_format);
   use Time::Local;
   use Time::Zone;

   # Get GMT Time.
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime ($epoch);
   my $gm = Time::Local::timelocal ($sec,$min,$hour,$mday,$mon,$year);
   $gm = $epoch unless defined $tz;

   # Offset the time.
   if (defined $tz) {
      my $offset = tz_offset ($tz);
      $gm += $offset;
   }

   # Format the time.
   return time_format ($format,$gm);
}

So for example, given the same value of time() and wanting EDT and PDT, PDT returns a time that's 3 hours behind EDT, like it should.

I started to wonder if time() itself was dependent on the time zone, and that if this code was moved to another server in a different time zone if that would mess everything up. So I had a friend of mine on the west coast tell me what time() currently is, and the number he got was the same number I got on the east coast. So I've concluded that time() will return the same number for a given moment regardless of the server's time zone.

So, if time() is always synchronized in all time zones, shouldn't time() be something like GMT? But apparently it's not. In the code above I get gmtime($epoch), and then convert the results of that back into an epoch time number, and right now the new number is 14400 seconds off (4 hours away from GMT, which is what my time zone, EDT, is).

So... why does this work? It seems like taking gmtime() and converting it back through Time::Local would get the same number you started with. The code seems to work but I just don't get how and it's kinda confusing.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
I'd like to know where this came from
Code:
my $tz = $root->{conf}->{site}->{timezone};
   if ($root->{auth}) {
      $tz = $root->{users}->{$root->{me}}->{timezone};
   }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Ah.

$root is the "root" data structure for the particular content management system that this came from.

$root->{conf}->{site}->{timezone} is the timezone for the site itself (used when the viewer isn't logged in and therefore it defaults to the "server time zone").

$root->{auth} is 1 if the user is logged in, and $root->{me} is the user's login name.

$root->{users}->{ $root->{me} }->{timezone} would be the time zone variable on the user's profile.

So some example values:
Code:
$root->{conf}->{site}->{timezone} = 'EDT';
$root->{auth} = 1;
$root->{me} = 'kirsle';
$root->{users}->{kirsle}->{timezone} = 'PDT';

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top