64
64
"""Build the final description of the tree, based on
65
65
the changeset_info object.
67
self.base_tree = self.branch.revision_tree(self.changeset_info.base)
68
69
def format_highres_date(t, offset=0):
69
70
"""Format a date, such that it includes higher precision in the
72
:param t: UTC time in fractional seconds
73
:param t: The local time in fractional seconds since the epoch
74
75
:param offset: The timezone offset in integer seconds
78
Example: format_highres_date(time.time(), -time.timezone)
79
this will return a date stamp for right now,
80
formatted for the local timezone.
77
82
>>> from bzrlib.osutils import format_date
78
83
>>> format_date(1120153132.350850105, 0)
79
84
'Thu 2005-06-30 17:38:52 +0000'
83
88
'Thu 2005-06-30 12:38:52 -0500'
84
89
>>> format_highres_date(1120153132.350850105, -5*3600)
85
90
'Thu 2005-06-30 12:38:52.350850105 -0500'
91
>>> format_highres_date(1120153132.350850105, 7200)
92
'Thu 2005-06-30 19:38:52.350850105 +0200'
87
from bzrlib.errors import BzrError
89
95
assert isinstance(t, float)
107
113
:param date: A date formated by format_highres_date
108
114
:type date: string
116
>>> import time, random
117
>>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
118
(1120153132.3508501, -18000)
119
>>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
120
(1120153132.3508501, 0)
121
>>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
122
(1120153132.3508501, 7200)
123
>>> from bzrlib.osutils import local_time_offset
125
>>> o = local_time_offset()
126
>>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
131
>>> for count in xrange(500):
132
... t += random.random()*24*3600*365*2 - 24*3600*364 # Random time within +/- 1 year
133
... o = random.randint(-12,12)*3600 # Random timezone
134
... date = format_highres_date(t, o)
135
... t2, o2 = unpack_highres_date(date)
136
... if t != t2 or o != o2:
137
... print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
140
#from bzrlib.errors import BzrError
141
from bzrlib.osutils import local_time_offset
143
# Up until the first period is a datestamp that is generated
144
# as normal from time.strftime, so use time.strptime to
146
dot_loc = date.find('.')
148
raise ValueError('Date string does not contain high-precision seconds: %r' % date)
149
base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
150
fract_seconds, offset = date[dot_loc:].split()
151
fract_seconds = float(fract_seconds)
153
offset = int(offset / 100) * 3600 + offset % 100
155
# mktime returns the a local timestamp, not the timestamp based
156
# on the offset given in the file, so we need to adjust based
157
# on what the local offset is, and then re-adjust based on
159
timestamp = time.mktime(base_time)
160
timestamp += local_time_offset(timestamp) - offset
161
# Add back in the fractional seconds
162
timestamp += fract_seconds
163
return (timestamp, offset)
112
165
if __name__ == '__main__':
114
167
doctest.testmod()