~sambuddhabasu1/mailman/fix_mailman_run_error

« back to all changes in this revision

Viewing changes to src/mailman/utilities/datetime.py

  • Committer: Barry Warsaw
  • Date: 2012-03-26 12:04:00 UTC
  • Revision ID: barry@list.org-20120326120400-jfezy6cg60ygod7k
Architecture
------------
 * Internally, all datetimes are kept in the UTC timezone, however because of
   LP: #280708, they are stored in the database in naive format.
 * `received_time` is now added to the message metadata by the LMTP runner
   instead of by `Switchboard.enqueue()`.  This latter no longer depends on
   `received_time` in the metadata.
 * The `ArchiveRunner` no longer acquires a lock before it calls the
   individual archiver implementations, since not all of them need a lock.  If
   they do, the implementations must acquire said lock themselves.

Configuration
-------------
 * New configuration variables `clobber_date` and `clobber_skew` supported in
   every `[archiver.<name>]` section.  These are used to determine under what
   circumstances a message destined for a specific archiver should have its
   `Date:` header clobbered.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
__metaclass__ = type
29
29
__all__ = [
30
30
    'DateFactory',
 
31
    'RFC822_DATE_FMT',
 
32
    'UTC',
31
33
    'factory',
32
34
    'now',
33
35
    'today',
 
36
    'utc',
34
37
    ]
35
38
 
36
39
 
39
42
from mailman.testing import layers
40
43
 
41
44
 
 
45
# Python always sets the locale to 'C' locale unless the user explicitly calls
 
46
# locale.setlocale(locale.LC_ALL, '').  Since we never do this in Mailman (and
 
47
# no library better do it either!) this will safely give us expected RFC 5322
 
48
# Date headers.
 
49
RFC822_DATE_FMT = '%a, %d %b %Y %H:%M:%S %z'
 
50
 
 
51
 
 
52
 
 
53
# Definition of UTC timezone, taken from
 
54
# http://docs.python.org/library/datetime.html
 
55
ZERO = datetime.timedelta(0)
 
56
 
 
57
class UTC(datetime.tzinfo):
 
58
    def utcoffset(self, dt):
 
59
        return ZERO
 
60
    def tzname(self, dt):
 
61
        return 'UTC'
 
62
    def dst(self, dt):
 
63
        return ZERO
 
64
 
 
65
utc = UTC()
 
66
_missing = object()
 
67
 
 
68
 
42
69
 
43
70
class DateFactory:
44
71
    """A factory for today() and now() that works with testing."""
47
74
    predictable_now = None
48
75
    predictable_today = None
49
76
 
50
 
    def now(self, tz=None):
 
77
    def now(self, tz=_missing, strip_tzinfo=True):
51
78
        # We can't automatically fast-forward because some tests require us to
52
79
        # stay on the same day for a while, e.g. autorespond.txt.
53
 
        return (self.predictable_now
54
 
                if layers.is_testing()
55
 
                else datetime.datetime.now(tz))
 
80
        if tz is _missing:
 
81
            tz = utc
 
82
        # Storm cannot yet handle datetimes with tz suffixes.  Assume we're
 
83
        # using UTC datetimes everywhere, so set the tzinfo to None.  This
 
84
        # does *not* change the actual time values.  LP: #280708
 
85
        tz_now = (self.predictable_now
 
86
                  if layers.is_testing()
 
87
                  else datetime.datetime.now(tz))
 
88
        return (tz_now.replace(tzinfo=None)
 
89
                if strip_tzinfo
 
90
                else tz_now)
 
91
        
56
92
 
57
93
    def today(self):
58
94
        return (self.predictable_today
61
97
 
62
98
    @classmethod
63
99
    def reset(cls):
64
 
        cls.predictable_now = datetime.datetime(2005, 8, 1, 7, 49, 23)
 
100
        cls.predictable_now = datetime.datetime(2005, 8, 1, 7, 49, 23,
 
101
                                                tzinfo=utc)
65
102
        cls.predictable_today = cls.predictable_now.date()
66
103
 
67
104
    @classmethod