/usr/share/pyshared/mx/DateTime/ARPA.py is in python-egenix-mxdatetime 3.2.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | """ This module provides a set of constructors and routines to convert
between DateTime[Delta] instances and ARPA representations of date
and time. The format is specified by RFC822 + RFC1123.
Note: Timezones are only interpreted by ParseDateTimeGMT(). All
other constructors silently ignore the time zone information.
Copyright (c) 1998-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2011, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
import DateTime,Timezone
import re
# Grammar: RFC822 + RFC1123 + depreciated RFC850
_litday = '(?P<litday>Mon|Tue|Wed|Thu|Fri|Sat|Sun)[a-z]*'
_litmonth = '(?P<litmonth>Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)'\
'[a-z]*'
_date = ('(?:(?P<day>\d?\d)(?: +' + _litmonth +
' +|-(?P<month>\d?\d)-)(?P<year>(?:\d\d)?\d\d))')
_zone = Timezone.zone
_time = ('(?:(?P<hour>\d\d):(?P<minute>\d\d)'
'(?::(?P<second>\d\d))?(?: +'+_zone+')?)')
# Timezone information is made optional because some mail apps
# forget to add it (most of these seem to be spamming engines, btw).
# It defaults to UTC.
_arpadate = '(?:'+ _litday + ',? )? *' + _date
_arpadatetime = '(?:'+ _litday + ',? )? *' + _date + ' +' + _time
# We are not strict about the extra characters: some applications
# add extra information to the date header field. Additional spaces
# between the fields and extra characters in the literal day
# and month fields are also silently ignored.
arpadateRE = re.compile(_arpadate)
arpadatetimeRE = re.compile(_arpadatetime)
# Translation tables
litdaytable = {'mon':0, 'tue':1, 'wed':2, 'thu':3, 'fri':4, 'sat':5, 'sun':6 }
litmonthtable = {'jan':1, 'feb':2, 'mar':3, 'apr':4, 'may':5, 'jun':6,
'jul':7, 'aug':8, 'sep':9, 'oct':10, 'nov':11, 'dec':12 }
_days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
_months = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
def ParseDate(arpastring,parse_arpadate=arpadateRE.match):
""" ParseDate(arpastring)
Returns a DateTime instance reflecting the given ARPA
date. Only the date part is parsed, any time part will be
ignored. The instance's time is set to 0:00:00.
"""
s = arpastring.strip()
date = parse_arpadate(s)
if not date:
raise ValueError,'wrong format'
litday,day,litmonth,month,year = date.groups()
if len(year) == 2:
year = DateTime.add_century(int(year))
else:
year = int(year)
if litmonth:
litmonth = litmonth.lower()
try:
month = litmonthtable[litmonth]
except KeyError:
raise ValueError,'wrong month format'
else:
month = int(month)
day = int(day)
# litday and timezone are ignored
return DateTime.DateTime(year,month,day)
def ParseDateTime(arpastring,parse_arpadatetime=arpadatetimeRE.match):
""" ParseDateTime(arpastring)
Returns a DateTime instance reflecting the given ARPA date
assuming it is local time (timezones are silently ignored).
"""
s = arpastring.strip()
date = parse_arpadatetime(s)
if not date:
raise ValueError,'wrong format or unknown time zone'
litday,day,litmonth,month,year,hour,minute,second,zone = date.groups()
if len(year) == 2:
year = DateTime.add_century(int(year))
else:
year = int(year)
if litmonth:
litmonth = litmonth.lower()
try:
month = litmonthtable[litmonth]
except KeyError:
raise ValueError,'wrong month format'
else:
month = int(month)
day = int(day)
hour = int(hour)
minute = int(minute)
if second is None:
second = 0.0
else:
second = float(second)
# litday and timezone are ignored
return DateTime.DateTime(year,month,day,hour,minute,second)
def ParseDateTimeGMT(arpastring,parse_arpadatetime=arpadatetimeRE.match):
""" ParseDateTimeGMT(arpastring)
Returns a DateTime instance reflecting the given ARPA date
converting it to UTC (timezones are honored).
"""
s = arpastring.strip()
date = parse_arpadatetime(s)
if not date:
raise ValueError,'wrong format or unknown time zone'
litday,day,litmonth,month,year,hour,minute,second,zone = date.groups()
if len(year) == 2:
year = DateTime.add_century(int(year))
else:
year = int(year)
if litmonth:
litmonth = litmonth.lower()
try:
month = litmonthtable[litmonth]
except KeyError:
raise ValueError,'wrong month format'
else:
month = int(month)
day = int(day)
hour = int(hour)
minute = int(minute)
if second is None:
second = 0.0
else:
second = float(second)
offset = Timezone.utc_offset(zone)
# litday is ignored
return DateTime.DateTime(year,month,day,hour,minute,second) - offset
# Alias
ParseDateTimeUTC = ParseDateTimeGMT
def str(datetime,tz=None):
""" str(datetime,tz=DateTime.tz_offset(datetime))
Returns the datetime instance as ARPA date string. tz can be
given as DateTimeDelta instance providing the time zone
difference from datetime's zone to UTC. It defaults to
DateTime.tz_offset(datetime) which assumes local time.
"""
if tz is None:
tz = datetime.gmtoffset()
return '%s, %02i %s %04i %02i:%02i:%02i %+03i%02i' % (
_days[datetime.day_of_week], datetime.day,
_months[datetime.month], datetime.year,
datetime.hour, datetime.minute, datetime.second,
tz.hour,tz.minute)
def strGMT(datetime):
""" strGMT(datetime)
Returns the datetime instance as ARPA date string assuming it
is given in GMT.
"""
return '%s, %02i %s %04i %02i:%02i:%02i GMT' % (
_days[datetime.day_of_week], datetime.day,
_months[datetime.month], datetime.year,
datetime.hour, datetime.minute, datetime.second)
def strUTC(datetime):
""" strUTC(datetime)
Returns the datetime instance as ARPA date string assuming it
is given in UTC.
"""
return '%s, %02i %s %04i %02i:%02i:%02i UTC' % (
_days[datetime.day_of_week], datetime.day,
_months[datetime.month], datetime.year,
datetime.hour, datetime.minute, datetime.second)
def _test():
import sys, os, rfc822
file = os.path.join(os.environ['HOME'], 'nsmail/Inbox')
f = open(file, 'r')
while 1:
m = rfc822.Message(f)
if not m:
break
print 'From:', m.getaddr('from')
print 'To:', m.getaddrlist('to')
print 'Subject:', m.getheader('subject')
raw = m.getheader('date')
try:
date = ParseDateTimeUTC(raw)
print 'Date:',strUTC(date)
except ValueError,why:
print 'PROBLEMS:',repr(raw),'-->',why
raw_input('...hit return to continue')
print
# Netscape mail file
while 1:
line = f.readline()
if line[:6] == 'From -':
break
if __name__ == '__main__':
_test()
|