Problem/Motivation

The plain and custom datetime formatters aren't ignoring the time portion on date-only fields. This is appearing in Pacific/Auckland again. The default formatter appears to work as intended.

Proposed resolution

Fix it.

Add test coverage to make sure this doesn't happen again.

Remaining tasks

User interface changes

API changes

Data model changes

Original Report

Hi,

I have two date only fields on a node. Both are displayed with a custom php date format. They are both showing the date on day after what the field is actually set to. So a date that is set to April 4 is displaying April 5.

The sites timezone is set to pacific/auckland. Users may not set their own timezones. I have set the custom field display widgets to also use the pacific/auckland time zone. (bugs also manifests when no time zone is selected, because I assume it still defaults to pacific/auckland)

I added a printout for the hour in 24hour format to see what was happening and I see the time is set to 00 hours the next day.

According to this: https://www.drupal.org/node/2859021#comment-11978151
when date only fields are loaded into timestamps for display they are assigned a time of 1200 hours, but I think since my site is set to Pacific/Auckland time which is +12 hours, that becomes 0000hours the next day.

I believe this could be a consequence of "Remove any timezone conversion and time handling from date-only fields." related to the bug in this ticket
https://www.drupal.org/node/2739290

As I workaround I am setting the custom display widgets time zone to UTC(+0), which will get the dates to display correctly.

Comments

Pharitz created an issue. See original summary.

mpdonadio’s picture

Version: 8.2.7 » 8.4.x-dev
Priority: Major » Normal
Issue summary: View changes
Issue tags: +Needs tests
Parent issue: » #2739290: UTC+12 is broken on date only fields
Related issues: +#2830094: Deprecate and remove usages of datetime_date_default_time().

Confirmed :(

Not sure what we botched in #2739290: UTC+12 is broken on date only fields, but it looks like the custom and plain formatters aren't honoring the logic to ignore the time-portion for date-only formats. Also not sure why the timezone loop in those tests isn't catching this.

Downgrading this, as I don't think this qualifies as a Major.

As a workaround, I think the best action is to create a custom date format under admin/config/regional/date-time, use the Default formatter, and then pick the custom format you just made. That seems to work for me.

mpdonadio’s picture

Status: Active » Needs review
StatusFileSize
new412 bytes

DateTimeFieldTest::testDateField() tests all the formatters in the zone loop. UTC+12 and UTC+13 are in there. I have no clue what is up. And, the attached passes locally.

mpdonadio’s picture

StatusFileSize
new470 bytes
new2.22 KB

This works with manual testing. I am really confused about what is going on with the test coverage and how this slipped through.

Still still needs test adjustments.

The last submitted patch, 4: 2861986-test-only.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 4: 2861986-04.patch, failed testing.

mpdonadio’s picture

Status: Needs work » Needs review
StatusFileSize
new3.14 KB
new4.9 KB

This should come up green, but I can't figure out a way to make the plain formatters fail w/o the fix.

@Pharitz, can you test this to see if it fixes your problem?

The last submitted patch, 7: 2861986-test-only.patch, failed testing.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Tests have been converted to phpunit.

I can't figure out a way to make the plain formatters fail w/o the fix.

Is it an issue with the plain formatter?

jofitz’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new5.07 KB

Re-rolled.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

> I can't figure out a way to make the plain formatters fail w/o the fix.

Me and @kim.pepper did! :)

See #3009377-68: Replace drupal_get_user_timezone with a date_default_timezone_get() and an event listener for the explanation why it didn't fail in HEAD. Came up with the same fix as this did over there after a lot of painful debugging.

I guess we can close this as a duplicate.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
savkaviktor16@gmail.com’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new4.81 KB

Re-rolled

joachim’s picture

Status: Needs review » Needs work
+++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php
@@ -55,7 +55,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
+    $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();
     return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);

We've now got two ternary operators for the $timezone in two different places, which makes it hard to read.
Is the existing ternary still needed -- can $timezone still be '' with the new code?

If so, the logic should all be combined into one part of code (though avoid nesting ternaries for readability please...).

If not, it should be removed.

mpdonadio’s picture

Status: Needs work » Closed (duplicate)

Tentatively closing this as a dup, since the same code is essentially also in #3009377: Replace drupal_get_user_timezone with a date_default_timezone_get() and an event listener, which is currently RTBC.

I do agree with the comment in #17, though, but I think we can make that a novice followup task once ^^ lands.