-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add basic timezone support for form datetime input. #11517
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
Conversation
src/Database/Type/DateTimeType.php
Outdated
| * | ||
| * @return bool | ||
| */ | ||
| protected function _isEmpty($value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opening brace should be on a new line
src/Database/Type/DateTimeType.php
Outdated
| return PDO::PARAM_STR; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected 1 newline at end of file; 0 found
Codecov Report
@@ Coverage Diff @@
## 3.next #11517 +/- ##
============================================
+ Coverage 92.17% 92.46% +0.28%
- Complexity 13177 16245 +3068
============================================
Files 459 462 +3
Lines 33708 39672 +5964
============================================
+ Hits 31070 36682 +5612
- Misses 2638 2990 +352
Continue to review full report at Codecov.
|
|
looks this is only for showing , not converting request data from user |
|
Both, it converts correctly for display in the input form (dropdowns by default), and it converts back before validating/saving. |
|
do you think we can add calender option in future ?(non-Gregorian) |
|
how happen if someone remove hidden timezone field ? |
src/View/Widget/DateTimeWidget.php
Outdated
| * - `second` - Set to true to enable the seconds input. Defaults to false. | ||
| * - `meridian` - Set to true to enable the meridian input. Defaults to false. | ||
| * The meridian will be enabled automatically if you choose a 12 hour format. | ||
| * - `timezone` - A hidden field for timezone handling when working with UTC internally. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not just UTC really. Any timezone could be stored in the hidden input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point is that you store anything but UTC usually :) The UTC is the server timezone for DB storing it normalized, and the timezone is either passed in the array from user input (session data?) or coming from config by default (e.g. german based website using global gmt+1 output).
So the idea is UTC => GMT+1 for display => UTC for form handling.
Thus "working with UTC internally" - which seems to be the best practice when dealing with localized datetime across multiple timezones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree UTC is what we should recommend, but people will do all sorts of interesting things.
src/View/Widget/DateTimeWidget.php
Outdated
| 'templateVars' => [], | ||
| ]; | ||
|
|
||
| return $this->_select->render($options, $context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we making a select and not a hidden input? Seems like a hidden input is what we want here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. How would that look like here? Using a widget in a widget?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what $this->_select is as well. The WidgetRegistry stitches various widget dependencies together.
| $internalTimezone = date_default_timezone_get(); | ||
| if ($tz && $tz !== $internalTimezone) { | ||
| $time = $time->timezone($internalTimezone); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the time type need this kind of change as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think so, so far I only checked datetime. If anyone has the chance to help with other types, that would be more than welcome.
src/View/Widget/DateTimeWidget.php
Outdated
| $date = new DateTime('now', $timezone); | ||
| } elseif (is_int($value) || is_numeric($value)) { | ||
| $date = new DateTime('@' . $value); | ||
| $date = new DateTime('@' . $value, $timezone); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing $timezone here is redundant. From the PHP manual:
The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).
|
|
||
| $internalTimezone = date_default_timezone_get(); | ||
| if ($tz && $tz !== $internalTimezone) { | ||
| $time = $time->timezone($internalTimezone); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this change mean the timezone should be again set to default timezone before converting the object to string in toDatabase() method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My tests seemed to yield the right conversion, so maybe we do not? But I am happy to be proven wrong.
It is unused for unix timestmaps.
|
Made a few small fixes, but didn't add any tests. |
Skip hidden input generation if default output timezone is not set or explicitly disabled through widget data.
|
Anyone able to help out, getting this ready for 3.7 maybe? |
|
This pull request is stale because it has been open 30 days with no activity. Remove the |
Basic stab at #10877
Currently all forms are broken if you follow recommended UTC for DB and localized forms (e.g. UTC+1 Berlin time).
The time in forms is always behind upon transformation to output, and when you save it, it becomes even more behind as it converts it wrong a 2nd time.
Using a defaultOutputTimezone config (and optionally even a timezone dropdown maybe) you can have localized forms and keep the expected timezoned datetime.
If there is only one timezone (output), one can just use config here:
I kindly ask for help to finalize this for 3.6.