~zyga/bzr-fastimport/fixes

« back to all changes in this revision

Viewing changes to dates.py

  • Committer: Ian Clatworthy
  • Date: 2008-02-14 06:28:42 UTC
  • Revision ID: ian.clatworthy@internode.on.net-20080214062842-x2yy4rk70ny1ounp
1st cut: gfi parser + --info processing method

Show diffs side-by-side

added added

removed removed

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