~ubuntu-branches/debian/jessie/subunit/jessie

« back to all changes in this revision

Viewing changes to python/subunit/iso8601.py

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2011-10-31 02:02:11 UTC
  • mfrom: (1.1.6) (3.2.8 sid)
  • Revision ID: package-import@ubuntu.com-20111031020211-wz4nxhqiw8k23xsj
Tags: 0.0.7-1
* New upstream release.
* Add support for multi-arch.
 + Switch to debhelper >= 8.1.3, compat level 9.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
from datetime import datetime, timedelta, tzinfo
33
33
import re
 
34
import sys
34
35
 
35
36
__all__ = ["parse_date", "ParseError"]
36
37
 
37
38
# Adapted from http://delete.me.uk/2005/03/iso8601.html
38
 
ISO8601_REGEX = re.compile(r"(?P<year>[0-9]{4})(-(?P<month>[0-9]{1,2})(-(?P<day>[0-9]{1,2})"
 
39
ISO8601_REGEX_PATTERN = (r"(?P<year>[0-9]{4})(-(?P<month>[0-9]{1,2})(-(?P<day>[0-9]{1,2})"
39
40
    r"((?P<separator>.)(?P<hour>[0-9]{2}):(?P<minute>[0-9]{2})(:(?P<second>[0-9]{2})(\.(?P<fraction>[0-9]+))?)?"
40
41
    r"(?P<timezone>Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?"
41
42
)
42
 
TIMEZONE_REGEX = re.compile("(?P<prefix>[+-])(?P<hours>[0-9]{2}).(?P<minutes>[0-9]{2})")
 
43
TIMEZONE_REGEX_PATTERN = "(?P<prefix>[+-])(?P<hours>[0-9]{2}).(?P<minutes>[0-9]{2})"
 
44
ISO8601_REGEX = re.compile(ISO8601_REGEX_PATTERN.encode('utf8'))
 
45
TIMEZONE_REGEX = re.compile(TIMEZONE_REGEX_PATTERN.encode('utf8'))
 
46
 
 
47
zulu = "Z".encode('latin-1')
 
48
minus = "-".encode('latin-1')
 
49
 
 
50
if sys.version_info < (3, 0):
 
51
    bytes = str
 
52
 
43
53
 
44
54
class ParseError(Exception):
45
55
    """Raised when there is a problem parsing a date string"""
84
94
    """Parses ISO 8601 time zone specs into tzinfo offsets
85
95
    
86
96
    """
87
 
    if tzstring == "Z":
 
97
    if tzstring == zulu:
88
98
        return default_timezone
89
99
    # This isn't strictly correct, but it's common to encounter dates without
90
100
    # timezones so I'll assume the default (which defaults to UTC).
94
104
    m = TIMEZONE_REGEX.match(tzstring)
95
105
    prefix, hours, minutes = m.groups()
96
106
    hours, minutes = int(hours), int(minutes)
97
 
    if prefix == "-":
 
107
    if prefix == minus:
98
108
        hours = -hours
99
109
        minutes = -minutes
100
110
    return FixedOffset(hours, minutes, tzstring)
107
117
    default timezone specified in default_timezone is used. This is UTC by
108
118
    default.
109
119
    """
110
 
    if not isinstance(datestring, basestring):
111
 
        raise ParseError("Expecting a string %r" % datestring)
 
120
    if not isinstance(datestring, bytes):
 
121
        raise ParseError("Expecting bytes %r" % datestring)
112
122
    m = ISO8601_REGEX.match(datestring)
113
123
    if not m:
114
124
        raise ParseError("Unable to parse date string %r" % datestring)