~zeitgeist/zeitgeist/remove-datahub

« back to all changes in this revision

Viewing changes to _zeitgeist/loggers/iso_strptime.py

  • Committer: Seif Lotfy
  • Date: 2010-10-13 15:33:17 UTC
  • Revision ID: seif@lotfy.com-20101013153317-883z7f4mfe5d7to0
removed datahub and logger as an initiative to start using the vala datahub

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -.- coding: utf-8 -.-
2
 
 
3
 
# This file is part of wadllib.
4
 
#
5
 
# Copyright © 2009 Canonical Ltd.
6
 
#
7
 
# wadllib is free software: you can redistribute it and/or modify it under the
8
 
# terms of the GNU Lesser General Public License as published by the Free
9
 
# Software Foundation, version 3 of the License.
10
 
#
11
 
# wadllib is distributed in the hope that it will be useful, but WITHOUT ANY
12
 
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14
 
# details.
15
 
#
16
 
# You should have received a copy of the GNU Lesser General Public License
17
 
# along with wadllib. If not, see <http://www.gnu.org/licenses/>.
18
 
 
19
 
"""
20
 
Parser for ISO 8601 time strings
21
 
================================
22
 
 
23
 
>>> d = iso_strptime("2008-01-07T05:30:30.345323+03:00")
24
 
>>> d
25
 
datetime.datetime(2008, 1, 7, 5, 30, 30, 345323, tzinfo=TimeZone(10800))
26
 
>>> d.timetuple()
27
 
(2008, 1, 7, 5, 30, 30, 0, 7, 0)
28
 
>>> d.utctimetuple()
29
 
(2008, 1, 7, 2, 30, 30, 0, 7, 0)
30
 
>>> iso_strptime("2008-01-07T05:30:30.345323-03:00")
31
 
datetime.datetime(2008, 1, 7, 5, 30, 30, 345323, tzinfo=TimeZone(-10800))
32
 
>>> iso_strptime("2008-01-07T05:30:30.345323")
33
 
datetime.datetime(2008, 1, 7, 5, 30, 30, 345323)
34
 
>>> iso_strptime("2008-01-07T05:30:30")
35
 
datetime.datetime(2008, 1, 7, 5, 30, 30)
36
 
>>> iso_strptime("2008-01-07T05:30:30+02:00")
37
 
datetime.datetime(2008, 1, 7, 5, 30, 30, tzinfo=TimeZone(7200))
38
 
"""
39
 
 
40
 
import re
41
 
import datetime
42
 
 
43
 
RE_TIME = re.compile(r"""^
44
 
   # pattern matching date
45
 
   (?P<year>\d{4})\-(?P<month>\d{2})\-(?P<day>\d{2})
46
 
   # separator
47
 
   T
48
 
   # pattern matching time
49
 
   (?P<hour>\d{2})\:(?P<minutes>\d{2})\:(?P<seconds>\d{2})
50
 
   # pattern matching optional microseconds
51
 
   (\.(?P<microseconds>\d{6})\d*)?
52
 
   # pattern matching optional timezone offset
53
 
   (?P<tz_offset>[\-\+]\d{2}\:\d{2})?
54
 
   $""", re.VERBOSE)
55
 
 
56
 
class TimeZone(datetime.tzinfo):
57
 
 
58
 
    def __init__(self, tz_string):
59
 
        hours, minutes = tz_string.lstrip("-+").split(":")
60
 
        self.stdoffset = datetime.timedelta(hours=int(hours),
61
 
                                            minutes=int(minutes))
62
 
        if tz_string.startswith("-"):
63
 
            self.stdoffset *= -1
64
 
 
65
 
    def __repr__(self):
66
 
        return "TimeZone(%s)" % (
67
 
            self.stdoffset.days*24*60*60 + self.stdoffset.seconds)
68
 
 
69
 
    def utcoffset(self, dt):
70
 
        return self.stdoffset
71
 
 
72
 
    def dst(self, dt):
73
 
        return datetime.timedelta(0)
74
 
 
75
 
def iso_strptime(time_str):
76
 
    x = RE_TIME.match(time_str)
77
 
    if not x:
78
 
        raise ValueError("unable to parse time '%s'" %time_str)
79
 
    d = datetime.datetime(int(x.group("year")), int(x.group("month")),
80
 
        int(x.group("day")), int(x.group("hour")), int(x.group("minutes")),
81
 
        int(x.group("seconds")))
82
 
    if x.group("microseconds"):
83
 
        d = d.replace(microsecond=int(x.group("microseconds")))
84
 
    if x.group("tz_offset"):
85
 
        d = d.replace(tzinfo=TimeZone(x.group("tz_offset")))
86
 
    return d