bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.64.1
by Ian Clatworthy
1st cut: gfi parser + --info processing method |
1 |
# Copyright (C) 2008 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Date parsing routines.
|
|
18 |
||
|
0.64.5
by Ian Clatworthy
first cut at generic processing method |
19 |
Each routine returns timestamp,timezone where
|
20 |
||
21 |
* timestamp is seconds since epoch
|
|
22 |
* timezone is the offset from UTC in seconds.
|
|
|
0.64.1
by Ian Clatworthy
1st cut: gfi parser + --info processing method |
23 |
"""
|
24 |
||
25 |
||
|
0.64.5
by Ian Clatworthy
first cut at generic processing method |
26 |
import time |
|
0.64.1
by Ian Clatworthy
1st cut: gfi parser + --info processing method |
27 |
|
28 |
||
29 |
def parse_raw(s): |
|
30 |
"""Parse a date from a raw string. |
|
31 |
|
|
32 |
The format must be exactly "seconds-since-epoch offset-utc".
|
|
33 |
See the spec for details.
|
|
34 |
"""
|
|
35 |
timestamp_str, timezone_str = s.split(' ', 1) |
|
36 |
timestamp = float(timestamp_str) |
|
37 |
timezone = _parse_tz(timezone_str) |
|
|
0.64.5
by Ian Clatworthy
first cut at generic processing method |
38 |
return timestamp, timezone |
|
0.64.1
by Ian Clatworthy
1st cut: gfi parser + --info processing method |
39 |
|
40 |
||
41 |
def _parse_tz(tz): |
|
42 |
"""Parse a timezone specification in the [+|-]HHMM format. |
|
43 |
||
44 |
:return: the timezone offset in seconds.
|
|
45 |
"""
|
|
46 |
# from git_repository.py in bzr-git
|
|
47 |
assert len(tz) == 5 |
|
48 |
sign = {'+': +1, '-': -1}[tz[0]] |
|
49 |
hours = int(tz[1:3]) |
|
50 |
minutes = int(tz[3:]) |
|
51 |
return sign * 60 * (60 * hours + minutes) |
|
52 |
||
53 |
||
54 |
def parse_rfc2822(s): |
|
55 |
"""Parse a date from a rfc2822 string. |
|
56 |
|
|
57 |
See the spec for details.
|
|
58 |
"""
|
|
59 |
raise NotImplementedError(parse_rfc2822) |
|
60 |
||
61 |
||
62 |
def parse_now(s): |
|
63 |
"""Parse a date from a string. |
|
64 |
||
65 |
The format must be exactly "now".
|
|
66 |
See the spec for details.
|
|
67 |
"""
|
|
|
0.64.5
by Ian Clatworthy
first cut at generic processing method |
68 |
return time.time(), 0 |
|
0.64.1
by Ian Clatworthy
1st cut: gfi parser + --info processing method |
69 |
|
70 |
||
71 |
# Lookup tabel of date parsing routines
|
|
72 |
DATE_PARSERS_BY_NAME = { |
|
73 |
'raw': parse_raw, |
|
74 |
'rfc2822': parse_rfc2822, |
|
75 |
'now': parse_now, |
|
76 |
}
|