Skip to content
Browse files

work around bug in DateTime.strftime that always normalizes displayed…

… date to the system timezone, regardless of the timezone on the DateTime value itself. This bug is not likely to get fixed in Zope DateTime upstream soon (long-standing), so Products.CMFPlone.i18nl10n.ulocalized_time is subject to triggering it -- here that function is wrapped with a local copy that does the right thing with the timezone by using datetime.datetime.strftime without broken magic of DateTime.strftime.
  • Loading branch information...
1 parent cbe6a86 commit 994224f5130e36c54046dd51024d0dd4b61221cb @seanupton committed
Showing with 34 additions and 4 deletions.
  1. +33 −2 plone/app/event/base.py
  2. +1 −2 plone/app/event/browser/event_view.py
View
35 plone/app/event/base.py
@@ -1,6 +1,8 @@
import pytz
from DateTime import DateTime
from Products.CMFCore.utils import getToolByName
+from Products.CMFPlone import i18nl10n
+from Products.CMFPlone.i18nl10n import ulocalized_time as orig_ulocalized_time
from datetime import date
from datetime import datetime
from datetime import timedelta
@@ -192,10 +194,10 @@ def DT(dt):
@param dt: python datetime instance
"""
-
tz = default_timezone(getSite())
if isinstance(dt, datetime):
- tz = validated_timezone(dt.tzname(), tz)
+ zone_id = getattr(dt.tzinfo, 'zone', tz)
+ tz = validated_timezone(zone_id, tz)
return DateTime(dt.year, dt.month, dt.day,\
dt.hour, dt.minute, dt.second, tz)
elif isinstance(dt, date):
@@ -248,3 +250,32 @@ def guess_date_from(datestr, context=None):
return
return pytz.timezone(default_timezone(context)).localize(dateobj)
+
+
+def DT2dt(v):
+ dt = v
+ if isinstance(v, date):
+ dt = datetime(*v.timetuple()[:3])
+ elif isinstance(v, DateTime):
+ dt = v.asdatetime()
+ tz = dt.tzinfo
+ if not tz:
+ return dt
+ else:
+ tz = dt.tzinfo
+ return tz.localize(datetime(*dt.timetuple()[:7]))
+
+
+_strftime = lambda v, fmt: DT2dt(v).strftime(fmt)
+
+
+class PatchedDateTime(DateTime):
+
+ def strftime(self, fmt):
+ return _strftime(self, fmt)
+
+
+def ulocalized_time(time, *args, **kwargs):
+ """Corrects for DateTime bugs doing wrong thing with timezones"""
+ wrapped_time = PatchedDateTime(time)
+ return orig_ulocalized_time(wrapped_time, *args, **kwargs)
View
3 plone/app/event/browser/event_view.py
@@ -1,9 +1,8 @@
-from Products.CMFPlone.i18nl10n import ulocalized_time
from Products.Five.browser import BrowserView
from plone.event.interfaces import IEventAccessor, IRecurrenceSupport
from plone.event.utils import is_same_day, is_same_time
-from plone.app.event.base import DT
+from plone.app.event.base import DT, ulocalized_time
def prepare_for_display(context, start, end, whole_day):

0 comments on commit 994224f

Please sign in to comment.
Something went wrong with that request. Please try again.