summaryrefslogtreecommitdiff
path: root/src/mailman/utilities/datetime.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/utilities/datetime.py')
-rw-r--r--src/mailman/utilities/datetime.py47
1 files changed, 42 insertions, 5 deletions
diff --git a/src/mailman/utilities/datetime.py b/src/mailman/utilities/datetime.py
index 96d14fce5..3f451efaf 100644
--- a/src/mailman/utilities/datetime.py
+++ b/src/mailman/utilities/datetime.py
@@ -28,9 +28,12 @@ from __future__ import absolute_import, unicode_literals
__metaclass__ = type
__all__ = [
'DateFactory',
+ 'RFC822_DATE_FMT',
+ 'UTC',
'factory',
'now',
'today',
+ 'utc',
]
@@ -39,6 +42,30 @@ import datetime
from mailman.testing import layers
+# Python always sets the locale to 'C' locale unless the user explicitly calls
+# locale.setlocale(locale.LC_ALL, ''). Since we never do this in Mailman (and
+# no library better do it either!) this will safely give us expected RFC 5322
+# Date headers.
+RFC822_DATE_FMT = '%a, %d %b %Y %H:%M:%S %z'
+
+
+
+# Definition of UTC timezone, taken from
+# http://docs.python.org/library/datetime.html
+ZERO = datetime.timedelta(0)
+
+class UTC(datetime.tzinfo):
+ def utcoffset(self, dt):
+ return ZERO
+ def tzname(self, dt):
+ return 'UTC'
+ def dst(self, dt):
+ return ZERO
+
+utc = UTC()
+_missing = object()
+
+
class DateFactory:
"""A factory for today() and now() that works with testing."""
@@ -47,12 +74,21 @@ class DateFactory:
predictable_now = None
predictable_today = None
- def now(self, tz=None):
+ def now(self, tz=_missing, strip_tzinfo=True):
# We can't automatically fast-forward because some tests require us to
# stay on the same day for a while, e.g. autorespond.txt.
- return (self.predictable_now
- if layers.is_testing()
- else datetime.datetime.now(tz))
+ if tz is _missing:
+ tz = utc
+ # Storm cannot yet handle datetimes with tz suffixes. Assume we're
+ # using UTC datetimes everywhere, so set the tzinfo to None. This
+ # does *not* change the actual time values. LP: #280708
+ tz_now = (self.predictable_now
+ if layers.is_testing()
+ else datetime.datetime.now(tz))
+ return (tz_now.replace(tzinfo=None)
+ if strip_tzinfo
+ else tz_now)
+
def today(self):
return (self.predictable_today
@@ -61,7 +97,8 @@ class DateFactory:
@classmethod
def reset(cls):
- cls.predictable_now = datetime.datetime(2005, 8, 1, 7, 49, 23)
+ cls.predictable_now = datetime.datetime(2005, 8, 1, 7, 49, 23,
+ tzinfo=utc)
cls.predictable_today = cls.predictable_now.date()
@classmethod