[Checkins] SVN: z3ext.formatter/trunk/ Predefine date, time, datetime formats, do not use locale formats
Nikolay Kim
fafhrd91 at gmail.com
Mon Aug 10 07:27:28 EDT 2009
Log message for revision 102624:
Predefine date,time,datetime formats, do not use locale formats
Changed:
U z3ext.formatter/trunk/CHANGES.txt
U z3ext.formatter/trunk/setup.py
U z3ext.formatter/trunk/src/z3ext/formatter/README.txt
U z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py
U z3ext.formatter/trunk/src/z3ext/formatter/config.py
D z3ext.formatter/trunk/src/z3ext/formatter/config.txt
U z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml
U z3ext.formatter/trunk/src/z3ext/formatter/dformatter.py
U z3ext.formatter/trunk/src/z3ext/formatter/dtformatter.py
U z3ext.formatter/trunk/src/z3ext/formatter/expression.py
U z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py
U z3ext.formatter/trunk/src/z3ext/formatter/interfaces.py
U z3ext.formatter/trunk/src/z3ext/formatter/tests.py
A z3ext.formatter/trunk/src/z3ext/formatter/timeformatter.py
U z3ext.formatter/trunk/src/z3ext/formatter/vocabulary.py
-=-
Modified: z3ext.formatter/trunk/CHANGES.txt
===================================================================
--- z3ext.formatter/trunk/CHANGES.txt 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/CHANGES.txt 2009-08-10 11:27:27 UTC (rev 102624)
@@ -2,6 +2,12 @@
CHANGES
=======
+1.4.0 (Unreleased)
+------------------
+
+- Predefine date,time,datetime formats, do not use locale formats
+
+
1.3.0 (2009-07-05)
------------------
Modified: z3ext.formatter/trunk/setup.py
===================================================================
--- z3ext.formatter/trunk/setup.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/setup.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -65,7 +65,7 @@
'zope.app.appsetup',
'zope.app.pagetemplate',
'zope.app.publisher',
- 'z3c.pt',
+ 'z3c.pt',
],
extras_require = dict(test=['zope.app.testing',
'zope.testing',
Modified: z3ext.formatter/trunk/src/z3ext/formatter/README.txt
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/README.txt 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/README.txt 2009-08-10 11:27:27 UTC (rev 102624)
@@ -9,14 +9,10 @@
<product-config z3ext.formatter>
timezone UTC
- timezoneFormat 2
- principalTimezone true
</product-config>
Values for timezoneFormat are:
1: No timezone
- 2: Number of hours
- 3: Timezone name
We need register controlpanel configlet
@@ -95,11 +91,11 @@
>>> print page.render(request, now=dt)
<html>
<body>
- 1/1/07 12:00 AM
- Jan 1, 2007 12:00:00 AM
- January 1, 2007 12:00:00 AM +000
- Monday, January 1, 2007 12:00:00 AM +000
- Jan 1, 2007 12:00:00 AM
+ 01/01/07 12:00 AM
+ Jan 01, 2007 12:00:00 AM
+ January 01, 2007 12:00:00 AM +0000
+ Monday, January 01, 2007 12:00:00 AM UTC
+ Jan 01, 2007 12:00:00 AM
</body>
</html>
@@ -109,11 +105,11 @@
>>> print page.render(request, now=datetime(2007, 1, 1, 0, 0))
<html>
<body>
- 1/1/07 12:00 AM
- Jan 1, 2007 12:00:00 AM
- January 1, 2007 12:00:00 AM +000
- Monday, January 1, 2007 12:00:00 AM +000
- Jan 1, 2007 12:00:00 AM
+ 01/01/07 12:00 AM
+ Jan 01, 2007 12:00:00 AM
+ January 01, 2007 12:00:00 AM +0000
+ Monday, January 01, 2007 12:00:00 AM UTC
+ Jan 01, 2007 12:00:00 AM
</body>
</html>
@@ -126,81 +122,15 @@
>>> print page.render(request, now=dt)
<html>
<body>
- 12/31/06 4:00 PM
- Dec 31, 2006 4:00:00 PM
- December 31, 2006 4:00:00 PM -800
- Sunday, December 31, 2006 4:00:00 PM -800
- Dec 31, 2006 4:00:00 PM
+ 12/31/06 04:00 PM
+ Dec 31, 2006 04:00:00 PM
+ December 31, 2006 04:00:00 PM -0800
+ Sunday, December 31, 2006 04:00:00 PM PST
+ Dec 31, 2006 04:00:00 PM
</body>
</html>
-Now we can change timezone format to 3 (Timezone name)
-
- >>> configlet.timezoneFormat = 3
-
- >>> print page.render(request, now=dt)
- <html>
- <body>
- 12/31/06 4:00 PM
- Dec 31, 2006 4:00:00 PM US/Pacific
- December 31, 2006 4:00:00 PM -800
- Sunday, December 31, 2006 4:00:00 PM US/Pacific
- Dec 31, 2006 4:00:00 PM US/Pacific
- </body>
- </html>
-
-
-We also can redefine timezone for principal if we use principalTimezone true
-
- >>> from pytz import timezone
- >>> from zope import interface, component
- >>> class IPrincipal(interface.Interface):
- ... pass
-
- >>> class Principal:
- ... interface.implements(IPrincipal)
- ...
- ... def __init__(self, id):
- ... self.id = id
- ... self.groups = []
-
- >>> @component.adapter(IPrincipal)
- ... @interface.implementer(interface.common.idatetime.ITZInfo)
- ... def getTimezone(prin):
- ... if prin.id == 'user1':
- ... return timezone('Europe/Paris')
- ... elif prin.id == 'user2':
- ... return timezone('Asia/Almaty')
- >>> component.provideAdapter(getTimezone)
-
- >>> request.setPrincipal(Principal('user1'))
- >>> print page.render(request, now=dt)
- <html>
- <body>
- 1/1/07 1:00 AM
- Jan 1, 2007 1:00:00 AM Europe/Paris
- January 1, 2007 1:00:00 AM +100
- Monday, January 1, 2007 1:00:00 AM Europe/Paris
- Jan 1, 2007 1:00:00 AM Europe/Paris
- </body>
- </html>
-
- >>> request.setPrincipal(Principal('user2'))
- >>> print page.render(request, now=dt)
- <html>
- <body>
- 1/1/07 6:00 AM
- Jan 1, 2007 6:00:00 AM Asia/Almaty
- January 1, 2007 6:00:00 AM +600
- Monday, January 1, 2007 6:00:00 AM Asia/Almaty
- Jan 1, 2007 6:00:00 AM Asia/Almaty
- </body>
- </html>
-
- >>> request.setPrincipal(None)
-
-
fancyDatetime formatter
-----------------------
@@ -280,8 +210,8 @@
>>> print datepage.render(request, today=d)
<html>
<body>
- Jan 1, 2007
- 1/1/07
+ Jan 01, 2007
+ 01/01/07
</body>
</html>
@@ -291,7 +221,7 @@
>>> from z3ext.formatter.utils import getFormatter
>>> formatter = getFormatter(request, 'dateTime', 'full')
>>> formatter.format(dt)
- u'Sunday, December 31, 2006 4:00:00 PM US/Pacific'
+ u'Sunday, December 31, 2006 04:00:00 PM PST'
We will get FormatterNotDefined if formatter is unknown
@@ -321,6 +251,31 @@
FormatterNotDefined: unknown
+Time formatter
+--------------
+
+ >>> datepage = ZPTPage()
+ >>> datepage.pt_edit(u'''
+ ... <html>
+ ... <body>
+ ... <tal:block tal:content="formatter:time:options/time" />
+ ... <tal:block tal:content="formatter:time,short:options/time" />
+ ... </body>
+ ... </html>''', 'text/html')
+
+ >>> t = datetime(2007, 1, 1, 10, 34, 03)
+ >>> t
+ datetime.datetime(2007, 1, 1, 10, 34, 3)
+
+ >>> print datepage.render(request, time=t)
+ <html>
+ <body>
+ 10:34:03 AM
+ 10:34 AM
+ </body>
+ </html>
+
+
Custom formatter
================
Modified: z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/chameleonexpr.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-"""
+"""
$Id: 2007-12-12 12:27:02Z fafhrd $
"""
Modified: z3ext.formatter/trunk/src/z3ext/formatter/config.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/config.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/config.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -39,18 +39,4 @@
except LookupError:
timezone = u'US/Pacific'
- principalTimezone = config.get('principalTimezone', u'true').lower()
- if principalTimezone == 'true':
- principalTimezone = True
- else:
- principalTimezone = False
-
- try:
- timezoneFormat = vocabulary.timezonesOptions.getTermByToken(
- config.get('timezoneFormat', u'2')).value
- except LookupError:
- timezoneFormat = 2
-
IFormatterConfiglet['timezone'].default = timezone
- IFormatterConfiglet['timezoneFormat'].default = timezoneFormat
- IFormatterConfiglet['principalTimezone'].default = principalTimezone
Deleted: z3ext.formatter/trunk/src/z3ext/formatter/config.txt
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/config.txt 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/config.txt 2009-08-10 11:27:27 UTC (rev 102624)
@@ -1,62 +0,0 @@
-config.py - load configuration from zope.conf <product-config z3ext.formatter>
-
-By default it uses this settings
-
-<product-config z3ext.formatter>
- timezone UTC
- timezoneFormat 2
- principalTimezone true
-</product-config>
-
-Usually initFormatter() function is colled during IDatabaseOpenedEvent event,
-we simply call it directly:
-
- >>> from z3ext.formatter.config import initFormatter
- >>> initFormatter(None)
-
-Now we can get IFormatterConfiglet utility
-
- >>> from zope.component import getUtility
- >>> from zope.interface.verify import verifyObject
- >>> from z3ext.formatter.interfaces import IFormatterConfiglet
-
- >>> IFormatterConfiglet['timezone'].default
- u'US/Pacific'
-
- >>> IFormatterConfiglet['timezoneFormat'].default
- 2
-
- >>> IFormatterConfiglet['principalTimezone'].default
- True
-
-
-Now let's emulate <product-config>
-
- >>> from zope.app.appsetup import product
- >>> product._configs['z3ext.formatter'] = {
- ... 'timezone': u'UTC', 'timezoneFormat': '2', 'principalTimezone': 'false'}
-
- >>> initFormatter(None)
-
- >>> IFormatterConfiglet['timezone'].default
- u'UTC'
-
- >>> IFormatterConfiglet['timezoneFormat'].default
- 2
-
- >>> IFormatterConfiglet['principalTimezone'].default
- False
-
-
-If we supply wrong values config will use default values
-
- >>> product._configs['z3ext.formatter'] = {
- ... 'timezone': u'wrong', 'timezoneFormat': 'wrong'}
-
- >>> initFormatter(None)
-
- >>> IFormatterConfiglet['timezone'].default
- u'US/Pacific'
-
- >>> IFormatterConfiglet['timezoneFormat'].default
- 2
Modified: z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/configure.zcml 2009-08-10 11:27:27 UTC (rev 102624)
@@ -23,6 +23,9 @@
<adapter name="date"
factory=".dformatter.DateFormatterFactory" />
+ <adapter name="time"
+ factory=".timeformatter.TimeFormatterFactory" />
+
<adapter name="dateTime"
factory=".dtformatter.DatetimeFormatterFactory" />
Modified: z3ext.formatter/trunk/src/z3ext/formatter/dformatter.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/dformatter.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/dformatter.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -16,12 +16,12 @@
$Id$
"""
-
+from datetime import date
from zope import interface, component
-from zope.interface.common.idatetime import ITZInfo
+from zope.component import getUtility
from zope.publisher.interfaces.http import IHTTPRequest
-from interfaces import IFormatter, IFormatterFactory
+from interfaces import IFormatter, IFormatterFactory, IFormatterConfiglet
class DateFormatter(object):
@@ -33,18 +33,20 @@
except:
self.tp = 'medium'
- self.formatter = request.locale.dates.getFormatter('date', self.tp)
-
def format(self, value):
- return self.formatter.format(value)
+ if not isinstance(value, date):
+ return value
+ configlet = getUtility(IFormatterConfiglet)
+ return value.strftime(str(getattr(configlet, 'date_'+self.tp)))
+
class DateFormatterFactory(object):
component.adapts(IHTTPRequest)
interface.implements(IFormatterFactory)
def __init__(self, request):
self.request = request
-
+
def __call__(self, *args, **kw):
return DateFormatter(self.request, *args)
Modified: z3ext.formatter/trunk/src/z3ext/formatter/dtformatter.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/dtformatter.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/dtformatter.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -16,7 +16,7 @@
$Id$
"""
-from pytz import utc, timezone
+from pytz import timezone
from datetime import datetime
from zope import interface, component
@@ -36,34 +36,24 @@
except:
self.tp = 'medium'
- self.request = request
- self.formatter = request.locale.dates.getFormatter('dateTime', self.tp)
-
def format(self, value):
- formatter = self.formatter
+ if not isinstance(value, datetime):
+ return value
configlet = getUtility(IFormatterConfiglet)
- tz = None
- if configlet.principalTimezone:
- tz = ITZInfo(self.request.principal, None)
- if tz is None:
- tz = timezone(configlet.timezone)
-
+ tz = timezone(configlet.timezone)
if value.tzinfo is None:
value = datetime(value.year, value.month, value.day, value.hour,
- value.minute, value.second, value.microsecond, utc)
+ value.minute, value.second, value.microsecond, tz)
value = value.astimezone(tz)
- if configlet.timezoneFormat == 3:
- if self.tp in ('medium', 'full'):
- formatter.setPattern(
- formatter.getPattern().replace('z', '').strip())
- formatted = formatter.format(value)
- return u'%s %s'%(formatted, tz.zone)
+ format = '%s %s'%(
+ getattr(configlet, 'date_'+self.tp),
+ getattr(configlet, 'time_'+self.tp))
- return formatter.format(value)
+ return unicode(value.strftime(str(format)))
class DatetimeFormatterFactory(object):
@@ -72,6 +62,6 @@
def __init__(self, request):
self.request = request
-
+
def __call__(self, *args, **kw):
return DatetimeFormatter(self.request, *args)
Modified: z3ext.formatter/trunk/src/z3ext/formatter/expression.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/expression.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/expression.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -41,7 +41,7 @@
super(FormatterExpression, self).__init__(
name, pathExpr, engine, traverser=simpleTraverse)
-
+
def __call__(self, econtext):
request = removeAllProxies(econtext.request)
Modified: z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/fancydatetime.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -21,10 +21,9 @@
from zope import interface, component
from zope.i18n import translate
from zope.component import getUtility
-from zope.interface.common.idatetime import ITZInfo
from zope.publisher.interfaces.http import IHTTPRequest
-from interfaces import _, IFormatter, IFormatterFactory, IFormatterConfiglet
+from interfaces import IFormatter, IFormatterFactory, IFormatterConfiglet
class FancyDatetimeFormatter(object):
@@ -40,70 +39,45 @@
self.formatter = request.locale.dates.getFormatter('dateTime', self.tp)
def format(self, value):
- formatter = self.formatter
+ if not isinstance(value, datetime):
+ return value
configlet = getUtility(IFormatterConfiglet)
- tz = None
- if configlet.principalTimezone:
- tz = ITZInfo(self.request.principal, None)
- if tz is None:
- tz = timezone(configlet.timezone)
+ tz = timezone(configlet.timezone)
if value.tzinfo is None:
value = datetime(value.year, value.month, value.day, value.hour,
- value.minute, value.second, value.microsecond, utc)
+ value.minute, value.second, value.microsecond, tz)
value = value.astimezone(tz)
- timezoneFormat = configlet.timezoneFormat
-
d1 = datetime.now(utc).date()
d2 = value.astimezone(utc).date()
delta = d1 - d2
- oldpattern = formatter.getPattern()
- if ':ss' in oldpattern:
- formatter.setPattern(oldpattern.replace(':ss', '').strip())
-
if delta.days == 0:
- pattern = formatter.getPattern()
- pos = pattern.find('h')
- if pos < 0:
- pos = pattern.find('H')
+ format = str(getattr(configlet, 'time_'+self.tp))
- formatter.setPattern(pattern[pos:])
value = translate(
u'Today at ${value}', 'z3ext.formatter',
- mapping={'value': formatter.format(value)})
- formatter.setPattern(oldpattern)
+ mapping={'value': value.strftime(format)})
return value
if delta.days == 1:
- pattern = formatter.getPattern()
- pos = pattern.find('h')
- if pos < 0:
- pos = pattern.find('H')
+ format = str(getattr(configlet, 'time_'+self.tp))
- formatter.setPattern(pattern[pos:])
value = translate(
u'Yesterday at ${value}', 'z3ext.formatter',
- mapping={'value': formatter.format(value)})
- formatter.setPattern(oldpattern)
+ mapping={'value': value.strftime(format)})
return value
- if timezoneFormat == 3:
- if self.tp in ('full',):
- formatter.setPattern(
- formatter.getPattern().replace('z', '').strip())
- formatted = formatter.format(value)
- formatter.setPattern(oldpattern)
- return u'%s %s'%(formatted, tz.zone)
+ format = '%s %s'%(
+ getattr(configlet, 'date_'+self.tp),
+ getattr(configlet, 'time_'+self.tp))
- value = formatter.format(value)
- formatter.setPattern(oldpattern)
- return value
+ return unicode(value.strftime(str(format)))
class FancyDatetimeFormatterFactory(object):
@@ -112,6 +86,6 @@
def __init__(self, request):
self.request = request
-
+
def __call__(self, *args, **kw):
return FancyDatetimeFormatter(self.request, *args)
Modified: z3ext.formatter/trunk/src/z3ext/formatter/interfaces.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/interfaces.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/interfaces.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -25,12 +25,54 @@
class FormatterNotDefined(Exception):
""" """
+
class FormatterExpressionError(Exception):
""" """
class IFormatterConfiglet(interface.Interface):
+ """ formatter configlet schema """
+ date_short = schema.TextLine(
+ title = _(u'Short date format'),
+ default = u'%m/%d/%y',
+ required = True)
+
+ date_medium = schema.TextLine(
+ title = _(u'Medium date format'),
+ default = u'%b %d, %Y',
+ required = True)
+
+ date_long = schema.TextLine(
+ title = _(u'Long date format'),
+ default = u'%B %d, %Y',
+ required = True)
+
+ date_full = schema.TextLine(
+ title = _(u'Full date format'),
+ default = u'%A, %B %d, %Y',
+ required = True)
+
+ time_short = schema.TextLine(
+ title = _(u'Short time format'),
+ default = u'%I:%M %p',
+ required = True)
+
+ time_medium = schema.TextLine(
+ title = _(u'Medium time format'),
+ default = u'%I:%M:%S %p',
+ required = True)
+
+ time_long = schema.TextLine(
+ title = _(u'Long time format'),
+ default = u'%I:%M:%S %p %z',
+ required = True)
+
+ time_full = schema.TextLine(
+ title = _(u'Full time format'),
+ default = u'%I:%M:%S %p %Z',
+ required = True)
+
timezone = schema.Choice(
title = _(u'Timezone'),
description = _(u'Portal timezone.'),
@@ -38,20 +80,13 @@
vocabulary = vocabulary.timezones,
required = False)
- timezoneFormat = schema.Choice(
- title = _(u'Timezone format'),
- description = _(u'Timezone format'),
- default = 3,
- vocabulary = vocabulary.timezonesOptions,
- required = False)
+ #principalTimezone = schema.Bool(
+ # title = _(u'Use principal timezone'),
+ # description = _(u'Render datetime with user selected timezone.'),
+ # default = True,
+ # required = False)
- principalTimezone = schema.Bool(
- title = _(u'Use principal timezone'),
- description = _(u'Render datetime with user selected timezone.'),
- default = True,
- required = False)
-
class IFormatterFactory(interface.Interface):
""" formatter factory """
@@ -64,3 +99,7 @@
def format(value):
""" format value """
+
+
+class IStaticFormatter(interface.Interface):
+ """ """
Modified: z3ext.formatter/trunk/src/z3ext/formatter/tests.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/tests.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/tests.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -29,12 +29,13 @@
from z3ext.controlpanel.testing import setUpControlPanel
-from z3ext.formatter import dformatter, dtformatter, fancydatetime
+from z3ext.formatter import \
+ dformatter, dtformatter, fancydatetime, timeformatter
from z3ext.formatter.expression import FormatterExpression
class ZPTPage(TrustedAppPT, PageTemplate):
-
+
def render(self, request, *args, **kw):
namespace = self.pt_getContext(args, kw)
namespace['request'] = request
@@ -53,6 +54,7 @@
provideAdapter(dtformatter.DatetimeFormatterFactory, name='dateTime')
provideAdapter(
fancydatetime.FancyDatetimeFormatterFactory, name='fancyDatetime')
+ provideAdapter(timeformatter.TimeFormatterFactory, name='time')
def tearDown(test):
@@ -66,6 +68,4 @@
'README.txt',
setUp=setUp, tearDown=tearDown,
optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
- doctest.DocFileSuite(
- 'config.txt', setUp=setUp, tearDown=tearDown),
))
Added: z3ext.formatter/trunk/src/z3ext/formatter/timeformatter.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/timeformatter.py (rev 0)
+++ z3ext.formatter/trunk/src/z3ext/formatter/timeformatter.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -0,0 +1,58 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+
+$Id$
+"""
+from pytz import timezone
+from datetime import datetime
+from zope import interface, component
+from zope.component import getUtility
+from zope.publisher.interfaces.http import IHTTPRequest
+
+from interfaces import IFormatter, IFormatterFactory, IFormatterConfiglet
+
+
+class TimeFormatter(object):
+ interface.implements(IFormatter)
+
+ def __init__(self, request, *args):
+ try:
+ self.tp = args[0]
+ except:
+ self.tp = 'medium'
+
+ def format(self, value):
+ if not isinstance(value, datetime):
+ return value
+
+ configlet = getUtility(IFormatterConfiglet)
+ tz = timezone(configlet.timezone)
+ if value.tzinfo is None:
+ value = datetime(value.year, value.month, value.day, value.hour,
+ value.minute, value.second, value.microsecond, tz)
+
+ value = value.astimezone(tz)
+ return unicode(value.strftime(str(getattr(configlet, 'time_'+self.tp))))
+
+
+class TimeFormatterFactory(object):
+ component.adapts(IHTTPRequest)
+ interface.implements(IFormatterFactory)
+
+ def __init__(self, request):
+ self.request = request
+
+ def __call__(self, *args, **kw):
+ return TimeFormatter(self.request, *args)
Property changes on: z3ext.formatter/trunk/src/z3ext/formatter/timeformatter.py
___________________________________________________________________
Added: svn:keywords
+ Id
Modified: z3ext.formatter/trunk/src/z3ext/formatter/vocabulary.py
===================================================================
--- z3ext.formatter/trunk/src/z3ext/formatter/vocabulary.py 2009-08-10 10:47:15 UTC (rev 102623)
+++ z3ext.formatter/trunk/src/z3ext/formatter/vocabulary.py 2009-08-10 11:27:27 UTC (rev 102624)
@@ -24,11 +24,6 @@
_ = MessageFactory('z3ext.formatter')
-timezonesOptions = SimpleVocabulary(
- [SimpleTerm(1, '1', _(u'No timezone')),
- SimpleTerm(2, '2', _(u'Number of hours')),
- SimpleTerm(3, '3', _(u'Timezone name'))])
-
timezones = SimpleVocabulary(
[SimpleTerm(unicode(tz), tz, unicode(tz)) for tz in pytz.common_timezones])
More information about the Checkins
mailing list