1
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
21
def format_highres_date(t, offset=0):
34
31
this will return a date stamp for right now,
35
32
formatted for the local timezone.
37
>>> from breezy.osutils import format_date
34
>>> from bzrlib.osutils import format_date
38
35
>>> format_date(1120153132.350850105, 0)
39
36
'Thu 2005-06-30 17:38:52 +0000'
40
37
>>> format_highres_date(1120153132.350850105, 0)
48
45
>>> format_highres_date(1152428738.867522, 19800)
49
46
'Sun 2006-07-09 12:35:38.867522001 +0530'
51
if not isinstance(t, float):
48
assert isinstance(t, float)
54
50
# This has to be formatted for "original" date, so that the
55
51
# revision XML entry will be reproduced faithfully.
58
54
tt = time.gmtime(t + offset)
60
return (osutils.weekdays[tt[6]] + time.strftime(" %Y-%m-%d %H:%M:%S", tt) +
56
return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
61
57
# Get the high-res seconds, but ignore the 0
62
('%.9f' % (t - int(t)))[1:] +
63
' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
58
+ ('%.9f' % (t - int(t)))[1:]
59
+ ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
66
62
def unpack_highres_date(date):
72
68
:param date: A date formated by format_highres_date
71
>>> import time, random
72
>>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
73
(1120153132.3508501, -18000)
74
>>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
75
(1120153132.3508501, 0)
76
>>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
77
(1120153132.3508501, 7200)
78
>>> unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530')
79
(1152428738.867522, 19800)
80
>>> from bzrlib.osutils import local_time_offset
82
>>> o = local_time_offset()
83
>>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
88
>>> t -= 24*3600*365*2 # Start 2 years ago
90
>>> for count in xrange(500):
91
... t += random.random()*24*3600*30
92
... o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
93
... date = format_highres_date(t, o)
94
... t2, o2 = unpack_highres_date(date)
95
... if t != t2 or o != o2:
96
... print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
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:
80
'Date string does not contain a day of week: %r' % date)
81
100
# Up until the first period is a datestamp that is generated
82
101
# as normal from time.strftime, so use time.strptime to
87
106
'Date string does not contain high-precision seconds: %r' % date)
88
base_time = time.strptime(date[space_loc:dot_loc], " %Y-%m-%d %H:%M:%S")
107
base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
89
108
fract_seconds, offset = date[dot_loc:].split()
90
109
fract_seconds = float(fract_seconds)
109
128
Inverse of parse_patch_date.
113
"can't represent timezone %s offset by fractional minutes" % offset)
114
# so that we don't need to do calculations on pre-epoch times,
115
# which doesn't work with win32 python gmtime, we always
116
# give the epoch in utc
119
if secs + offset < 0:
120
from warnings import warn
121
warn("gmtime of negative time (%s, %s) may not work on Windows" %
123
return osutils.format_date(secs, offset=offset,
124
date_fmt='%Y-%m-%d %H:%M:%S')
127
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
128
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
129
RE_PATCHDATE = re.compile(
130
"(\\d+-\\d+-\\d+\\s+\\d+:\\d+:\\d+)\\s*([+-]\\d\\d)(\\d\\d)$")
131
RE_PATCHDATE_NOOFFSET = re.compile("\\d+-\\d+-\\d+\\s+\\d+:\\d+:\\d+$")
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)
134
136
def parse_patch_date(date_str):
137
139
Inverse of format_patch_date.
139
match = RE_PATCHDATE.match(date_str)
141
if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
142
raise ValueError("time data %r is missing a timezone offset"
145
raise ValueError("time data %r does not match format " % date_str +
146
"'%Y-%m-%d %H:%M:%S %z'")
147
secs_str = match.group(1)
148
offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
149
if abs(offset_hours) >= 24 or offset_mins >= 60:
150
raise ValueError("invalid timezone %r" %
151
(match.group(2) + match.group(3)))
152
offset = offset_hours * 3600 + offset_mins * 60
141
secs_str = date_str[:-6]
142
offset_str = date_str[-6:]
143
offset = int(offset_str) * 36
153
144
tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
154
145
# adjust seconds according to offset before converting to POSIX
155
146
# timestamp, to avoid edge problems