~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/timestamp.py

  • Committer: John Arbash Meinel
  • Date: 2007-04-28 15:04:17 UTC
  • mfrom: (2466 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2566.
  • Revision ID: john@arbash-meinel.com-20070428150417-trp3pi0pzd411pu4
[merge] bzr.dev 2466

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
127
129
 
128
130
    Inverse of parse_patch_date.
129
131
    """
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)
 
132
    assert offset % 60 == 0, \
 
133
        "can't represent timezone %s offset by fractional minutes" % offset
 
134
    # so that we don't need to do calculations on pre-epoch times, 
 
135
    # which doesn't work with win32 python gmtime, we always
 
136
    # give the epoch in utc
 
137
    if secs == 0:
 
138
        offset = 0
 
139
    if secs + offset < 0:
 
140
        from warnings import warn
 
141
        warn("gmtime of negative time (%s, %s) may not work on Windows" %
 
142
                (secs, offset))
 
143
    return osutils.format_date(secs, offset=offset,
 
144
            date_fmt='%Y-%m-%d %H:%M:%S')
134
145
 
135
146
 
136
147
def parse_patch_date(date_str):
139
150
    Inverse of format_patch_date.
140
151
    """
141
152
    secs_str = date_str[:-6]
142
 
    offset_str = date_str[-6:]
143
 
    offset = int(offset_str) * 36
 
153
    offset_str = date_str[-5:]
 
154
    assert len(offset_str) == 5, \
 
155
            "invalid timezone %r" % offset_str
 
156
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
 
157
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
144
158
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
145
159
    # adjust seconds according to offset before converting to POSIX
146
160
    # timestamp, to avoid edge problems