19
def format_highres_date(t, offset=0):
20
"""Format a date, such that it includes higher precision in the
23
:param t: The local time in fractional seconds since the epoch
25
:param offset: The timezone offset in integer seconds
28
Example: format_highres_date(time.time(), -time.timezone)
29
this will return a date stamp for right now,
30
formatted for the local timezone.
32
>>> from bzrlib.osutils import format_date
33
>>> format_date(1120153132.350850105, 0)
34
'Thu 2005-06-30 17:38:52 +0000'
35
>>> format_highres_date(1120153132.350850105, 0)
36
'Thu 2005-06-30 17:38:52.350850105 +0000'
37
>>> format_date(1120153132.350850105, -5*3600)
38
'Thu 2005-06-30 12:38:52 -0500'
39
>>> format_highres_date(1120153132.350850105, -5*3600)
40
'Thu 2005-06-30 12:38:52.350850105 -0500'
41
>>> format_highres_date(1120153132.350850105, 7200)
42
'Thu 2005-06-30 19:38:52.350850105 +0200'
45
assert isinstance(t, float)
47
# This has to be formatted for "original" date, so that the
48
# revision XML entry will be reproduced faithfully.
51
tt = time.gmtime(t + offset)
53
return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
54
+ ('%.9f' % (t - int(t)))[1:] # Get the high-res seconds, but
56
+ ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
59
def unpack_highres_date(date):
60
"""This takes the high-resolution date stamp, and
61
converts it back into the tuple (timestamp, timezone)
62
Where timestamp is in real UTC since epoch seconds, and timezone is an
63
integer number of seconds offset.
65
:param date: A date formated by format_highres_date
69
>>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
70
(1120153132.3508501, -18000)
71
>>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
72
(1120153132.3508501, 0)
73
>>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
74
(1120153132.3508501, 7200)
75
>>> from bzrlib.osutils import local_time_offset
77
>>> o = local_time_offset()
78
>>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
85
# Up until the first period is a datestamp that is generated
86
# as normal from time.strftime, so use time.strptime to
88
dot_loc = date.find('.')
90
raise ValueError('Date string does not contain high-precision seconds:'
92
base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
93
fract_seconds, offset = date[dot_loc:].split()
94
fract_seconds = float(fract_seconds)
96
offset = int(offset / 100) * 3600 + offset % 100
98
# time.mktime returns localtime, but calendar.timegm returns UTC time
99
timestamp = calendar.timegm(base_time)
101
# Add back in the fractional seconds
102
timestamp += fract_seconds
103
return (timestamp, offset)
106
19
if __name__ == '__main__':