/usr/lib/python2.7/dist-packages/mx/DateTime/ISO.py is in python-egenix-mxdatetime 3.2.9-1.
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | """ This module provides a set of constructors and routines to convert
between DateTime[Delta] instances and ISO representations of date
and time.
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-2015, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author.
"""
import DateTime,Timezone
import re
# Grammar: ISO 8601 (not all, but what we need from it)
_year = '(?P<year>\d?\d\d\d)'
_month = '(?P<month>\d?\d)'
_day = '(?P<day>\d?\d)'
_hour = '(?P<hour>\d?\d)'
_minute = '(?P<minute>\d?\d)'
_second = '(?P<second>\d?\d(?:\.\d+)?)'
_sign = '(?P<sign>[-+])'
_week = 'W(?P<week>\d?\d)'
_zone = Timezone.isozone
_weekdate = _year + '-?(?:' + _week + '-?' + _day + '?)?'
_date = _year + '-?' + '(?:' + _month + '-?' + _day + '?)?'
_time = _hour + ':?' + _minute + ':?' + _second + '?(?:' + _zone + ')?'
isodatetimeRE = re.compile(_date + '(?:[ T]' + _time + ')?$')
isodateRE = re.compile(_date + '$')
isotimeRE = re.compile(_time + '$')
isodeltaRE = re.compile(_sign + '?' + _time + '$')
isoweekRE = re.compile(_weekdate + '$')
isoweektimeRE = re.compile(_weekdate + '(?:[ T]' + _time + ')?$')
def WeekTime(year,isoweek=1,isoday=1,hour=0,minute=0,second=0.0):
""" Week(year,isoweek=1,isoday=1,hour=0,minute=0,second=0.0)
Returns a DateTime instance pointing to the given ISO week and
day. isoday defaults to 1, which corresponds to Monday in the
ISO numbering. The time part is set as given.
"""
d = DateTime.DateTime(year,1,1,hour,minute,second)
if d.iso_week[0] == year:
# 1.1. belongs to year (backup to Monday)
return d + (-d.day_of_week + 7 * (isoweek-1) + isoday-1)
else:
# 1.1. belongs to year-1 (advance to next Monday)
return d + (7-d.day_of_week + 7 * (isoweek-1) + isoday-1)
# Alias
Week = WeekTime
# Aliases for the other constructors (they all happen to already use
# ISO format)
Date = DateTime.Date
Time = DateTime.Time
TimeDelta = DateTime.TimeDelta
def ParseDateTime(isostring,parse_isodatetime=isodatetimeRE.match):
""" ParseDateTime(isostring)
Returns a DateTime instance reflecting the given ISO date. A
time part is optional and must be delimited from the date by a
space or 'T'.
Time zone information is parsed, but not evaluated.
"""
s = isostring.strip()
date = parse_isodatetime(s)
if not date:
raise ValueError,'wrong format, use YYYY-MM-DD HH:MM:SS'
year,month,day,hour,minute,second,zone = date.groups()
year = int(year)
if month is None:
month = 1
else:
month = int(month)
if day is None:
day = 1
else:
day = int(day)
if hour is None:
hour = 0
else:
hour = int(hour)
if minute is None:
minute = 0
else:
minute = int(minute)
if second is None:
second = 0.0
else:
second = float(second)
return DateTime.DateTime(year,month,day,hour,minute,second)
def ParseDateTimeGMT(isostring,parse_isodatetime=isodatetimeRE.match):
""" ParseDateTimeGMT(isostring)
Returns a DateTime instance in UTC reflecting the given ISO
date. A time part is optional and must be delimited from the
date by a space or 'T'. Timezones are honored.
"""
s = isostring.strip()
date = parse_isodatetime(s)
if not date:
raise ValueError,'wrong format, use YYYY-MM-DD HH:MM:SS'
year,month,day,hour,minute,second,zone = date.groups()
year = int(year)
if month is None:
month = 1
else:
month = int(month)
if day is None:
day = 1
else:
day = int(day)
if hour is None:
hour = 0
else:
hour = int(hour)
if minute is None:
minute = 0
else:
minute = int(minute)
if second is None:
second = 0.0
else:
second = float(second)
offset = Timezone.utc_offset(zone)
return DateTime.DateTime(year,month,day,hour,minute,second) - offset
# Alias
ParseDateTimeUTC = ParseDateTimeGMT
def ParseDate(isostring,parse_isodate=isodateRE.match):
""" ParseDate(isostring)
Returns a DateTime instance reflecting the given ISO date. A
time part may not be included.
"""
s = isostring.strip()
date = parse_isodate(s)
if not date:
raise ValueError,'wrong format, use YYYY-MM-DD'
year,month,day = date.groups()
year = int(year)
if month is None:
month = 1
else:
month = int(month)
if day is None:
day = 1
else:
day = int(day)
return DateTime.DateTime(year,month,day)
def ParseWeek(isostring,parse_isoweek=isoweekRE.match):
""" ParseWeek(isostring)
Returns a DateTime instance reflecting the given ISO date. A
time part may not be included.
"""
s = isostring.strip()
date = parse_isoweek(s)
if not date:
raise ValueError,'wrong format, use yyyy-Www-d, e.g. 1998-W01-1'
year,week,day = date.groups()
year = int(year)
if week is None:
week = 1
else:
week = int(week)
if day is None:
day = 1
else:
day = int(day)
return Week(year,week,day)
def ParseWeekTime(isostring,parse_isoweektime=isoweektimeRE.match):
""" ParseWeekTime(isostring)
Returns a DateTime instance reflecting the given ISO date. A
time part is optional and must be delimited from the date by a
space or 'T'.
"""
s = isostring.strip()
date = parse_isoweektime(s)
if not date:
raise ValueError,'wrong format, use e.g. "1998-W01-1 12:00:30"'
year,week,day,hour,minute,second,zone = date.groups()
year = int(year)
if week is None:
week = 1
else:
week = int(week)
if day is None:
day = 1
else:
day = int(day)
if hour is None:
hour = 0
else:
hour = int(hour)
if minute is None:
minute = 0
else:
minute = int(minute)
if second is None:
second = 0.0
else:
second = float(second)
return WeekTime(year,week,day,hour,minute,second)
def ParseTime(isostring,parse_isotime=isotimeRE.match):
""" ParseTime(isostring)
Returns a DateTimeDelta instance reflecting the given ISO time.
Hours and minutes must be given, seconds are
optional. Fractions of a second may also be used,
e.g. 12:23:12.34.
"""
s = isostring.strip()
time = parse_isotime(s)
if not time:
raise ValueError,'wrong format, use HH:MM:SS'
hour,minute,second,zone = time.groups()
hour = int(hour)
minute = int(minute)
if second is not None:
second = float(second)
else:
second = 0.0
return DateTime.TimeDelta(hour,minute,second)
def ParseTimeDelta(isostring,parse_isodelta=isodeltaRE.match):
""" ParseTimeDelta(isostring)
Returns a DateTimeDelta instance reflecting the given ISO time
as delta. Hours and minutes must be given, seconds are
optional. Fractions of a second may also be used,
e.g. 12:23:12.34. In addition to the ISO standard a sign may be
prepended to the time, e.g. -12:34.
"""
s = isostring.strip()
time = parse_isodelta(s)
if not time:
raise ValueError,'wrong format, use [-]HH:MM:SS'
sign,hour,minute,second,zone = time.groups()
hour = int(hour)
minute = int(minute)
if second is not None:
second = float(second)
else:
second = 0.0
if sign and sign == '-':
return -DateTime.TimeDelta(hour,minute,second)
else:
return DateTime.TimeDelta(hour,minute,second)
def ParseAny(isostring):
""" ParseAny(isostring)
Parses the given string and tries to convert it to a
DateTime[Delta] instance.
"""
try:
return ParseDateTime(isostring)
except ValueError:
pass
try:
return ParseWeekTime(isostring)
except ValueError:
pass
try:
return ParseTimeDelta(isostring)
except ValueError:
raise ValueError,'unsupported format: "%s"' % isostring
def str(datetime,tz=None):
""" str(datetime,tz=DateTime.tz_offset(datetime))
Returns the datetime instance as ISO 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 '%04i-%02i-%02i %02i:%02i:%02i%+03i%02i' % (
datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second,
tz.hour,tz.minute)
def strGMT(datetime):
""" strGMT(datetime)
Returns the datetime instance as ISO date string assuming it is
given in GMT.
"""
return '%04i-%02i-%02i %02i:%02i:%02i+0000' % (
datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second)
def strUTC(datetime):
""" strUTC(datetime)
Returns the datetime instance as ISO date string assuming it is
given in UTC.
"""
return '%04i-%02i-%02i %02i:%02i:%02i+0000' % (
datetime.year, datetime.month, datetime.day,
datetime.hour, datetime.minute, datetime.second)
# Testing
if __name__ == '__main__':
e = DateTime.Date(1900,1,1)
for i in range(100000):
d = e + i
year,week,day = d.iso_week
c = WeekTime(year,week,day)
if d != c:
print ' Check %s (given; %i) != %s (parsed)' % (d,d.day_of_week,c)
elif i % 1000 == 0:
print d,'ok'
|