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

Handle timezone in publication fields (effective and expires) #1192

Merged
merged 7 commits into from
Feb 13, 2025

Conversation

cekk
Copy link
Member

@cekk cekk commented Aug 9, 2021

This could fix plone/volto#2597

The failing test is because also if i customize env var, i can't save a DateTime value in that field with the wanted timezone.

Anyone have an idea on how to fix it? @buchi ?

@cekk cekk requested review from buchi and tisto August 9, 2021 14:43
@mister-roboto
Copy link

@cekk thanks for creating this Pull Request and help improve Plone!

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass.

Whenever you feel that the pull request is ready to be tested, either start all jenkins jobs pull requests by yourself, or simply add a comment in this pull request stating:

@jenkins-plone-org please run jobs

With this simple comment all the jobs will be started automatically.

Happy hacking!

@tisto
Copy link
Member

tisto commented Aug 24, 2021

@jenkins-plone-org please run jobs

2 similar comments
@tisto
Copy link
Member

tisto commented Aug 31, 2021

@jenkins-plone-org please run jobs

@cekk
Copy link
Member Author

cekk commented Oct 26, 2021

@jenkins-plone-org please run jobs

@netlify
Copy link

netlify bot commented Apr 7, 2023

Deploy Preview for plone-restapi canceled.

Name Link
🔨 Latest commit 7c89fb6
🔍 Latest deploy log https://app.netlify.com/sites/plone-restapi/deploys/6682ae32afa2140007f988a8

@avoinea
Copy link
Member

avoinea commented Apr 7, 2023

Holy mother of... this is here for 2 years and nobody took care of it? 😄

I can confirm @cekk 's conclusions from here plone/volto#2597 (comment)

How to test and reproduce:

  1. Start plone backend with TZ env
docker run -it --rm -p 8080:8080 -e TZ="Europe/Berlin" plone/plone-backend
  1. In Volto publish a News items with publishing date and time after a half an hour
  2. In Volto add a listing block showing all news before today
  3. The News should not be listed yet, but it is.
  4. Play with the Publishing Date until the News item disappear from the listing block
  5. Check the portal_catalog entry and you'll notice the EffectiveDate is actually UTC - 2 😱

is_publication_field = self.field.interface == IPublication
if is_publication_field:
# because IPublication datamanager strips timezones
tzinfo = pytz.timezone(default_timezone())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cekk While this is way better than assuming UTC always, it can still have issues if the server has different timezone than the one from registry, UTC for example (the default behavior of plone/plone-backend Docker image).

And I think @sneridagh can confirm that their installations doesn't provide the TZ environment variable to docker containers, thus UTC by default. While the default timezone registry entry when creating a new Plone instance is Europe/Berlin.

How to test:

docker run -it --rm -p 8080:8080 -e SITE=Plone plone/plone-backend bash

git clone https://github.com/plone/plone.restapi.git
cd plone.restapi
git checkout handle_tz_in_publication_fields
cd /app
bin/pip install -e ./plone.restapi --no-deps
./docker-entrypoint.sh start

Now add a News Item with Effective Date via Volto and check portal_catalog entry.

@erral
Copy link
Member

erral commented Aug 28, 2023

We have been hit by this too, and I can confirm that with this changes the original datetime as entered in Volto interface is correctly saved in Plone.

We configure our Zope instance passing the TZ = Europe/Madrid environment variable.

In all cases here, I have Europe/Madrid configured as a default timezone in Plone.

Item saved with the following datetime through Volto: 2023-08-28 10:00:00

Previous scenario (without this branch):

>>> news = app.Plone.komunikazioa.albisteak['albistea-2023-08-28-10-00-00']
>>> news
<FolderishNewsItem at /Plone/komunikazioa/albisteak/albistea-2023-08-28-10-00-00>
>>> value = news.effective()
>>> value
DateTime('2023/08/28 08:00:00 GMT+2')
>>> value = value.asdatetime()
>>> value
datetime.datetime(2023, 8, 28, 8, 0, tzinfo=<StaticTzInfo 'GMT+2'>)

Current scenario (with this branch)

