/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
16
17
import calendar
18
import time
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
19
import re
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from . import osutils
2425.6.2 by Martin Pool
Make timestamps use existing format_date; document that function more
22
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
23
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
24
def format_highres_date(t, offset=0):
25
    """Format a date, such that it includes higher precision in the
26
    seconds field.
27
28
    :param t:   The local time in fractional seconds since the epoch
29
    :type t: float
30
    :param offset:  The timezone offset in integer seconds
31
    :type offset: int
32
33
    Example: format_highres_date(time.time(), -time.timezone)
34
    this will return a date stamp for right now,
35
    formatted for the local timezone.
36
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
37
    >>> from breezy.osutils import format_date
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
38
    >>> format_date(1120153132.350850105, 0)
39
    'Thu 2005-06-30 17:38:52 +0000'
40
    >>> format_highres_date(1120153132.350850105, 0)
41
    'Thu 2005-06-30 17:38:52.350850105 +0000'
42
    >>> format_date(1120153132.350850105, -5*3600)
43
    'Thu 2005-06-30 12:38:52 -0500'
44
    >>> format_highres_date(1120153132.350850105, -5*3600)
45
    'Thu 2005-06-30 12:38:52.350850105 -0500'
46
    >>> format_highres_date(1120153132.350850105, 7200)
47
    'Thu 2005-06-30 19:38:52.350850105 +0200'
48
    >>> format_highres_date(1152428738.867522, 19800)
49
    'Sun 2006-07-09 12:35:38.867522001 +0530'
50
    """
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
51
    if not isinstance(t, float):
52
        raise ValueError(t)
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
53
54
    # This has to be formatted for "original" date, so that the
55
    # revision XML entry will be reproduced faithfully.
56
    if offset is None:
57
        offset = 0
