Skip to content

Date Timezone unexpected clone, why? #18228

@gzhegow1991

Description

@gzhegow1991

Description

https://gyazo.com/28d32430a6bcb915755046e4aaca37b9

The following code:

<?php
$tz = new \DateTimeZone('UTC');

$format = 'H:i';

$dt = \DateTime::createFromFormat($format, '15:00', $tz);

var_dump($dt, $tz, $dt->getTimezone());
var_dump($dt->getTimezone() === $tz); // false, what?

Resulted in this output:

false

But I expected this output instead:

true

Once i saw how symfony checks is data was passed to array or gained from default values

It works like
($this->get($key, $default = $this) === $this) // value was default because $this exists only inside class scope

I want to warn programmer that his date is unable to parse timezone, so the date becomes uncomparable.
The case is about "date parser" ensures that "if i passed only time - i mean server timezone and maybe date should be used", but it can be developer mistake that is forget to ask user to sent correct date with his own timezone.

Aproximately it could be compared by timezone name, but when he pass exactly same name expected by format - he's doing all things good, so warning is useless. So the attention point can be, again, aproximately, moved to date format - like "you didnt ask for timezone, you get something unexpected".

Seriosly thought about dates is like floats, you can compare it safely only if you drop parts to same precision, and if you ensure about timezones/dst is correctly parsed. Unable to compare "2nd April" and "2nd April 15:00 BST" because:

  1. if you draw both on timeline - it takes different space size as periods (day vs hour, "uncomparable") but... if you make it same size - you can return -1, 0, 1 instead of "intersect left, intersect right, fully included" and so on, so we drop same parts then compare begin points
  2. first date has no timezone, so compare them in different time of year gives different result
  3. this way i came to requirement to check "what exactly parts of user date was parsed successfully" and "what was default"... Maybe we can safely drop for example microseconds when we compare only by seconds and thats not a problem. But, guess, my feelings joke with me

PHP Version

PHP 8.4.5

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions