~vmiklos/bzr-fastimport/darcs

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
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.
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
23
"""
24
25
5 by Ian Clatworthy
first cut at generic processing method
26
import time
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
27
136.1.1 by Ian Clatworthy
improve error msg when bad timezone encountered
28
from bzrlib.plugins.fastimport import errors
29
30
31
def parse_raw(s, lineno=0):
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
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)
136.1.1 by Ian Clatworthy
improve error msg when bad timezone encountered
39
    timezone = _parse_tz(timezone_str, lineno)
5 by Ian Clatworthy
first cut at generic processing method
40
    return timestamp, timezone
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
41
42
136.1.1 by Ian Clatworthy
improve error msg when bad timezone encountered
43
def _parse_tz(tz, lineno):
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
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
136.1.1 by Ian Clatworthy
improve error msg when bad timezone encountered
49
    if len(tz) != 5:
137 by Ian Clatworthy
improve error msg when bad timezone encountered
50
        raise errors.InvalidTimezone(lineno, tz)
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
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
136.1.1 by Ian Clatworthy
improve error msg when bad timezone encountered
57
def parse_rfc2822(s, lineno=0):
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
58
    """Parse a date from a rfc2822 string.
59
    
60
    See the spec for details.
61
    """
62
    raise NotImplementedError(parse_rfc2822)
63
64
136.1.1 by Ian Clatworthy
improve error msg when bad timezone encountered
65
def parse_now(s, lineno=0):
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
66
    """Parse a date from a string.
67
68
    The format must be exactly "now".
69
    See the spec for details.
70
    """
5 by Ian Clatworthy
first cut at generic processing method
71
    return time.time(), 0
1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
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
    }