git-remote-hg: improve sanitation of local repo urls
[git] / git_remote_helpers / fastimport / dates.py
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
19 Each routine returns timestamp,timezone where
20
21 * timestamp is seconds since epoch
22 * timezone is the offset from UTC in seconds.
23 """
24
25
26 import time
27
28 from git_remote_helpers.fastimport import errors
29
30
31 def parse_raw(s, lineno=0):
32     """Parse a date from a raw string.
33     
34     The format must be exactly "seconds-since-epoch offset-utc".
35     See the spec for details.
36     """
37     timestamp_str, timezone_str = s.split(' ', 1)
38     timestamp = float(timestamp_str)
39     timezone = _parse_tz(timezone_str, lineno)
40     return timestamp, timezone
41
42
43 def _parse_tz(tz, lineno):
44     """Parse a timezone specification in the [+|-]HHMM format.
45
46     :return: the timezone offset in seconds.
47     """
48     # from git_repository.py in bzr-git
49     if len(tz) != 5:
50         raise errors.InvalidTimezone(lineno, tz)
51     sign = {'+': +1, '-': -1}[tz[0]]
52     hours = int(tz[1:3])
53     minutes = int(tz[3:])
54     return sign * 60 * (60 * hours + minutes)
55
56
57 def parse_rfc2822(s, lineno=0):
58     """Parse a date from a rfc2822 string.
59     
60     See the spec for details.
61     """
62     raise NotImplementedError(parse_rfc2822)
63
64
65 def parse_now(s, lineno=0):
66     """Parse a date from a string.
67
68     The format must be exactly "now".
69     See the spec for details.
70     """
71     return time.time(), 0
72
73
74 # Lookup tabel of date parsing routines
75 DATE_PARSERS_BY_NAME = {
76     'raw':      parse_raw,
77     'rfc2822':  parse_rfc2822,
78     'now':      parse_now,
79     }