/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to dates.py

  • Committer: Ian Clatworthy
  • Date: 2009-02-17 23:37:24 UTC
  • mto: (0.64.114 trunk)
  • mto: This revision was merged to the branch mainline in revision 6631.
  • Revision ID: ian.clatworthy@canonical.com-20090217233724-y6q12cyoyln6vkh6
code & tests for file copying

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 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
 
 
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)
 
38
    return timestamp, timezone
 
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
    """
 
68
    return time.time(), 0
 
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
    }