>>> news = app.Plone.komunikazioa.albisteak['albistea-2023-08-28-11-00']
>>> news
<FolderishNewsItem at /Plone/komunikazioa/albisteak/albistea-2023-08-28-11-00>
>>> news.effective()
DateTime('2023/08/28 10:00:00 GMT+2')
>>> news.effective().asdatetime()
datetime.datetime(2023, 8, 28, 10, 0, tzinfo=<StaticTzInfo 'GMT+2'>)

I have made a test without adding the TZ environment variable in the instance configuration and the behavior is the following:

Previous scenario (without this branch)

>>> news = app.Plone.komunikazioa.albisteak['albistea-2023-08-28-12-00']
>>> news
<FolderishNewsItem at /Plone/komunikazioa/albisteak/albistea-2023-08-28-12-00>
>>> value = news.effective()
>>> value
DateTime('2023/08/28 08:00:00 GMT+2')
>>> value.asdatetime()
datetime.datetime(2023, 8, 28, 8, 0, tzinfo=<StaticTzInfo 'GMT+2'>)

Current scenario (with this branch):

>>> news = app.Plone.komunikazioa.albisteak['albistea-2023-08-28-12-00']
>>> newd
<FolderishNewsItem at /Plone/komunikazioa/albisteak/albistea-2023-08-28-12-00>
>>> value = news.effective()
>>> value
DateTime('2023/08/28 10:00:00 GMT+2')
>>> value = value.asdatetime()
>>> value
datetime.datetime(2023, 8, 28, 10, 0, tzinfo=<StaticTzInfo 'GMT+2'>)

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@stevepiercy
Copy link
Contributor

@tisto @cekk @erral @avoinea @sneridagh can we add this to the Volto Team meeting agenda? This bug has been hanging around for almost 3 years now without resolution. The tests are broken, too.

@sneridagh
Copy link
Member

@davisagli @jensens @thet related with current dates handling. Aside from the new fixes that we are working on already, we probably will need this patch for now. Are you able to take a look?

Copy link
Member

@jensens jensens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this nails it. Checking specifically for IPublication is wrong, ugly and is a recipe for harmful future side effects.

The dates of effective/expiration/created/modified are all still Zope DateTime instances - best would to get rid of those and convert to python datetime. But this is lots of work and even if it was planned for 6.0 nobody took time and energy to make it happen. Maybe for 7.0?

One can not rely on it being a Zope Datetime: It could be a Python datetime as well.

To convert Zope DateTime to a Python datetime we have a function pydt in plone.event.utils.
The right way for the serializer is to check if there is a Zope DateTime returned and if so, use pydt to convert it. It's missing_zone parameter passes in a pytz zone to be used, if no timezone is present.

I propose to change this to a generic solution to deal with Zope DateTime instead of covering just one special case here.

@claytonc
Copy link
Member

Same problem volto version 16.32.1 and plone version 6.0.13, I had to make this correction because the content was not being published on the selected date and time.

Verified

This commit was signed with the committer’s verified signature.
davisagli David Glick
…ion_fields

Verified

This commit was signed with the committer’s verified signature.
davisagli David Glick

Verified

This commit was signed with the committer’s verified signature.
davisagli David Glick
@davisagli
Copy link
Member

@jenkins-plone-org please run jobs

@davisagli
Copy link
Member

davisagli commented Feb 12, 2025

@cekk @avoinea @erral @stevepiercy @jensens I think I (finally) have this in a state where it can be merged now.

I also tested in a docker container per @avoinea's instructions (thanks, helpful!)

@jensens This is in fact a special case needed for the fields from IPublication, because they use the DCFieldProperty adapter which expects timezone-naive datetimes. It would be cleaner to fix it there, and maybe we can in Plone 6.2 or 7, but I don't think it can be done in a backwards-compatible way. So I'll accept this as a pragmatic workaround in the meantime.

I plan to merge this tomorrow unless someone finds that it is not working correctly.

@davisagli davisagli merged commit 6e2ab6b into main Feb 13, 2025
24 checks passed
@davisagli davisagli deleted the handle_tz_in_publication_fields branch February 13, 2025 05:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Wrong Time displayed in DateTimeWidget
10 participants