Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validator][ConstraintValidator] Safe fail on invalid timezones #34904

Merged
merged 1 commit into from Dec 13, 2019

Conversation

fancyweb
Copy link
Contributor

@fancyweb fancyweb commented Dec 9, 2019

Co-authored-by: Scott Dawson scott@loyaltycorp.com.au

Q A
Branch? 3.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets #33901
License MIT
Doc PR

Alternative to #33902.

I will explain why I think it is better this way:

  1. We set the timezone with the setter because it's 100% safe, it never fails. It fall backs to the default timezone if the provided timezone is not supported (as if we passed null, so the same behavior that always existed). We are therefore compatible with all edge cases.
  2. We don't validate the timezone with \DateTimeZone::listIdentifiers(). It only returns full identifiers like "Europe/Paris" but it doesn't take into account "numeric" identifiers such as "+08:00" which are perfectly valid. I added a test case to ensure we stay valid with this case. + some invalid identifiers for the native \IntlDateFormatter are valid with the polyfill that uses \DateTimeZone (eg : X). I don't think we can validate anything safely that will work reliably on both implementations.

@fancyweb
Copy link
Contributor Author

fancyweb commented Dec 9, 2019

Tests pass on my machine with both implem (polyfill and native) but fails on CI 😕 Looks like the good value is not returned when using UTC.

@yceruto yceruto added this to the 3.4 milestone Dec 9, 2019
@fancyweb
Copy link
Contributor Author

fancyweb commented Dec 9, 2019

Also we have to think of what is the goal here: display the passed date relatively to its timezone (the same datetime in all timezones must display the same thing basically). Can't we just always set the formatter to UTC, copy the passed value in a temp var and set the timezone to UTC, so we don't have to deal with this timezone stuff?

@fancyweb fancyweb force-pushed the validator-zulu-fix- branch 2 times, most recently from 9871424 to 7fbfa3c Compare December 10, 2019 07:55
@fancyweb
Copy link
Contributor Author

Can't we just always set the formatter to UTC, copy the passed value in a temp var and set the timezone to UTC, so we don't have to deal with this timezone stuff?

Looks like we can. It simplifies everything. I just pushed it.

@nicolas-grekas
Copy link
Member

rebase needed

Co-authored-by: Scott Dawson <scott@loyaltycorp.com.au>
@fancyweb
Copy link
Contributor Author

Rebased + tests pass. I modified the tests to strenghten them.

@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

nicolas-grekas added a commit that referenced this pull request Dec 13, 2019
…zones (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator][ConstraintValidator] Safe fail on invalid timezones

Co-authored-by: Scott Dawson <scott@loyaltycorp.com.au>

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #33901
| License       | MIT
| Doc PR        |

Alternative to #33902.

I will explain why I think it is better this way:

1. We set the timezone with the setter because it's 100% safe, it never fails. It fall backs to the default timezone if the provided timezone is not supported (as if we passed null, so the same behavior that always existed). We are therefore compatible with all edge cases.
2. We don't validate the timezone with `\DateTimeZone::listIdentifiers()`. It only returns full identifiers like "Europe/Paris" but it doesn't take into account "numeric" identifiers such as "+08:00" which are perfectly valid. I added a test case to ensure we stay valid with this case. + some invalid identifiers for the native `\IntlDateFormatter` are valid with the polyfill that uses `\DateTimeZone` (eg : `X`). I don't think we can validate anything safely that will work reliably on both implementations.

Commits
-------

3b1b994 [Validator][ConstraintValidator] Safe fail on invalid timezones
@nicolas-grekas nicolas-grekas merged commit 3b1b994 into symfony:3.4 Dec 13, 2019
@fancyweb fancyweb deleted the validator-zulu-fix- branch December 19, 2019 12:08
This was referenced Dec 19, 2019
This was referenced Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants