diff options
| author | Barry Warsaw | 2012-03-26 08:04:00 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2012-03-26 08:04:00 -0400 |
| commit | 5cb68db131db32c643382f6fd1418a3659dc6f8e (patch) | |
| tree | 13a2e02a48303804c2cae37c656937711bd37fa5 /src/mailman/utilities/datetime.py | |
| parent | cfb7138579ddb8a4adb10956ceb39089181271b4 (diff) | |
| download | mailman-5cb68db131db32c643382f6fd1418a3659dc6f8e.tar.gz mailman-5cb68db131db32c643382f6fd1418a3659dc6f8e.tar.zst mailman-5cb68db131db32c643382f6fd1418a3659dc6f8e.zip | |
Diffstat (limited to 'src/mailman/utilities/datetime.py')
| -rw-r--r-- | src/mailman/utilities/datetime.py | 47 |
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 |
