fix timezones in darcs-fast-export, take 2
[bzr-fastimport/rorcz.git] / dates.py
blob209d069ddc54f268cd1dc3bbb2fe8d84dbdf3f3b
1 # Copyright (C) 2008 Canonical Ltd
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.
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.
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
17 """Date parsing routines.
19 Each routine returns timestamp,timezone where
21 * timestamp is seconds since epoch
22 * timezone is the offset from UTC in seconds.
23 """
26 import time
28 from bzrlib.plugins.fastimport import errors
31 def parse_raw(s, lineno=0):
32 """Parse a date from a raw string.
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
43 def _parse_tz(tz, lineno):
44 """Parse a timezone specification in the [+|-]HHMM format.
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)
57 def parse_rfc2822(s, lineno=0):
58 """Parse a date from a rfc2822 string.
60 See the spec for details.
61 """
62 raise NotImplementedError(parse_rfc2822)
65 def parse_now(s, lineno=0):
66 """Parse a date from a string.
68 The format must be exactly "now".
69 See the spec for details.
70 """
71 return time.time(), 0
74 # Lookup tabel of date parsing routines
75 DATE_PARSERS_BY_NAME = {
76 'raw': parse_raw,
77 'rfc2822': parse_rfc2822,
78 'now': parse_now,