ramonski
(Ramon Bartl)
May 19, 2023, 7:11pm
1
Hi everyone,
I just had a look into plone.dexterity.content.DexterityContent to see how the creation_date et al. are set and was wondering why it uses DateTime instead of datetime objects:
class DexterityContent(DAVResourceMixin, PortalContent, PropertyManager, Contained):
"""Base class for Dexterity content"""
...
def __init__(...):
...
now = DateTime()
self.creation_date = now
Now I am wondering if it makes sense at all to store datetime objects for our custom Dexterity contents or if we should just store DateTime objects there as well.
The reason why I ask is because I just get nuts with the timezones ...
Thanks for any insights.
Ramon
mekell
(me-kell)
May 19, 2023, 7:44pm
2
Following links might help:
https://5.docs.plone.org/develop/plone/misc/datetime.html
I have tried this but this will cause other problems, e.g
plone.app.layout.viewlets.content.isExpired
if base_hasattr(self.context, 'expires'):
return self.context.expires().isPast()
will fail.
And don't know about the indexing side effects.
Thanks all for your help! I am going to try this out first and will report back, because 4.1.1 has what seems to be the exact fix I need.
Changelog
4.2 (2017-04-26)
Add support for Python 3.6, drop support for Python 3.3.
4.1.1 (2016-04-30)
Support unpickling instances having a numeric timezone like +0430.
4.1 (2016-04-03)
Add support for Python 3.4 and 3.5.
Drop support for Python 2.6 and 3.2.
4.0.1 (2013-10-15)
Provide more backward compatible timezones. [vangheem]
petschki
(Peter Mathis)
May 22, 2023, 9:05am
3
Default plone.app.z3cform.converter returns datetime object and respects a default_timezone attribute if it exists:
:returns: Datetime in format `Y-m-d H:M`
:rtype: string
"""
if value is self.field.missing_value:
return ""
return (
f"{value.year:}-{value.month:02}-{value.day:02}T"
f"{value.hour:02}:{value.minute:02}"
)
def toFieldValue(self, value):
"""Converts from widget value to field.
:param value: Value inserted by datetime widget.
:type value: string
:returns: `datetime.datetime` object.
:rtype: datetime
"""
if not value:
return self.field.missing_value
ramonski
(Ramon Bartl)
May 23, 2023, 7:39pm
4
Thanks @petschki , I was aware of this code.
The headaches come more when handling both DateTime to datetime and comparing timezone naive with timezone aware objects.
We wrote an whole API to wrap things up and make it a bit easier to handle:
# -*- coding: utf-8 -*-
#
# This file is part of SENAITE.CORE.
#
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright 2018-2023 by it's authors.
# Some rights reserved, see README and LICENSE.
This file has been truncated. show original
Anyhow, we will just continue to use datetime objects in our Dexterity types and try to live with the other existing DateTime objects.
thet
(Johannes Raggam)
May 23, 2023, 8:33pm
5
I'd also just use datetime. The older DateTime should once be replaced with Python's own datetime.
These APIs/utilities might also help:
from datetime import date
from datetime import datetime
from datetime import timedelta
import logging
import os
import pytz
import time
DSTADJUST = "adjust"
DSTKEEP = "keep"
DSTAUTO = "auto"
MAX32 = int(2**31 - 1)
logger = logging.getLogger("plone.event")
def validated_timezone(timezone, fallback=None):
"""Validate a given timezone identifier. If a fallback is given, return it
This file has been truncated. show original
1 Like