~mailman-coders/mailman/3.0

7286 by Barry Warsaw
Bump copyright years.
1
# Copyright (C) 2009-2015 by the Free Software Foundation, Inc.
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
2
#
3
# This file is part of GNU Mailman.
4
#
5
# GNU Mailman is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free
7
# Software Foundation, either version 3 of the License, or (at your option)
8
# any later version.
9
#
10
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13
# more details.
14
#
15
# You should have received a copy of the GNU General Public License along with
16
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
17
18
"""Datetime utilities.
19
20
Use these functions to produce variable times rather than the built-in
21
datetime.datetime.now() and datetime.date.today().  These are better
22
instrumented for testing purposes.
23
"""
24
25
__all__ = [
6703 by Barry Warsaw
__all__
26
    'DateFactory',
7132 by Barry Warsaw
Architecture
27
    'RFC822_DATE_FMT',
28
    'UTC',
6703 by Barry Warsaw
__all__
29
    'factory',
30
    'now',
31
    'today',
7132 by Barry Warsaw
Architecture
32
    'utc',
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
33
    ]
34
35
36
import datetime
37
6974.1.18 by Barry Warsaw
More fleshing out of the users REST API.
38
from mailman.testing import layers
6974.3.2 by Barry Warsaw
* Re-organize the interface between buildout.cfg and the zope.testing layer
39
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
40
7132 by Barry Warsaw
Architecture
41
# Python always sets the locale to 'C' locale unless the user explicitly calls
42
# locale.setlocale(locale.LC_ALL, '').  Since we never do this in Mailman (and
43
# no library better do it either!) this will safely give us expected RFC 5322
44
# Date headers.
45
RFC822_DATE_FMT = '%a, %d %b %Y %H:%M:%S %z'
46
47
48

49
# Definition of UTC timezone, taken from
50
# http://docs.python.org/library/datetime.html
51
ZERO = datetime.timedelta(0)
52
53
class UTC(datetime.tzinfo):
54
    def utcoffset(self, dt):
55
        return ZERO
56
    def tzname(self, dt):
57
        return 'UTC'
58
    def dst(self, dt):
59
        return ZERO
60
61
utc = UTC()
62
_missing = object()
63
64
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
65

66
class DateFactory:
67
    """A factory for today() and now() that works with testing."""
68
69
    # The predictable time.
70
    predictable_now = None
71
    predictable_today = None
72
7132 by Barry Warsaw
Architecture
73
    def now(self, tz=_missing, strip_tzinfo=True):
6974.3.2 by Barry Warsaw
* Re-organize the interface between buildout.cfg and the zope.testing layer
74
        # We can't automatically fast-forward because some tests require us to
75
        # stay on the same day for a while, e.g. autorespond.txt.
7132 by Barry Warsaw
Architecture
76
        if tz is _missing:
77
            tz = utc
78
        # Storm cannot yet handle datetimes with tz suffixes.  Assume we're
79
        # using UTC datetimes everywhere, so set the tzinfo to None.  This
80
        # does *not* change the actual time values.  LP: #280708
81
        tz_now = (self.predictable_now
82
                  if layers.is_testing()
83
                  else datetime.datetime.now(tz))
84
        return (tz_now.replace(tzinfo=None)
85
                if strip_tzinfo
86
                else tz_now)
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
87
88
    def today(self):
89
        return (self.predictable_today
6974.1.18 by Barry Warsaw
More fleshing out of the users REST API.
90
                if layers.is_testing()
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
91
                else datetime.date.today())
92
93
    @classmethod
94
    def reset(cls):
7132 by Barry Warsaw
Architecture
95
        cls.predictable_now = datetime.datetime(2005, 8, 1, 7, 49, 23,
96
                                                tzinfo=utc)
6693 by Barry Warsaw
Add IAutoResponseSet.last_response() and fix a few bugs with the datetime
97
        cls.predictable_today = cls.predictable_now.date()
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
98
99
    @classmethod
100
    def fast_forward(cls, days=1):
6693 by Barry Warsaw
Add IAutoResponseSet.last_response() and fix a few bugs with the datetime
101
        cls.predictable_now += datetime.timedelta(days=days)
102
        cls.predictable_today = cls.predictable_now.date()
6691 by Barry Warsaw
Add a "mock and monkey" layer to set up testing infrastructure, e.g. for
103
104
105
factory = DateFactory()
106
factory.reset()
107
today = factory.today
108
now = factory.now
6974.1.18 by Barry Warsaw
More fleshing out of the users REST API.
109
layers.MockAndMonkeyLayer.register_reset(factory.reset)