Skip to content

Conversation

@dereuromark
Copy link
Member

@dereuromark dereuromark commented Dec 7, 2017

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:

	'App' => [
		'defaultLocale' => env('APP_DEFAULT_LOCALE', 'de_DE'),
		'defaultOutputTimezone' => env('APP_DEFAULT_OUTPUT_TIMEZONE', 'Europe/Berlin'),

I kindly ask for help to finalize this for 3.6.

@dereuromark dereuromark added this to the 3.6.0 milestone Dec 7, 2017
*
* @return bool
*/
protected function _isEmpty($value) {
Copy link
Contributor

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

return PDO::PARAM_STR;
}
}
}
Copy link
Contributor

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-io
Copy link

codecov-io commented Dec 7, 2017

Codecov Report

Merging #11517 into 3.next will increase coverage by 0.28%.
The diff coverage is 83.33%.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ Complexity Δ
src/View/Helper/FormHelper.php 96.13% <ø> (+0.3%) 431 <0> (+61) ⬆️
src/Database/Type/DateTimeType.php 97% <100%> (+0.22%) 53 <1> (+3) ⬆️
src/View/Widget/DateTimeWidget.php 96.39% <71.42%> (-1.69%) 77 <1> (+3)
src/Collection/functions.php 50% <0%> (-50%) 0% <0%> (ø)
src/basics.php 30.76% <0%> (-13.68%) 0% <0%> (ø)
src/I18n/functions.php 72.41% <0%> (-11.59%) 0% <0%> (ø)
src/Core/functions.php 78.5% <0%> (-9%) 0% <0%> (ø)
src/TestSuite/TestSuite.php 92.3% <0%> (-7.7%) 6% <0%> (ø)
src/ORM/Exception/PersistenceFailedException.php 95% <0%> (-5%) 10% <0%> (+8%)
src/Datasource/ResultSetDecorator.php 71.42% <0%> (-3.58%) 2% <0%> (ø)
... and 72 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 866a920...2c80a4e. Read the comment docs.

@saeideng
Copy link
Member

saeideng commented Dec 7, 2017

looks this is only for showing , not converting request data from user

@dereuromark
Copy link
Member Author

Both, it converts correctly for display in the input form (dropdowns by default), and it converts back before validating/saving.
Not yet sure about any edge cases yet though.
But seems BC so far, if you do not use any timezoning here.

@saeideng
Copy link
Member

saeideng commented Dec 7, 2017

do you think we can add calender option in future ?(non-Gregorian)

@saeideng
Copy link
Member

saeideng commented Dec 7, 2017

how happen if someone remove hidden timezone field ?

* - `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.
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

'templateVars' => [],
];

return $this->_select->render($options, $context);
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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);
}
Copy link
Member

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?

Copy link
Member Author

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.

$date = new DateTime('now', $timezone);
} elseif (is_int($value) || is_numeric($value)) {
$date = new DateTime('@' . $value);
$date = new DateTime('@' . $value, $timezone);
Copy link
Member

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);
Copy link
Member

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?

Copy link
Member Author

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.

@josegonzalez
Copy link
Member

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.
@markstory markstory modified the milestones: 3.6.0, 3.7.0 Apr 15, 2018
@dereuromark
Copy link
Member Author

Anyone able to help out, getting this ready for 3.7 maybe?
We could also look into https://github.com/JeanValJeann/cakephp-timezone and what they did.

@github-actions
Copy link

This pull request is stale because it has been open 30 days with no activity. Remove the stale label or comment on this issue, or it will be closed in 15 days

@github-actions github-actions bot added the stale label Sep 16, 2019
@github-actions github-actions bot closed this Oct 2, 2019
@othercorey othercorey deleted the 3.6-form-timezone branch December 16, 2019 04:42
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.

8 participants