Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-04-18 02:41:13

Aaron Jacobs
New Member
Registered: 2006-04-18
Posts: 2

Offset calculation bug in tz_offset()

I was looking at the logs for my site running Textpattern 4.0.3 tonight, and noticed that all of the dates were wrong — instead of the latest hits showing as 4/17, they were showing as 4/16. I thought this might be related to the change in DST recently. But after some digging, I discovered the problem.

In lib/txplib_misc.php the following line is used to calculate the server’s offset from GMT:

<pre>
<code> $serveroffset = gmmktime(0,0,0) – mktime(0,0,0); $offset = $gmtoffset – $serveroffset;
</code>
</pre>

That checks how many seconds from the epoch it was at midnight GMT today, and the same for the local time and then takes their difference. That’s fine in theory, but what if today is different for the two time zones? That’s the case for 7 hours per day in my case, and so it was taking the difference in seconds since the epoch between midnight on 2006-04-18 GMT and midnight at 2006-04-17 PDT. That’s not cool!

I have created a patch to “fix” this problem. In actuality, a real fix would be to stop calculating all this junk ourselves and start relying on a well-tested library. Anyway, this should yield correct results all but possibly two days per year when the DST switch happens and things get funky. But that’s a lot better than being wrong for 7 hours every day. I’m really not sure why I didn’t notice this issue before — it’s a big, nasty one.

<pre>
<code> —- txplib_misc.php.bak 2006-04-17 21:37:21.000000000 -0500 +++ txplib_misc.php 2006-04-17 21:37:21.000000000 -0500 -883,14 +883,23

// ——————————————————————————————- // Calculate the offset between the server local time and the -// user’s selected time zone +// user’s selected time zone. Make sure to specify a specific +// date (e.g. today in the server local time) when subtracting +// the difference in midnight timestamps so that we don’t have +// a problem when it is a different date in GMT than in local +// time. function tz_offset() { global $gmtoffset, $is_dst;

- $serveroffset = gmmktime(0,0,0) – mktime(0,0,0); - $offset = $gmtoffset – $serveroffset; + $date = getdate(); + $year = $date[‘year’]; + $month = $date[‘mon’]; + $day = $date[‘mday’];

+ $serveroffset = gmmktime(0, 0, 0, $month, $day, $year) – mktime(0, 0, 0, $month, $day, $year); + $offset = $gmtoffset – $serveroffset; + return $offset + ($is_dst ? 3600 : 0); }

</code>
</pre>

Offline

#2 2006-04-18 03:57:44

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Offset calculation bug in tz_offset()

Thanks for reporting the bug and providing a patch, but it’s a bit late I’m afraid: the problem has already been encountered and patched (so it will appear in the next release).

Offline

#3 2006-04-18 04:05:03

Aaron Jacobs
New Member
Registered: 2006-04-18
Posts: 2

Re: Offset calculation bug in tz_offset()

No problem, I’m just glad it has been fixed. I should say that I still stand by my implied remark that it is dumb to be doing all of this by hand — surely some well-tested library should be relied upon instead. From a bit of reading it seems that maybe the problem is that PHP didn’t formerly have time zone support, but does now in version 5. If that’s the case, then maybe at the very least the new time zone aware built-in functions could be utilized on servers with PHP 5.

Offline

#4 2006-04-18 06:20:32

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Offset calculation bug in tz_offset()

From a bit of reading it seems that maybe the problem is that PHP didn’t formerly have time zone support, but does now in version 5.

That’s correct.

Personally, I don’t know how reliable the new stuff works in practice, which would have bearing on whether incorporating it in Txp will happen at this point. Alex has looked through various libraries and didn’t find anything that solved more problems than it created. I don’t know/remember specifics, he’d have to say.

Offline

#5 2006-04-18 07:38:35

zem
Developer Emeritus
From: Melbourne, Australia
Registered: 2004-04-08
Posts: 2,579

Re: Offset calculation bug in tz_offset()

If that’s the case, then maybe at the very least the new time zone aware built-in functions could be utilized on servers with PHP 5.

Sure, we’d welcome a patch. Just bear in mind everything we do has to work on a bog-standard PHP 4.3 install.


Alex

Offline

Board footer

Powered by FluxBB