2 Replies - 1042 Views - Last Post: 30 June 2016 - 01:50 AM

#1 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 253
  • View blog
  • Posts: 761
  • Joined: 24-November 08

Bug with DateTime Timezones?

Posted 29 June 2016 - 07:37 AM

Hi all.

Something I'm working on currently involves using different timezones for setting some webservice token dates. I was running a script that I suspected to return true and yet it returns false.

After some debugging I figured that it's happening AFTER i was setting the timezone, using DateTime's setTimezone method which seems to be causing all the strange behaviour.

Test script replicating the issue:

<?php

$past = new DateTime('now', new DateTimeZone('Europe/London'));

$future = new DateTime('now', new DateTimeZone('PST'));
$future->add(new DateInterval('PT1H')); // Add an hour

// Test 1
var_dump($past < $future); // returns true (correct)

// This seems to be causing the bug
$future->setTimezone(new DateTimeZone('PST')); // Set the timezone which it already was anyway

// Test 2
var_dump($past < $future); // This one returns false (incorrect)

// Test 3
var_dump($past->getTimestamp() < $future->getTimestamp()); // Timestamp dumps return true

// Test 4 (and now it works and returns true again??)
var_dump($past < $future);



What was strange is that I left the last var_dump in the script when my colleague suggested comparing the timestamps (test 3) - which then had a direct impact on the result of test 4.

Obviously if I remove $future->setTimezone(new DateTimeZone('PST')); it resolves the issue, but I wasn't initally setting it in the constructor, i was using this method which seems to be causing the problems.

Reproduced on php 7.0.4-7 and 5.5.34 and 5.6.5

Thoughts?

Is This A Good Question/Topic? 0
  • +

Replies To: Bug with DateTime Timezones?

#2 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Bug with DateTime Timezones?

Posted 29 June 2016 - 08:06 AM

Yea, there seems to be some sort of bug internally in the DateTime object, when it comes to timezones. Some sort of internal timestamp counter seems to be adjusted incorrectly when you set the timezone, so some things fail to work, while others don't.

Consider what happens when you print the ->format('U') value of the DateTime before and after setting the timezone.
var_dump($future->format('U'));
$future->setTimezone(new DateTimeZone('PST')); // Set the timezone which it already was anyway
var_dump($future->format('U'));


string '1467216208' (length=10)
string '1467187408' (length=10)



Clearly something isn't right, as the Unix timestamp should not change with the timezone change.

But if you call the ->getTimestamp() function right after the timezone change, it seems to correct whatever internal error is happening, and from then it works normally.
var_dump($future->format('U'));
$future->setTimezone(new DateTimeZone('PST')); // Set the timezone which it already was anyway
$future->getTimestamp();
var_dump($future->format('U'));


string '1467216301' (length=10)
string '1467216301' (length=10)



I've run into this before, and it's been around for a while I think. There is probably a bug report around somewhere, though I couldn't find it. (Not that I tried hard :P)
Was This Post Helpful? 2
  • +
  • -

#3 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 253
  • View blog
  • Posts: 761
  • Joined: 24-November 08

Re: Bug with DateTime Timezones?

Posted 30 June 2016 - 01:50 AM

How strange.

I wouldn't have expected the getTimestamp() method to do any kind of recalculations for the timestamp. I know nothing of C but it seems like it shouldn't be too much of a difficult fix if the getTimestamp method is "fixing" the timestamp - the same calculation could be made when setTimezone is being called?

In fact, why is setTimezone even touching the timestamp? surely it should remain a fixed value and the datetime worked out from that value with whatever timezone is being set?

Also odd that's it been around for a while with no fix.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1