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

Fix timezone bug in date field #2349

Merged
merged 2 commits into from Dec 15, 2019

Conversation

@pedroborges
Copy link

pedroborges commented Dec 12, 2019

This issue has been very annoying for quite some time. I'm working on an event calendar plugin and it will be almost impossible for it to work correctly without fixing this issue.

The cause for this bug is: let's say a date field in the content file contains 2019-12-09 10:00. PHP assumes that the server timezone is UTC and sends 2019-12-09T10:00:00+00:00 to the browser. Because dayjs receives the date including a timezone, it tries to be smart by offsetting the date with my system's timezone. Because I'm in Brazil (GMT-03:00) I see time as 07:00 in the date field. In some cases, you even see a different day. If I set the time to 15:00, It is saved correctly. When I refresh the page it shows 12:00 because dayjs offsets the date once again.

If I set the server timezone to be the same as in my system, it works fine. The problem would be solved if panel users were always in the same timezone. But we can do better and not try to guess panel users' timezone.

Describe the PR

The solution is to send dates without timezone to the browser in the /config/fields/date.php file. This changes makes the browser displays the exact same datetime as in the content file.

No change is required in the front-end because the date I see when hitting "save" is being stored correctly to the content file.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Fixed code style issues with CS fixer and composer fix
  • Added in-code documentation (if needed)
This issue has been very annoying for quite some time. I'm working on an event calendar plugin and it will be almost impossible for it to work correctly without fixing this issue.

The cause for this bug is: let's say a date field in the content file contains `2019-12-09 10:00`. PHP assumes that the server timezone is UTC and sends `2019-12-09T10:00:00+00:00` to the browser. Because dayjs receives the date including a timezone, it tries to be smart by offsetting the date with my system's timezone. Because I'm in Brazil (GMT-03:00) I see time as 07:00 in the date field. In some cases, you even see a different day. If I set the time to 15:00, It is saved correctly. When I refresh the page it shows 12:00 because dayjs offsets the date once again.

If I set the server timezone to be the same as in my system, it works fine. The problem would be solved if panel users were always in the same timezone. But we can do better and not try to guess panel users' timezone.

The solution: send dates without timezone to the browser in the [`/config/fields/date.php`](https://github.com/getkirby/kirby/blob/21ebebda6bdde9aee62140f81f658f0852ea4e60/config/fields/date.php#L70) file. This changes makes the browser displays the exact same datetime as in the content file.

No change is required in the front-end because the date I see when hitting "save" is being stored correctly to the content file.
@distantnative

This comment has been minimized.

Copy link
Contributor

distantnative commented Dec 15, 2019

@pedroborges You are a champ for figuring this out (and me being mislead by the buggy timezone Chrome extension)!

@distantnative distantnative merged commit c94664c into getkirby:develop Dec 15, 2019
2 checks passed
2 checks passed
Travis CI - Pull Request Build Passed
Details
coverage/coveralls Coverage remained the same at 85.09%
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.