~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/timestamp.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import calendar
18
18
import time
19
19
 
 
20
from bzrlib import osutils
 
21
 
20
22
 
21
23
def format_highres_date(t, offset=0):
22
24
    """Format a date, such that it includes higher precision in the
45
47
    >>> format_highres_date(1152428738.867522, 19800)
46
48
    'Sun 2006-07-09 12:35:38.867522001 +0530'
47
49
    """
48
 
    assert isinstance(t, float)
 
50
    if not isinstance(t, float):
 
51
        raise ValueError(t)
49
52
 
50
53
    # This has to be formatted for "original" date, so that the
51
54
    # revision XML entry will be reproduced faithfully.
53
56
        offset = 0
54
57
    tt = time.gmtime(t + offset)
55
58
 
56
 
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
 
59
    return (osutils.weekdays[tt[6]] +
 
60
            time.strftime(" %Y-%m-%d %H:%M:%S", tt)
57
61
            # Get the high-res seconds, but ignore the 0
58
62
            + ('%.9f' % (t - int(t)))[1:]
59
63
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
97
101
    ...      break
98
102
 
99
103
    """
 
104
    # Weekday parsing is locale sensitive, so drop the weekday
 
105
    space_loc = date.find(' ')
 
106
    if space_loc == -1 or date[:space_loc] not in osutils.weekdays:
 
107
        raise ValueError(
 
108
            'Date string does not contain a day of week: %r' % date)
100
109
    # Up until the first period is a datestamp that is generated
101
110
    # as normal from time.strftime, so use time.strptime to
102
111
    # parse it
104
113
    if dot_loc == -1:
105
114
        raise ValueError(
106
115
            'Date string does not contain high-precision seconds: %r' % date)
107
 
    base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
 
116
    base_time = time.strptime(date[space_loc:dot_loc], " %Y-%m-%d %H:%M:%S")
108
117
    fract_seconds, offset = date[dot_loc:].split()
109
118
    fract_seconds = float(fract_seconds)
110
119
 
127
136
 
128
137
    Inverse of parse_patch_date.
129
138
    """
130
 
    assert offset % 36 == 0
131
 
    tm = time.gmtime(secs+offset)
132
 
    time_str = time.strftime('%Y-%m-%d %H:%M:%S', tm)
133
 
    return '%s %+05d' % (time_str, offset/36)
 
139
    if offset % 60 != 0:
 
140
        raise ValueError(
 
141
        "can't represent timezone %s offset by fractional minutes" % offset)
 
142
    # so that we don't need to do calculations on pre-epoch times,
 
143
    # which doesn't work with win32 python gmtime, we always
 
144
    # give the epoch in utc
 
145
    if secs == 0:
 
146
        offset = 0
 
147
    if secs + offset < 0:
 
148
        from warnings import warn
 
149
        warn("gmtime of negative time (%s, %s) may not work on Windows" %
 
150
                (secs, offset))
 
151
    return osutils.format_date(secs, offset=offset,
 
152
            date_fmt='%Y-%m-%d %H:%M:%S')
134
153
 
135
154
 
136
155
def parse_patch_date(date_str):
139
158
    Inverse of format_patch_date.
140
159
    """
141
160
    secs_str = date_str[:-6]
142
 
    offset_str = date_str[-6:]
143
 
    offset = int(offset_str) * 36
 
161
    offset_str = date_str[-5:]
 
162
    if len(offset_str) != 5:
 
163
        raise ValueError(
 
164
            "invalid timezone %r" % offset_str)
 
165
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
 
166
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
144
167
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
145
168
    # adjust seconds according to offset before converting to POSIX
146
169
    # timestamp, to avoid edge problems