Changeset 8193


Ignore:
Timestamp:
17 Apr 2012, 11:35:01 (13 years ago)
Author:
uli
Message:

Display local datetime when using FormattedDateDisplayWidget? (or
derived versions like FriendlyDateWidget?) with datetimes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/widgets/datewidget.py

    r8152 r8193  
    1919A datewidget with customizable date format.
    2020"""
     21import pytz
    2122from datetime import datetime
     23from zope.component import queryUtility
    2224from zope.formlib.interfaces import ConversionError, IDisplayWidget
    2325from zope.formlib.textwidgets import DateWidget, DateDisplayWidget, escape
    2426from zope.formlib.widget import renderElement, CustomWidgetFactory
    2527from zope.interface import implements
     28from waeup.kofa.interfaces import IKofaUtils
     29from waeup.kofa.utils.helpers import to_timezone
     30
    2631
    2732#: A dictionary of supported date formats.
     
    130135
    131136    This is a display widget.
     137
     138    It can also be used for displaying datetimes. If used to display a
     139    datetime (not a date), the widget returns local datetime with
     140    timezone set according to KofaUtils.
    132141    """
    133142    date_format = '%Y-%m-%d'
     
    152161        if content == self.context.missing_value:
    153162            return ""
     163        if isinstance(content, datetime):
     164            # shift value to local timezone
     165            tz = pytz.utc
     166            utils = queryUtility(IKofaUtils)
     167            if utils is not None:
     168                tz = utils.tzinfo
     169            content = to_timezone(content, tz)
    154170        content = content.strftime(self.date_format)
    155171        return renderElement("span", contents=escape(content),
Note: See TracChangeset for help on using the changeset viewer.