6840.1.1 by Jelmer Vernooij
Revert custom gmtime implementation without tests.
58
    tt = time.gmtime(t + offset)
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
59
7143.15.2 by Jelmer Vernooij
Run autopep8.
60
    return (osutils.weekdays[tt[6]] + time.strftime(" %Y-%m-%d %H:%M:%S", tt) +
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
61
            # Get the high-res seconds, but ignore the 0
7143.15.2 by Jelmer Vernooij
Run autopep8.
62
            ('%.9f' % (t - int(t)))[1:] +
63
            ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
64
65
66
def unpack_highres_date(date):
67
    """This takes the high-resolution date stamp, and
68
    converts it back into the tuple (timestamp, timezone)
69
    Where timestamp is in real UTC since epoch seconds, and timezone is an
70
    integer number of seconds offset.
71
72
    :param date: A date formated by format_highres_date
73
    :type date: string
74
75
    """
3512.3.1 by Martin von Gagern
Hand-selected minimalistic set of changes from my setlocale branch.
76
    # Weekday parsing is locale sensitive, so drop the weekday
77
    space_loc = date.find(' ')
78
    if space_loc == -1 or date[:space_loc] not in osutils.weekdays:
79
        raise ValueError(
80
            'Date string does not contain a day of week: %r' % date)
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
81
    # Up until the first period is a datestamp that is generated
82
    # as normal from time.strftime, so use time.strptime to
83
    # parse it
84
    dot_loc = date.find('.')
85
    if dot_loc == -1:
86
        raise ValueError(
87
            'Date string does not contain high-precision seconds: %r' % date)
3512.3.1 by Martin von Gagern
Hand-selected minimalistic set of changes from my setlocale branch.
88
    base_time = time.strptime(date[space_loc:dot_loc], " %Y-%m-%d %H:%M:%S")
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
89
    fract_seconds, offset = date[dot_loc:].split()
90
    fract_seconds = float(fract_seconds)
91
92
    offset = int(offset)
93
94
    hours = int(offset / 100)
95
    minutes = (offset % 100)
96
    seconds_offset = (hours * 3600) + (minutes * 60)
97
98
    # time.mktime returns localtime, but calendar.timegm returns UTC time
99
    timestamp = calendar.timegm(base_time)
100
    timestamp -= seconds_offset
101
    # Add back in the fractional seconds
102
    timestamp += fract_seconds
103
    return (timestamp, seconds_offset)
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
104
105
106
def format_patch_date(secs, offset=0):
107
    """Format a POSIX timestamp and optional offset as a patch-style date.
108
109
    Inverse of parse_patch_date.
110
    """
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
111
    if offset % 60 != 0:
112
        raise ValueError(
7143.15.2 by Jelmer Vernooij
Run autopep8.
113
            "can't represent timezone %s offset by fractional minutes" % offset)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
114
    # so that we don't need to do calculations on pre-epoch times,
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
115
    # which doesn't work with win32 python gmtime, we always
116
    # give the epoch in utc
117
    if secs == 0:
118
        offset = 0
6840.1.1 by Jelmer Vernooij
Revert custom gmtime implementation without tests.
119
    if secs + offset < 0:
120
        from warnings import warn
121
        warn("gmtime of negative time (%s, %s) may not work on Windows" %
7143.15.2 by Jelmer Vernooij
Run autopep8.
122
             (secs, offset))
2425.6.2 by Martin Pool
Make timestamps use existing format_date; document that function more
123
    return osutils.format_date(secs, offset=offset,
7143.15.2 by Jelmer Vernooij
Run autopep8.
124
                               date_fmt='%Y-%m-%d %H:%M:%S')
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
125
126
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
127
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
128
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
7143.15.2 by Jelmer Vernooij
Run autopep8.
129
RE_PATCHDATE = re.compile(
130
    "(\\d+-\\d+-\\d+\\s+\\d+:\\d+:\\d+)\\s*([+-]\\d\\d)(\\d\\d)$")
6798.1.1 by Jelmer Vernooij
Properly escape backslashes.
131
RE_PATCHDATE_NOOFFSET = re.compile("\\d+-\\d+-\\d+\\s+\\d+:\\d+:\\d+$")
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
132
7143.15.2 by Jelmer Vernooij
Run autopep8.
133
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
134
def parse_patch_date(date_str):
135
    """Parse a patch-style date into a POSIX timestamp and offset.
136
137
    Inverse of format_patch_date.
138
    """
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
139
    match = RE_PATCHDATE.match(date_str)
140
    if match is None:
6280.2.3 by Matt Giuca
bzrlib.timestamp: Better error message if the string is missing a timezone offset.
141
        if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
142
            raise ValueError("time data %r is missing a timezone offset"
7143.15.2 by Jelmer Vernooij
Run autopep8.
143
                             % date_str)
6280.2.3 by Matt Giuca
bzrlib.timestamp: Better error message if the string is missing a timezone offset.
144
        else:
7143.15.2 by Jelmer Vernooij
Run autopep8.
145
            raise ValueError("time data %r does not match format " % date_str +
146
                             "'%Y-%m-%d %H:%M:%S %z'")
6280.2.1 by Matt Giuca
bzrlib.timestamp: More robust handling of time stamp string. (LP: #892657)
147
    secs_str = match.group(1)
6280.2.2 by Matt Giuca
bzrlib.timestamp: Now checks offset hour and minute to ensure they are within correct range.
148
    offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
149
    if abs(offset_hours) >= 24 or offset_mins >= 60:
150
        raise ValueError("invalid timezone %r" %
7143.15.2 by Jelmer Vernooij
Run autopep8.
151
                         (match.group(2) + match.group(3)))
6280.2.2 by Matt Giuca
bzrlib.timestamp: Now checks offset hour and minute to ensure they are within correct range.
152
    offset = offset_hours * 3600 + offset_mins * 60
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
153
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
154
    # adjust seconds according to offset before converting to POSIX
155
    # timestamp, to avoid edge problems
156
    tm_time = tm_time[:5] + (tm_time[5] - offset,) + tm_time[6:]
157
    secs = calendar.timegm(tm_time)
158
    return secs, offset