About timezone implementation, both generally and in Moodle

About timezone implementation, both generally and in Moodle

by John Papaioannou -
Number of replies: 2
Yesterday I asked an offhand question related to the correct timezone implementation strategy (generally speaking), which (specifically speaking) if I am correct, isn't done properly in Moodle.

Hence this discussion: if someone can tell me where I 'm wrong, I 'd be grateful because I sure can't find a flaw in this logic myself:

Martin's reply to my question said:

> No, time() is definitely the absolute number of seconds since epoch, which
> is January 1 1970 00:00:00 GMT.

...and so we can use the return value of time() to timestamp an event in GMT. Here's why I don't think this is enough.

If a server's timezone is GMT+0000, then one day after epoch its time() function
would return 86400. However, at the exact same moment, a server with TZ GMT+0200
would return 86400 + 7200 for time(). This is of course obvious, as the GMT+02
server's clock wouldn't be one day (24 hours) but 26 hours after epoch.

So how can the number of seconds since epoch be "absolute" on any given machine?

On my home XP system I did a few tests (changing the timezone from the control panel), and time() returned different values when I did so. This HAS to indicate it's timezone-dependent (at least under Windows, but why should Linux be different?).

From the PHP docs on date():

date('O') returns "Difference to Greenwich time (GMT) in hours" in a string that looks like "+0200".

Wouldn't the correct solution be this?

$localtime = time();
$diff_from_gmt = intval(date('O'), $localtime); // May be negative

// Parse the number we have... remember that 2.5 hours after GMT
// will not be returned as +0250 but as +0230, so we cannot use
// 3600 * ($diff_from_gmt / 100) to offset the time

$diff_hours = intval($diff_from_gmt / 100);
$diff_mins = $diff_from_gmt % 100;

$gmt_time = $localtime - ($diff_hours * 60 + $diff_mins) * 60;

I 've been a bit liberal with the variables for the sake of clarity.

Where is the flaw in my reasoning? wide eyes

Average of ratings: -
In reply to John Papaioannou

Re: About timezone implementation, both generally and in Moodle

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Jon, please believe me, it works as it is. Read the documentation for time().

I even did a test just now, running this script:

<? echo time() ?>

on two machines ... one at GMT+8 and one at GMT.

Both returned the same number.

XP Home version may be buggy, or perhaps you need to restart the web server or the machine after a timezone change.
In reply to Martin Dougiamas

Re: About timezone implementation, both generally and in Moodle

by John Papaioannou -

It seems you are correct... blush

But I still don't get why it works this way on my machine. Maybe I 'm missing something. Anyway. I could do with some less timestamp manipulation.

Thanks for bothering! smile
Jon