paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - DateSupport library (derived from EventAggregatorSupport) |
paul@0 | 4 | |
paul@48 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@60 | 9 | from email.utils import parsedate_tz |
paul@0 | 10 | import calendar |
paul@0 | 11 | import datetime |
paul@0 | 12 | import re |
paul@0 | 13 | import bisect |
paul@0 | 14 | |
paul@0 | 15 | try: |
paul@0 | 16 | import pytz |
paul@0 | 17 | except ImportError: |
paul@0 | 18 | pytz = None |
paul@0 | 19 | |
paul@0 | 20 | # Date labels. |
paul@0 | 21 | |
paul@0 | 22 | month_labels = ["January", "February", "March", "April", "May", "June", |
paul@0 | 23 | "July", "August", "September", "October", "November", "December"] |
paul@0 | 24 | weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] |
paul@67 | 25 | weekday_labels_verbose = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] |
paul@0 | 26 | |
paul@0 | 27 | # Month, date, time and datetime parsing. |
paul@0 | 28 | |
paul@0 | 29 | month_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})' |
paul@0 | 30 | date_regexp_str = ur'(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})' |
paul@0 | 31 | time_regexp_str = ur'(?P<hour>[0-2][0-9]):(?P<minute>[0-5][0-9])(?::(?P<second>[0-6][0-9]))?' |
paul@8 | 32 | timezone_offset_str = ur'(?P<offset>(UTC)?(?:(?P<sign>[-+])?(?P<hours>[0-9]{2})(?::?(?P<minutes>[0-9]{2}))?))' |
paul@0 | 33 | timezone_olson_str = ur'(?P<olson>[a-zA-Z]+(?:/[-_a-zA-Z]+){1,2})' |
paul@0 | 34 | timezone_utc_str = ur'UTC' |
paul@0 | 35 | timezone_regexp_str = ur'(?P<zone>' + timezone_offset_str + '|' + timezone_olson_str + '|' + timezone_utc_str + ')' |
paul@0 | 36 | datetime_regexp_str = date_regexp_str + ur'(?:\s+' + time_regexp_str + ur'(?:\s+' + timezone_regexp_str + ur')?)?' |
paul@0 | 37 | |
paul@0 | 38 | month_regexp = re.compile(month_regexp_str, re.UNICODE) |
paul@0 | 39 | date_regexp = re.compile(date_regexp_str, re.UNICODE) |
paul@0 | 40 | time_regexp = re.compile(time_regexp_str, re.UNICODE) |
paul@0 | 41 | timezone_olson_regexp = re.compile(timezone_olson_str, re.UNICODE) |
paul@0 | 42 | timezone_offset_regexp = re.compile(timezone_offset_str, re.UNICODE) |
paul@0 | 43 | datetime_regexp = re.compile(datetime_regexp_str, re.UNICODE) |
paul@0 | 44 | |
paul@0 | 45 | # iCalendar date and datetime parsing. |
paul@0 | 46 | |
paul@0 | 47 | date_icalendar_regexp_str = ur'(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})' |
paul@0 | 48 | datetime_icalendar_regexp_str = date_icalendar_regexp_str + \ |
paul@0 | 49 | ur'(?:' \ |
paul@0 | 50 | ur'T(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-6][0-9])' \ |
paul@0 | 51 | ur'(?P<utc>Z)?' \ |
paul@0 | 52 | ur')?' |
paul@0 | 53 | |
paul@55 | 54 | # ISO 8601 date and datetime parsing. |
paul@57 | 55 | # NOTE: This is really RFC 3339 format. |
paul@57 | 56 | # NOTE: See: http://tools.ietf.org/html/rfc3339 |
paul@55 | 57 | |
paul@55 | 58 | timezone_iso8601_offset_str = ur'(?P<offset>(?:(?P<sign>[-+])(?P<hours>[0-9]{2}):(?P<minutes>[0-9]{2})))' |
paul@55 | 59 | datetime_iso8601_regexp_str = date_regexp_str + \ |
paul@55 | 60 | ur'(?:T' + time_regexp_str + \ |
paul@55 | 61 | ur'(?:(?P<utc>Z)|(?P<zone>' + timezone_iso8601_offset_str + '))' \ |
paul@55 | 62 | ur')?' |
paul@55 | 63 | |
paul@0 | 64 | date_icalendar_regexp = re.compile(date_icalendar_regexp_str, re.UNICODE) |
paul@0 | 65 | datetime_icalendar_regexp = re.compile(datetime_icalendar_regexp_str, re.UNICODE) |
paul@55 | 66 | datetime_iso8601_regexp = re.compile(datetime_iso8601_regexp_str, re.UNICODE) |
paul@0 | 67 | |
paul@0 | 68 | # Utility functions. |
paul@0 | 69 | |
paul@8 | 70 | def sign(x): |
paul@8 | 71 | if x < 0: |
paul@8 | 72 | return -1 |
paul@8 | 73 | else: |
paul@8 | 74 | return 1 |
paul@8 | 75 | |
paul@0 | 76 | def int_or_none(x): |
paul@0 | 77 | if x is None: |
paul@0 | 78 | return x |
paul@0 | 79 | else: |
paul@0 | 80 | return int(x) |
paul@0 | 81 | |
paul@0 | 82 | def getMonthLabel(month): |
paul@0 | 83 | |
paul@0 | 84 | "Return an unlocalised label for the given 'month'." |
paul@0 | 85 | |
paul@0 | 86 | return month_labels[month - 1] # zero-based labels |
paul@0 | 87 | |
paul@0 | 88 | def getDayLabel(weekday): |
paul@0 | 89 | |
paul@0 | 90 | "Return an unlocalised label for the given 'weekday'." |
paul@0 | 91 | |
paul@0 | 92 | return weekday_labels[weekday] |
paul@0 | 93 | |
paul@67 | 94 | def getVerboseDayLabel(weekday): |
paul@67 | 95 | |
paul@67 | 96 | "Return an unlocalised verbose label for the given 'weekday'." |
paul@67 | 97 | |
paul@67 | 98 | return weekday_labels_verbose[weekday] |
paul@67 | 99 | |
paul@0 | 100 | # Interfaces. |
paul@0 | 101 | |
paul@0 | 102 | class ActsAsTimespan: |
paul@0 | 103 | pass |
paul@0 | 104 | |
paul@0 | 105 | # Date-related functions. |
paul@0 | 106 | |
paul@0 | 107 | def cmp_dates_as_day_start(a, b): |
paul@0 | 108 | |
paul@0 | 109 | """ |
paul@0 | 110 | Compare dates/datetimes 'a' and 'b' treating dates without time information |
paul@0 | 111 | as the earliest time in a particular day. |
paul@0 | 112 | """ |
paul@0 | 113 | |
paul@50 | 114 | if a == b: |
paul@0 | 115 | a2 = a.as_datetime_or_date() |
paul@0 | 116 | b2 = b.as_datetime_or_date() |
paul@0 | 117 | |
paul@0 | 118 | if isinstance(a2, Date) and isinstance(b2, DateTime): |
paul@0 | 119 | return -1 |
paul@0 | 120 | elif isinstance(a2, DateTime) and isinstance(b2, Date): |
paul@0 | 121 | return 1 |
paul@0 | 122 | |
paul@0 | 123 | return cmp(a, b) |
paul@0 | 124 | |
paul@0 | 125 | class Convertible: |
paul@0 | 126 | |
paul@0 | 127 | "Support for converting temporal objects." |
paul@0 | 128 | |
paul@0 | 129 | def _get_converter(self, resolution): |
paul@0 | 130 | if resolution == "month": |
paul@0 | 131 | return lambda x: x and x.as_month() |
paul@0 | 132 | elif resolution == "date": |
paul@0 | 133 | return lambda x: x and x.as_date() |
paul@0 | 134 | elif resolution == "datetime": |
paul@0 | 135 | return lambda x: x and x.as_datetime_or_date() |
paul@0 | 136 | else: |
paul@0 | 137 | return lambda x: x |
paul@0 | 138 | |
paul@0 | 139 | class Temporal(Convertible): |
paul@0 | 140 | |
paul@0 | 141 | "A simple temporal representation, common to dates and times." |
paul@0 | 142 | |
paul@0 | 143 | def __init__(self, data): |
paul@0 | 144 | self.data = list(data) |
paul@0 | 145 | |
paul@0 | 146 | def __repr__(self): |
paul@0 | 147 | return "%s(%r)" % (self.__class__.__name__, self.data) |
paul@0 | 148 | |
paul@0 | 149 | def __hash__(self): |
paul@0 | 150 | return hash(self.as_tuple()) |
paul@0 | 151 | |
paul@0 | 152 | def as_tuple(self): |
paul@0 | 153 | return tuple(self.data) |
paul@0 | 154 | |
paul@0 | 155 | def convert(self, resolution): |
paul@0 | 156 | return self._get_converter(resolution)(self) |
paul@0 | 157 | |
paul@0 | 158 | def __cmp__(self, other): |
paul@0 | 159 | |
paul@0 | 160 | """ |
paul@0 | 161 | The result of comparing this instance with 'other' is derived from a |
paul@0 | 162 | comparison of the instances' date(time) data at the highest common |
paul@0 | 163 | resolution, meaning that if a date is compared to a datetime, the |
paul@0 | 164 | datetime will be considered as a date. Thus, a date and a datetime |
paul@0 | 165 | referring to the same date will be considered equal. |
paul@0 | 166 | """ |
paul@0 | 167 | |
paul@0 | 168 | if not isinstance(other, Temporal): |
paul@0 | 169 | return NotImplemented |
paul@0 | 170 | else: |
paul@0 | 171 | data = self.as_tuple() |
paul@0 | 172 | other_data = other.as_tuple() |
paul@0 | 173 | length = min(len(data), len(other_data)) |
paul@0 | 174 | return cmp(data[:length], other_data[:length]) |
paul@0 | 175 | |
paul@0 | 176 | def __sub__(self, other): |
paul@0 | 177 | |
paul@0 | 178 | """ |
paul@0 | 179 | Return the difference between this object and the 'other' object at the |
paul@0 | 180 | highest common accuracy of both objects. |
paul@0 | 181 | """ |
paul@0 | 182 | |
paul@0 | 183 | if not isinstance(other, Temporal): |
paul@0 | 184 | return NotImplemented |
paul@0 | 185 | else: |
paul@0 | 186 | data = self.as_tuple() |
paul@0 | 187 | other_data = other.as_tuple() |
paul@8 | 188 | direction = self < other and 1 or -1 |
paul@8 | 189 | |
paul@0 | 190 | if len(data) < len(other_data): |
paul@8 | 191 | return (len(self.until(other)) - 1) * direction |
paul@0 | 192 | else: |
paul@8 | 193 | return (len(other.until(self)) - 1) * -direction |
paul@0 | 194 | |
paul@0 | 195 | def _until(self, start, end, nextfn, prevfn): |
paul@0 | 196 | |
paul@0 | 197 | """ |
paul@0 | 198 | Return a collection of units of time by starting from the given 'start' |
paul@0 | 199 | and stepping across intervening units until 'end' is reached, using the |
paul@0 | 200 | given 'nextfn' and 'prevfn' to step from one unit to the next. |
paul@0 | 201 | """ |
paul@0 | 202 | |
paul@0 | 203 | current = start |
paul@0 | 204 | units = [current] |
paul@0 | 205 | if current < end: |
paul@0 | 206 | while current < end: |
paul@0 | 207 | current = nextfn(current) |
paul@0 | 208 | units.append(current) |
paul@0 | 209 | elif current > end: |
paul@0 | 210 | while current > end: |
paul@0 | 211 | current = prevfn(current) |
paul@0 | 212 | units.append(current) |
paul@0 | 213 | return units |
paul@0 | 214 | |
paul@0 | 215 | def ambiguous(self): |
paul@0 | 216 | |
paul@0 | 217 | "Only times can be ambiguous." |
paul@0 | 218 | |
paul@0 | 219 | return 0 |
paul@0 | 220 | |
paul@0 | 221 | class Month(Temporal): |
paul@0 | 222 | |
paul@0 | 223 | "A simple year-month representation." |
paul@0 | 224 | |
paul@0 | 225 | def __str__(self): |
paul@0 | 226 | return "%04d-%02d" % self.as_tuple()[:2] |
paul@0 | 227 | |
paul@0 | 228 | def as_datetime(self, day, hour, minute, second, zone): |
paul@0 | 229 | return DateTime(self.as_tuple() + (day, hour, minute, second, zone)) |
paul@0 | 230 | |
paul@0 | 231 | def as_date(self, day): |
paul@0 | 232 | if day < 0: |
paul@0 | 233 | weekday, ndays = self.month_properties() |
paul@0 | 234 | day = ndays + 1 + day |
paul@0 | 235 | return Date(self.as_tuple() + (day,)) |
paul@0 | 236 | |
paul@0 | 237 | def as_month(self): |
paul@0 | 238 | return self |
paul@0 | 239 | |
paul@0 | 240 | def year(self): |
paul@0 | 241 | return self.data[0] |
paul@0 | 242 | |
paul@0 | 243 | def month(self): |
paul@0 | 244 | return self.data[1] |
paul@0 | 245 | |
paul@0 | 246 | def month_properties(self): |
paul@0 | 247 | |
paul@0 | 248 | """ |
paul@0 | 249 | Return the weekday of the 1st of the month, along with the number of |
paul@0 | 250 | days, as a tuple. |
paul@0 | 251 | """ |
paul@0 | 252 | |
paul@0 | 253 | year, month = self.as_tuple()[:2] |
paul@0 | 254 | return calendar.monthrange(year, month) |
paul@0 | 255 | |
paul@0 | 256 | def month_update(self, n=1): |
paul@0 | 257 | |
paul@0 | 258 | "Return the month updated by 'n' months." |
paul@0 | 259 | |
paul@0 | 260 | year, month = self.as_tuple()[:2] |
paul@0 | 261 | return Month((year + (month - 1 + n) / 12, (month - 1 + n) % 12 + 1)) |
paul@0 | 262 | |
paul@0 | 263 | update = month_update |
paul@0 | 264 | |
paul@0 | 265 | def next_month(self): |
paul@0 | 266 | |
paul@0 | 267 | "Return the month following this one." |
paul@0 | 268 | |
paul@0 | 269 | return self.month_update(1) |
paul@0 | 270 | |
paul@0 | 271 | next = next_month |
paul@0 | 272 | |
paul@0 | 273 | def previous_month(self): |
paul@0 | 274 | |
paul@0 | 275 | "Return the month preceding this one." |
paul@0 | 276 | |
paul@0 | 277 | return self.month_update(-1) |
paul@0 | 278 | |
paul@0 | 279 | previous = previous_month |
paul@0 | 280 | |
paul@0 | 281 | def months_until(self, end): |
paul@0 | 282 | |
paul@0 | 283 | "Return the collection of months from this month until 'end'." |
paul@0 | 284 | |
paul@0 | 285 | return self._until(self.as_month(), end.as_month(), Month.next_month, Month.previous_month) |
paul@0 | 286 | |
paul@0 | 287 | until = months_until |
paul@0 | 288 | |
paul@0 | 289 | class Date(Month): |
paul@0 | 290 | |
paul@0 | 291 | "A simple year-month-day representation." |
paul@0 | 292 | |
paul@0 | 293 | def constrain(self): |
paul@0 | 294 | year, month, day = self.as_tuple()[:3] |
paul@0 | 295 | |
paul@0 | 296 | month = max(min(month, 12), 1) |
paul@0 | 297 | wd, last_day = calendar.monthrange(year, month) |
paul@0 | 298 | day = max(min(day, last_day), 1) |
paul@0 | 299 | |
paul@0 | 300 | self.data[1:3] = month, day |
paul@0 | 301 | |
paul@0 | 302 | def __str__(self): |
paul@0 | 303 | return "%04d-%02d-%02d" % self.as_tuple()[:3] |
paul@0 | 304 | |
paul@0 | 305 | def as_datetime(self, hour, minute, second, zone): |
paul@0 | 306 | return DateTime(self.as_tuple() + (hour, minute, second, zone)) |
paul@0 | 307 | |
paul@0 | 308 | def as_start_of_day(self): |
paul@0 | 309 | return self.as_datetime(None, None, None, None) |
paul@0 | 310 | |
paul@0 | 311 | def as_date(self): |
paul@0 | 312 | return self |
paul@0 | 313 | |
paul@0 | 314 | def as_datetime_or_date(self): |
paul@0 | 315 | return self |
paul@0 | 316 | |
paul@0 | 317 | def as_month(self): |
paul@0 | 318 | return Month(self.data[:2]) |
paul@0 | 319 | |
paul@0 | 320 | def day(self): |
paul@0 | 321 | return self.data[2] |
paul@0 | 322 | |
paul@0 | 323 | def day_update(self, n=1): |
paul@0 | 324 | |
paul@0 | 325 | "Return the month updated by 'n' days." |
paul@0 | 326 | |
paul@0 | 327 | delta = datetime.timedelta(n) |
paul@0 | 328 | dt = datetime.date(*self.as_tuple()[:3]) |
paul@0 | 329 | dt_new = dt + delta |
paul@0 | 330 | return Date((dt_new.year, dt_new.month, dt_new.day)) |
paul@0 | 331 | |
paul@0 | 332 | update = day_update |
paul@0 | 333 | |
paul@0 | 334 | def next_day(self): |
paul@0 | 335 | |
paul@0 | 336 | "Return the date following this one." |
paul@0 | 337 | |
paul@0 | 338 | year, month, day = self.as_tuple()[:3] |
paul@0 | 339 | _wd, end_day = calendar.monthrange(year, month) |
paul@0 | 340 | if day == end_day: |
paul@0 | 341 | if month == 12: |
paul@0 | 342 | return Date((year + 1, 1, 1)) |
paul@0 | 343 | else: |
paul@0 | 344 | return Date((year, month + 1, 1)) |
paul@0 | 345 | else: |
paul@0 | 346 | return Date((year, month, day + 1)) |
paul@0 | 347 | |
paul@0 | 348 | next = next_day |
paul@0 | 349 | |
paul@0 | 350 | def previous_day(self): |
paul@0 | 351 | |
paul@0 | 352 | "Return the date preceding this one." |
paul@0 | 353 | |
paul@0 | 354 | year, month, day = self.as_tuple()[:3] |
paul@0 | 355 | if day == 1: |
paul@0 | 356 | if month == 1: |
paul@0 | 357 | return Date((year - 1, 12, 31)) |
paul@0 | 358 | else: |
paul@0 | 359 | _wd, end_day = calendar.monthrange(year, month - 1) |
paul@0 | 360 | return Date((year, month - 1, end_day)) |
paul@0 | 361 | else: |
paul@0 | 362 | return Date((year, month, day - 1)) |
paul@0 | 363 | |
paul@0 | 364 | previous = previous_day |
paul@0 | 365 | |
paul@0 | 366 | def days_until(self, end): |
paul@0 | 367 | |
paul@0 | 368 | "Return the collection of days from this date until 'end'." |
paul@0 | 369 | |
paul@0 | 370 | return self._until(self.as_date(), end.as_date(), Date.next_day, Date.previous_day) |
paul@0 | 371 | |
paul@0 | 372 | until = days_until |
paul@0 | 373 | |
paul@0 | 374 | class DateTime(Date): |
paul@0 | 375 | |
paul@0 | 376 | "A simple date plus time representation." |
paul@0 | 377 | |
paul@0 | 378 | def constrain(self): |
paul@0 | 379 | Date.constrain(self) |
paul@0 | 380 | |
paul@0 | 381 | hour, minute, second = self.as_tuple()[3:6] |
paul@0 | 382 | |
paul@0 | 383 | if self.has_time(): |
paul@0 | 384 | hour = max(min(hour, 23), 0) |
paul@0 | 385 | minute = max(min(minute, 59), 0) |
paul@0 | 386 | |
paul@0 | 387 | if second is not None: |
paul@0 | 388 | second = max(min(second, 60), 0) # support leap seconds |
paul@0 | 389 | |
paul@0 | 390 | self.data[3:6] = hour, minute, second |
paul@0 | 391 | |
paul@0 | 392 | def __str__(self): |
paul@0 | 393 | return Date.__str__(self) + self.time_string() |
paul@0 | 394 | |
paul@53 | 395 | def time_string(self, zone_as_offset=False, time_prefix=" ", zone_prefix=" "): |
paul@0 | 396 | if self.has_time(): |
paul@0 | 397 | data = self.as_tuple() |
paul@53 | 398 | time_str = "%s%02d:%02d" % ((time_prefix,) + data[3:5]) |
paul@0 | 399 | if data[5] is not None: |
paul@0 | 400 | time_str += ":%02d" % data[5] |
paul@0 | 401 | if data[6] is not None: |
paul@6 | 402 | if zone_as_offset: |
paul@6 | 403 | utc_offset = self.utc_offset() |
paul@6 | 404 | if utc_offset: |
paul@53 | 405 | time_str += "%s%+03d:%02d" % ((zone_prefix,) + utc_offset) |
paul@6 | 406 | else: |
paul@53 | 407 | time_str += "%s%s" % (zone_prefix, data[6]) |
paul@0 | 408 | return time_str |
paul@0 | 409 | else: |
paul@0 | 410 | return "" |
paul@0 | 411 | |
paul@0 | 412 | def as_HTTP_datetime_string(self): |
paul@0 | 413 | weekday = calendar.weekday(*self.data[:3]) |
paul@0 | 414 | return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (( |
paul@0 | 415 | getDayLabel(weekday), |
paul@0 | 416 | self.data[2], |
paul@0 | 417 | getMonthLabel(self.data[1]), |
paul@0 | 418 | self.data[0] |
paul@0 | 419 | ) + tuple(self.data[3:6])) |
paul@0 | 420 | |
paul@6 | 421 | def as_ISO8601_datetime_string(self): |
paul@53 | 422 | return Date.__str__(self) + self.time_string(zone_as_offset=True, time_prefix="T", zone_prefix="") |
paul@6 | 423 | |
paul@0 | 424 | def as_datetime(self): |
paul@0 | 425 | return self |
paul@0 | 426 | |
paul@0 | 427 | def as_date(self): |
paul@0 | 428 | return Date(self.data[:3]) |
paul@0 | 429 | |
paul@0 | 430 | def as_datetime_or_date(self): |
paul@0 | 431 | |
paul@0 | 432 | """ |
paul@0 | 433 | Return a date for this datetime if fields are missing. Otherwise, return |
paul@0 | 434 | this datetime itself. |
paul@0 | 435 | """ |
paul@0 | 436 | |
paul@0 | 437 | if not self.has_time(): |
paul@0 | 438 | return self.as_date() |
paul@0 | 439 | else: |
paul@0 | 440 | return self |
paul@0 | 441 | |
paul@0 | 442 | def __cmp__(self, other): |
paul@0 | 443 | |
paul@0 | 444 | """ |
paul@0 | 445 | The result of comparing this instance with 'other' is, if both instances |
paul@0 | 446 | are datetime instances, derived from a comparison of the datetimes |
paul@0 | 447 | converted to UTC. If one or both datetimes cannot be converted to UTC, |
paul@0 | 448 | the datetimes are compared using the basic temporal comparison which |
paul@0 | 449 | compares their raw time data. |
paul@0 | 450 | """ |
paul@0 | 451 | |
paul@0 | 452 | this = self |
paul@0 | 453 | |
paul@0 | 454 | if this.has_time(): |
paul@0 | 455 | if isinstance(other, DateTime): |
paul@0 | 456 | if other.has_time(): |
paul@0 | 457 | this_utc = this.to_utc() |
paul@0 | 458 | other_utc = other.to_utc() |
paul@0 | 459 | if this_utc is not None and other_utc is not None: |
paul@0 | 460 | return cmp(this_utc.as_tuple(), other_utc.as_tuple()) |
paul@0 | 461 | else: |
paul@0 | 462 | other = other.padded() |
paul@0 | 463 | else: |
paul@0 | 464 | this = this.padded() |
paul@0 | 465 | |
paul@0 | 466 | return Date.__cmp__(this, other) |
paul@0 | 467 | |
paul@8 | 468 | def __sub__(self, other): |
paul@8 | 469 | |
paul@8 | 470 | """ |
paul@8 | 471 | Return the difference between this object and the 'other' object at the |
paul@8 | 472 | highest common accuracy of both objects. |
paul@8 | 473 | """ |
paul@8 | 474 | |
paul@8 | 475 | if not isinstance(other, Temporal): |
paul@8 | 476 | return NotImplemented |
paul@8 | 477 | elif not other.has_time(): |
paul@8 | 478 | return self.as_date() - other |
paul@8 | 479 | else: |
paul@8 | 480 | utc = self.to_utc() |
paul@8 | 481 | other = other.to_utc() |
paul@8 | 482 | days = utc.as_date() - other.as_date() |
paul@8 | 483 | h1, m1, s1 = utc.as_tuple()[3:6] |
paul@8 | 484 | h2, m2, s2 = other.as_tuple()[3:6] |
paul@8 | 485 | return days * 24 * 3600 + (h1 - h2) * 3600 + (m1 - m2) * 60 + s1 - s2 |
paul@8 | 486 | |
paul@0 | 487 | def has_time(self): |
paul@0 | 488 | |
paul@0 | 489 | """ |
paul@0 | 490 | Return whether this object has any time information. Objects without |
paul@0 | 491 | time information can refer to the very start of a day. |
paul@0 | 492 | """ |
paul@0 | 493 | |
paul@0 | 494 | return self.data[3] is not None and self.data[4] is not None |
paul@0 | 495 | |
paul@0 | 496 | def time(self): |
paul@0 | 497 | return self.data[3:] |
paul@0 | 498 | |
paul@0 | 499 | def seconds(self): |
paul@0 | 500 | return self.data[5] |
paul@0 | 501 | |
paul@0 | 502 | def time_zone(self): |
paul@0 | 503 | return self.data[6] |
paul@0 | 504 | |
paul@0 | 505 | def set_time_zone(self, value): |
paul@0 | 506 | self.data[6] = value |
paul@0 | 507 | |
paul@0 | 508 | def padded(self, empty_value=0): |
paul@0 | 509 | |
paul@0 | 510 | """ |
paul@0 | 511 | Return a datetime with missing fields defined as being the given |
paul@0 | 512 | 'empty_value' or 0 if not specified. |
paul@0 | 513 | """ |
paul@0 | 514 | |
paul@0 | 515 | data = [] |
paul@0 | 516 | for x in self.data[:6]: |
paul@0 | 517 | if x is None: |
paul@0 | 518 | data.append(empty_value) |
paul@0 | 519 | else: |
paul@0 | 520 | data.append(x) |
paul@0 | 521 | |
paul@0 | 522 | data += self.data[6:] |
paul@0 | 523 | return DateTime(data) |
paul@0 | 524 | |
paul@0 | 525 | def to_utc(self): |
paul@0 | 526 | |
paul@0 | 527 | """ |
paul@0 | 528 | Return this object converted to UTC, or None if such a conversion is not |
paul@0 | 529 | defined. |
paul@0 | 530 | """ |
paul@0 | 531 | |
paul@0 | 532 | if not self.has_time(): |
paul@0 | 533 | return None |
paul@0 | 534 | |
paul@0 | 535 | offset = self.utc_offset() |
paul@0 | 536 | if offset: |
paul@0 | 537 | hours, minutes = offset |
paul@0 | 538 | |
paul@0 | 539 | # Invert the offset to get the correction. |
paul@0 | 540 | |
paul@0 | 541 | hours, minutes = -hours, -minutes |
paul@0 | 542 | |
paul@0 | 543 | # Get the components. |
paul@0 | 544 | |
paul@0 | 545 | hour, minute, second, zone = self.time() |
paul@0 | 546 | date = self.as_date() |
paul@0 | 547 | |
paul@0 | 548 | # Add the minutes and hours. |
paul@0 | 549 | |
paul@0 | 550 | minute += minutes |
paul@0 | 551 | if minute < 0 or minute > 59: |
paul@0 | 552 | hour += minute / 60 |
paul@0 | 553 | minute = minute % 60 |
paul@0 | 554 | |
paul@0 | 555 | # NOTE: This makes various assumptions and probably would not work |
paul@0 | 556 | # NOTE: for general arithmetic. |
paul@0 | 557 | |
paul@0 | 558 | hour += hours |
paul@0 | 559 | if hour < 0: |
paul@0 | 560 | date = date.previous_day() |
paul@0 | 561 | hour += 24 |
paul@0 | 562 | elif hour > 23: |
paul@0 | 563 | date = date.next_day() |
paul@0 | 564 | hour -= 24 |
paul@0 | 565 | |
paul@0 | 566 | return date.as_datetime(hour, minute, second, "UTC") |
paul@0 | 567 | |
paul@0 | 568 | # Cannot convert. |
paul@0 | 569 | |
paul@0 | 570 | else: |
paul@0 | 571 | return None |
paul@0 | 572 | |
paul@0 | 573 | def utc_offset(self): |
paul@0 | 574 | |
paul@0 | 575 | "Return the UTC offset in hours and minutes." |
paul@0 | 576 | |
paul@0 | 577 | zone = self.time_zone() |
paul@0 | 578 | if not zone: |
paul@0 | 579 | return None |
paul@0 | 580 | |
paul@0 | 581 | # Support explicit UTC zones. |
paul@0 | 582 | |
paul@0 | 583 | if zone == "UTC": |
paul@0 | 584 | return 0, 0 |
paul@0 | 585 | |
paul@0 | 586 | # Attempt to return a UTC offset where an explicit offset has been set. |
paul@0 | 587 | |
paul@0 | 588 | match = timezone_offset_regexp.match(zone) |
paul@0 | 589 | if match: |
paul@0 | 590 | if match.group("sign") == "-": |
paul@8 | 591 | offset_sign = -1 |
paul@0 | 592 | else: |
paul@8 | 593 | offset_sign = 1 |
paul@0 | 594 | |
paul@8 | 595 | hours = int(match.group("hours")) * offset_sign |
paul@8 | 596 | minutes = int(match.group("minutes") or 0) * offset_sign |
paul@0 | 597 | return hours, minutes |
paul@0 | 598 | |
paul@0 | 599 | # Attempt to handle Olson time zone identifiers. |
paul@0 | 600 | |
paul@0 | 601 | dt = self.as_olson_datetime() |
paul@0 | 602 | if dt: |
paul@7 | 603 | seconds = dt.utcoffset().seconds + dt.utcoffset().days * 24 * 3600 |
paul@60 | 604 | return getHoursAndMinutes(seconds) |
paul@0 | 605 | |
paul@0 | 606 | # Otherwise return None. |
paul@0 | 607 | |
paul@0 | 608 | return None |
paul@0 | 609 | |
paul@0 | 610 | def olson_identifier(self): |
paul@0 | 611 | |
paul@0 | 612 | "Return the Olson identifier from any zone information." |
paul@0 | 613 | |
paul@0 | 614 | zone = self.time_zone() |
paul@0 | 615 | if not zone: |
paul@0 | 616 | return None |
paul@0 | 617 | |
paul@0 | 618 | # Attempt to match an identifier. |
paul@0 | 619 | |
paul@0 | 620 | match = timezone_olson_regexp.match(zone) |
paul@0 | 621 | if match: |
paul@0 | 622 | return match.group("olson") |
paul@0 | 623 | else: |
paul@0 | 624 | return None |
paul@0 | 625 | |
paul@0 | 626 | def _as_olson_datetime(self, hours=None): |
paul@0 | 627 | |
paul@0 | 628 | """ |
paul@0 | 629 | Return a Python datetime object for this datetime interpreted using any |
paul@0 | 630 | Olson time zone identifier and the given 'hours' offset, raising one of |
paul@0 | 631 | the pytz exceptions in case of ambiguity. |
paul@0 | 632 | """ |
paul@0 | 633 | |
paul@0 | 634 | olson = self.olson_identifier() |
paul@77 | 635 | if olson: |
paul@0 | 636 | tz = pytz.timezone(olson) |
paul@0 | 637 | data = self.padded().as_tuple()[:6] |
paul@0 | 638 | dt = datetime.datetime(*data) |
paul@0 | 639 | |
paul@0 | 640 | # With an hours offset, find a time probably in a previously |
paul@0 | 641 | # applicable time zone. |
paul@0 | 642 | |
paul@0 | 643 | if hours is not None: |
paul@0 | 644 | td = datetime.timedelta(0, hours * 3600) |
paul@0 | 645 | dt += td |
paul@0 | 646 | |
paul@0 | 647 | ldt = tz.localize(dt, None) |
paul@0 | 648 | |
paul@0 | 649 | # With an hours offset, adjust the time to define it within the |
paul@0 | 650 | # previously applicable time zone but at the presumably intended |
paul@0 | 651 | # position. |
paul@0 | 652 | |
paul@0 | 653 | if hours is not None: |
paul@0 | 654 | ldt -= td |
paul@0 | 655 | |
paul@0 | 656 | return ldt |
paul@0 | 657 | else: |
paul@0 | 658 | return None |
paul@0 | 659 | |
paul@0 | 660 | def as_olson_datetime(self): |
paul@0 | 661 | |
paul@0 | 662 | """ |
paul@0 | 663 | Return a Python datetime object for this datetime interpreted using any |
paul@0 | 664 | Olson time zone identifier, choosing the time from the zone before the |
paul@0 | 665 | period of ambiguity. |
paul@0 | 666 | """ |
paul@0 | 667 | |
paul@77 | 668 | self._check_pytz() |
paul@77 | 669 | |
paul@0 | 670 | try: |
paul@0 | 671 | return self._as_olson_datetime() |
paul@78 | 672 | except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError, pytz.NonExistentTimeError): |
paul@0 | 673 | |
paul@0 | 674 | # Try again, using an earlier local time and then stepping forward |
paul@0 | 675 | # in the chosen zone. |
paul@0 | 676 | # NOTE: Four hours earlier seems reasonable. |
paul@0 | 677 | |
paul@0 | 678 | return self._as_olson_datetime(-4) |
paul@0 | 679 | |
paul@0 | 680 | def ambiguous(self): |
paul@0 | 681 | |
paul@0 | 682 | "Return whether the time is local and ambiguous." |
paul@0 | 683 | |
paul@77 | 684 | self._check_pytz() |
paul@77 | 685 | |
paul@0 | 686 | try: |
paul@0 | 687 | self._as_olson_datetime() |
paul@78 | 688 | except (pytz.UnknownTimeZoneError, pytz.AmbiguousTimeError, pytz.NonExistentTimeError): |
paul@0 | 689 | return 1 |
paul@0 | 690 | |
paul@0 | 691 | return 0 |
paul@0 | 692 | |
paul@77 | 693 | def _check_pytz(self): |
paul@77 | 694 | if not pytz: |
paul@77 | 695 | raise NotImplementedError, "pytz must be installed for Olson " \ |
paul@77 | 696 | "time zones to be supported" |
paul@77 | 697 | |
paul@0 | 698 | class Timespan(ActsAsTimespan, Convertible): |
paul@0 | 699 | |
paul@0 | 700 | """ |
paul@0 | 701 | A period of time which can be compared against others to check for overlaps. |
paul@0 | 702 | """ |
paul@0 | 703 | |
paul@0 | 704 | def __init__(self, start, end): |
paul@0 | 705 | self.start = start |
paul@0 | 706 | self.end = end |
paul@0 | 707 | |
paul@0 | 708 | # NOTE: Should perhaps catch ambiguous time problems elsewhere. |
paul@0 | 709 | |
paul@0 | 710 | if self.ambiguous() and self.start is not None and self.end is not None and start > end: |
paul@0 | 711 | self.start, self.end = end, start |
paul@0 | 712 | |
paul@0 | 713 | def __repr__(self): |
paul@0 | 714 | return "%s(%r, %r)" % (self.__class__.__name__, self.start, self.end) |
paul@0 | 715 | |
paul@0 | 716 | def __hash__(self): |
paul@0 | 717 | return hash((self.start, self.end)) |
paul@0 | 718 | |
paul@0 | 719 | def as_timespan(self): |
paul@0 | 720 | return self |
paul@0 | 721 | |
paul@0 | 722 | def as_limits(self): |
paul@0 | 723 | return self.start, self.end |
paul@0 | 724 | |
paul@0 | 725 | def ambiguous(self): |
paul@0 | 726 | return self.start is not None and self.start.ambiguous() or self.end is not None and self.end.ambiguous() |
paul@0 | 727 | |
paul@0 | 728 | def convert(self, resolution): |
paul@0 | 729 | return Timespan(*map(self._get_converter(resolution), self.as_limits())) |
paul@0 | 730 | |
paul@0 | 731 | def is_before(self, a, b): |
paul@0 | 732 | |
paul@0 | 733 | """ |
paul@0 | 734 | Return whether 'a' is before 'b'. Since the end datetime of one period |
paul@0 | 735 | may be the same as the start datetime of another period, and yet the |
paul@0 | 736 | first period is intended to be concluded by the end datetime and not |
paul@0 | 737 | overlap with the other period, a different test is employed for datetime |
paul@0 | 738 | comparisons. |
paul@0 | 739 | """ |
paul@0 | 740 | |
paul@0 | 741 | # Datetimes without times can be equal to dates and be considered as |
paul@0 | 742 | # occurring before those dates. Generally, datetimes should not be |
paul@0 | 743 | # produced without time information as getDateTime converts such |
paul@0 | 744 | # datetimes to dates. |
paul@0 | 745 | |
paul@0 | 746 | if isinstance(a, DateTime) and (isinstance(b, DateTime) or not a.has_time()): |
paul@0 | 747 | return a <= b |
paul@0 | 748 | else: |
paul@0 | 749 | return a < b |
paul@0 | 750 | |
paul@0 | 751 | def __contains__(self, other): |
paul@0 | 752 | |
paul@0 | 753 | """ |
paul@0 | 754 | This instance is considered to contain 'other' if one is not before or |
paul@0 | 755 | after the other. If this instance overlaps or coincides with 'other', |
paul@0 | 756 | then 'other' is regarded as belonging to this instance's time period. |
paul@0 | 757 | """ |
paul@0 | 758 | |
paul@0 | 759 | return self == other |
paul@0 | 760 | |
paul@0 | 761 | def __cmp__(self, other): |
paul@0 | 762 | |
paul@0 | 763 | """ |
paul@0 | 764 | Return whether this timespan occupies the same period of time as the |
paul@0 | 765 | 'other'. Timespans are considered less than others if their end points |
paul@0 | 766 | precede the other's start point, and are considered greater than others |
paul@0 | 767 | if their start points follow the other's end point. |
paul@0 | 768 | """ |
paul@0 | 769 | |
paul@0 | 770 | if isinstance(other, ActsAsTimespan): |
paul@0 | 771 | other = other.as_timespan() |
paul@0 | 772 | |
paul@50 | 773 | before = self.end is not None and other.start is not None and self.is_before(self.end, other.start) |
paul@50 | 774 | after = self.start is not None and other.end is not None and self.is_before(other.end, self.start) |
paul@50 | 775 | else: |
paul@50 | 776 | before = self.end is not None and self.is_before(self.end, other) |
paul@50 | 777 | after = self.start is not None and self.is_before(other, self.start) |
paul@0 | 778 | |
paul@50 | 779 | # Two identical points in time will be "before" each other according to |
paul@50 | 780 | # the is_before test. |
paul@50 | 781 | |
paul@50 | 782 | if not before and not after or before and after: |
paul@50 | 783 | return 0 |
paul@50 | 784 | elif before: |
paul@50 | 785 | return -1 |
paul@0 | 786 | else: |
paul@50 | 787 | return 1 |
paul@0 | 788 | |
paul@0 | 789 | class TimespanCollection: |
paul@0 | 790 | |
paul@0 | 791 | """ |
paul@0 | 792 | A class providing a list-like interface supporting membership tests at a |
paul@0 | 793 | particular resolution in order to maintain a collection of non-overlapping |
paul@0 | 794 | timespans. |
paul@0 | 795 | """ |
paul@0 | 796 | |
paul@0 | 797 | def __init__(self, resolution, values=None): |
paul@0 | 798 | self.resolution = resolution |
paul@0 | 799 | self.values = values or [] |
paul@0 | 800 | |
paul@0 | 801 | def as_timespan(self): |
paul@0 | 802 | return Timespan(*self.as_limits()) |
paul@0 | 803 | |
paul@0 | 804 | def as_limits(self): |
paul@0 | 805 | |
paul@0 | 806 | "Return the earliest and latest points in time for this collection." |
paul@0 | 807 | |
paul@0 | 808 | if not self.values: |
paul@0 | 809 | return None, None |
paul@0 | 810 | else: |
paul@0 | 811 | first, last = self.values[0], self.values[-1] |
paul@0 | 812 | if isinstance(first, ActsAsTimespan): |
paul@0 | 813 | first = first.as_timespan().start |
paul@0 | 814 | if isinstance(last, ActsAsTimespan): |
paul@0 | 815 | last = last.as_timespan().end |
paul@0 | 816 | return first, last |
paul@0 | 817 | |
paul@0 | 818 | def convert(self, value): |
paul@0 | 819 | if isinstance(value, ActsAsTimespan): |
paul@0 | 820 | ts = value.as_timespan() |
paul@0 | 821 | return ts and ts.convert(self.resolution) |
paul@0 | 822 | else: |
paul@0 | 823 | return value.convert(self.resolution) |
paul@0 | 824 | |
paul@0 | 825 | def __iter__(self): |
paul@0 | 826 | return iter(self.values) |
paul@0 | 827 | |
paul@0 | 828 | def __len__(self): |
paul@0 | 829 | return len(self.values) |
paul@0 | 830 | |
paul@0 | 831 | def __getitem__(self, i): |
paul@0 | 832 | return self.values[i] |
paul@0 | 833 | |
paul@0 | 834 | def __setitem__(self, i, value): |
paul@0 | 835 | self.values[i] = value |
paul@0 | 836 | |
paul@0 | 837 | def __contains__(self, value): |
paul@0 | 838 | test_value = self.convert(value) |
paul@0 | 839 | return test_value in self.values |
paul@0 | 840 | |
paul@0 | 841 | def append(self, value): |
paul@0 | 842 | self.values.append(value) |
paul@0 | 843 | |
paul@0 | 844 | def insert(self, i, value): |
paul@0 | 845 | self.values.insert(i, value) |
paul@0 | 846 | |
paul@0 | 847 | def pop(self): |
paul@0 | 848 | return self.values.pop() |
paul@0 | 849 | |
paul@0 | 850 | def insert_in_order(self, value): |
paul@0 | 851 | bisect.insort_left(self, value) |
paul@0 | 852 | |
paul@0 | 853 | def getDate(s): |
paul@0 | 854 | |
paul@0 | 855 | "Parse the string 's', extracting and returning a date object." |
paul@0 | 856 | |
paul@0 | 857 | dt = getDateTime(s) |
paul@0 | 858 | if dt is not None: |
paul@0 | 859 | return dt.as_date() |
paul@0 | 860 | else: |
paul@0 | 861 | return None |
paul@0 | 862 | |
paul@0 | 863 | def getDateTime(s): |
paul@0 | 864 | |
paul@0 | 865 | """ |
paul@0 | 866 | Parse the string 's', extracting and returning a datetime object where time |
paul@0 | 867 | information has been given or a date object where time information is |
paul@0 | 868 | absent. |
paul@0 | 869 | """ |
paul@0 | 870 | |
paul@0 | 871 | m = datetime_regexp.search(s) |
paul@0 | 872 | if m: |
paul@0 | 873 | groups = list(m.groups()) |
paul@0 | 874 | |
paul@0 | 875 | # Convert date and time data to integer or None. |
paul@0 | 876 | |
paul@0 | 877 | return DateTime(map(int_or_none, groups[:6]) + [m.group("zone")]).as_datetime_or_date() |
paul@0 | 878 | else: |
paul@0 | 879 | return None |
paul@0 | 880 | |
paul@0 | 881 | def getDateFromCalendar(s): |
paul@0 | 882 | |
paul@0 | 883 | """ |
paul@0 | 884 | Parse the iCalendar format string 's', extracting and returning a date |
paul@0 | 885 | object. |
paul@0 | 886 | """ |
paul@0 | 887 | |
paul@0 | 888 | dt = getDateTimeFromCalendar(s) |
paul@0 | 889 | if dt is not None: |
paul@0 | 890 | return dt.as_date() |
paul@0 | 891 | else: |
paul@0 | 892 | return None |
paul@0 | 893 | |
paul@0 | 894 | def getDateTimeFromCalendar(s): |
paul@0 | 895 | |
paul@0 | 896 | """ |
paul@0 | 897 | Parse the iCalendar format datetime string 's', extracting and returning a |
paul@0 | 898 | datetime object where time information has been given or a date object where |
paul@0 | 899 | time information is absent. |
paul@0 | 900 | """ |
paul@0 | 901 | |
paul@0 | 902 | m = datetime_icalendar_regexp.search(s) |
paul@0 | 903 | if m: |
paul@0 | 904 | groups = list(m.groups()) |
paul@0 | 905 | |
paul@0 | 906 | # Convert date and time data to integer or None. |
paul@0 | 907 | |
paul@0 | 908 | return DateTime(map(int_or_none, groups[:6]) + [m.group("utc") and "UTC" or None]).as_datetime_or_date() |
paul@0 | 909 | else: |
paul@0 | 910 | return None |
paul@0 | 911 | |
paul@55 | 912 | def getDateTimeFromISO8601(s): |
paul@55 | 913 | |
paul@55 | 914 | """ |
paul@55 | 915 | Parse the ISO 8601 format datetime string 's', returning a datetime object. |
paul@55 | 916 | """ |
paul@55 | 917 | |
paul@55 | 918 | m = datetime_iso8601_regexp.search(s) |
paul@55 | 919 | if m: |
paul@55 | 920 | groups = list(m.groups()) |
paul@55 | 921 | |
paul@55 | 922 | # Convert date and time data to integer or None. |
paul@55 | 923 | |
paul@55 | 924 | return DateTime(map(int_or_none, groups[:6]) + [m.group("utc") and "UTC" or m.group("zone")]).as_datetime_or_date() |
paul@55 | 925 | else: |
paul@55 | 926 | return None |
paul@55 | 927 | |
paul@60 | 928 | def getDateTimeFromRFC2822(s): |
paul@60 | 929 | |
paul@60 | 930 | """ |
paul@60 | 931 | Parse the RFC 2822 format datetime string 's', returning a datetime object. |
paul@60 | 932 | """ |
paul@60 | 933 | |
paul@60 | 934 | data = parsedate_tz(s) |
paul@60 | 935 | offset = data[9] |
paul@60 | 936 | return DateTime(data[:6] + ("%02d:%02d" % getHoursAndMinutes(offset),)) |
paul@60 | 937 | |
paul@60 | 938 | def getHoursAndMinutes(seconds): |
paul@60 | 939 | |
paul@60 | 940 | "Return an (hours, minutes) tuple for the given number of 'seconds'." |
paul@60 | 941 | |
paul@60 | 942 | hours = abs(seconds) / 3600 |
paul@60 | 943 | minutes = (abs(seconds) % 3600) / 60 |
paul@60 | 944 | return sign(seconds) * hours, sign(seconds) * minutes |
paul@60 | 945 | |
paul@0 | 946 | def getDateStrings(s): |
paul@0 | 947 | |
paul@0 | 948 | "Parse the string 's', extracting and returning all date strings." |
paul@0 | 949 | |
paul@0 | 950 | start = 0 |
paul@0 | 951 | m = date_regexp.search(s, start) |
paul@0 | 952 | l = [] |
paul@0 | 953 | while m: |
paul@0 | 954 | l.append("-".join(m.groups())) |
paul@0 | 955 | m = date_regexp.search(s, m.end()) |
paul@0 | 956 | return l |
paul@0 | 957 | |
paul@0 | 958 | def getMonth(s): |
paul@0 | 959 | |
paul@0 | 960 | "Parse the string 's', extracting and returning a month object." |
paul@0 | 961 | |
paul@0 | 962 | m = month_regexp.search(s) |
paul@0 | 963 | if m: |
paul@0 | 964 | return Month(map(int, m.groups())) |
paul@0 | 965 | else: |
paul@0 | 966 | return None |
paul@0 | 967 | |
paul@0 | 968 | def getCurrentDate(): |
paul@0 | 969 | |
paul@10 | 970 | "Return the current date as a Date instance." |
paul@0 | 971 | |
paul@0 | 972 | today = datetime.date.today() |
paul@0 | 973 | return Date((today.year, today.month, today.day)) |
paul@0 | 974 | |
paul@0 | 975 | def getCurrentMonth(): |
paul@0 | 976 | |
paul@10 | 977 | "Return the current month as a Month instance." |
paul@0 | 978 | |
paul@0 | 979 | today = datetime.date.today() |
paul@0 | 980 | return Month((today.year, today.month)) |
paul@0 | 981 | |
paul@0 | 982 | def getCurrentYear(): |
paul@0 | 983 | |
paul@0 | 984 | "Return the current year." |
paul@0 | 985 | |
paul@0 | 986 | today = datetime.date.today() |
paul@0 | 987 | return today.year |
paul@0 | 988 | |
paul@10 | 989 | def getCurrentTime(): |
paul@10 | 990 | |
paul@10 | 991 | "Return the current time as a DateTime instance." |
paul@10 | 992 | |
paul@10 | 993 | now = datetime.datetime.utcnow() |
paul@10 | 994 | return DateTime((now.year, now.month, now.day, now.hour, now.minute, now.second, "UTC")) |
paul@10 | 995 | |
paul@0 | 996 | # vim: tabstop=4 expandtab shiftwidth=4 |