~bzr-pqm/bzr/bzr.dev

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
19
2425.6.2 by Martin Pool
Make timestamps use existing format_date; document that function more
20
from bzrlib import osutils
21
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
22
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
23
def format_highres_date(t, offset=0):
24
    """Format a date, such that it includes higher precision in the
25
    seconds field.
26
27
    :param t:   The local time in fractional seconds since the epoch
28
    :type t: float
29
    :param offset:  The timezone offset in integer seconds
30
    :type offset: int
31
32
    Example: format_highres_date(time.time(), -time.timezone)
33
    this will return a date stamp for right now,
34
    formatted for the local timezone.
35
36
    >>> from bzrlib.osutils import format_date
37
    >>> format_date(1120153132.350850105, 0)
38
    'Thu 2005-06-30 17:38:52 +0000'
39
    >>> format_highres_date(1120153132.350850105, 0)
40
    'Thu 2005-06-30 17:38:52.350850105 +0000'
41
    >>> format_date(1120153132.350850105, -5*3600)
42
    'Thu 2005-06-30 12:38:52 -0500'
43
    >>> format_highres_date(1120153132.350850105, -5*3600)
44
    'Thu 2005-06-30 12:38:52.350850105 -0500'
45
    >>> format_highres_date(1120153132.350850105, 7200)
46
    'Thu 2005-06-30 19:38:52.350850105 +0200'
47
    >>> format_highres_date(1152428738.867522, 19800)
48
    'Sun 2006-07-09 12:35:38.867522001 +0530'
49
    """
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
50
    if not isinstance(t, float):
51
        raise ValueError(t)
1551.12.28 by Aaron Bentley
Move bundle timestamp code to timestamp
52
53
    # This has to be formatted for "original" date, so that the
54
    # revision XML entry will be reproduced faithfully.
55
    if offset is None:
56
        offset = 0
57
    tt = time.gmtime(t + offset)
58
3512.3.1 by Martin von Gagern
Hand-selected minimalistic set of changes from my setlocale branch.
59
    return (osutils.weekdays[tt[6]] +
60
            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
62
            + ('%.9f' % (t - int(t)))[1:]
63
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
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(
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
119
    if secs + offset < 0:
120
        from warnings import warn
121
        warn("gmtime of negative time (%s, %s) may not work on Windows" %
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,
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
127
def parse_patch_date(date_str):
128
    """Parse a patch-style date into a POSIX timestamp and offset.
129
130
    Inverse of format_patch_date.
131
    """
132
    secs_str = date_str[:-6]
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
133
    offset_str = date_str[-5:]
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
134
    if len(offset_str) != 5:
135
        raise ValueError(
136
            "invalid timezone %r" % offset_str)
2425.6.1 by Martin Pool
Fix formatting of timezones in bundles and merge directives.
137
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
138
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
1551.12.29 by Aaron Bentley
Copy and extend patch date formatting code, add patch-date parsing
139
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
140
    # adjust seconds according to offset before converting to POSIX
141
    # timestamp, to avoid edge problems
142
    tm_time = tm_time[:5] + (tm_time[5] - offset,) + tm_time[6:]
143
    secs = calendar.timegm(tm_time)
144
    return secs, offset