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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
20
from bzrlib import osutils
26
23
def format_highres_date(t, offset=0):
36
33
this will return a date stamp for right now,
37
34
formatted for the local timezone.
39
>>> from breezy.osutils import format_date
36
>>> from bzrlib.osutils import format_date
40
37
>>> format_date(1120153132.350850105, 0)
41
38
'Thu 2005-06-30 17:38:52 +0000'
42
39
>>> format_highres_date(1120153132.350850105, 0)
60
57
tt = time.gmtime(t + offset)
62
return (osutils.weekdays[tt[6]] + time.strftime(" %Y-%m-%d %H:%M:%S", tt) +
59
return (osutils.weekdays[tt[6]] +
60
time.strftime(" %Y-%m-%d %H:%M:%S", tt)
63
61
# Get the high-res seconds, but ignore the 0
64
('%.9f' % (t - int(t)))[1:] +
65
' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
62
+ ('%.9f' % (t - int(t)))[1:]
63
+ ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
68
66
def unpack_highres_date(date):
74
72
:param date: A date formated by format_highres_date
75
>>> import time, random
76
>>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
77
(1120153132.3508501, -18000)
78
>>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
79
(1120153132.3508501, 0)
80
>>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
81
(1120153132.3508501, 7200)
82
>>> unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530')
83
(1152428738.867522, 19800)
84
>>> from bzrlib.osutils import local_time_offset
86
>>> o = local_time_offset()
87
>>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
92
>>> t -= 24*3600*365*2 # Start 2 years ago
94
>>> for count in xrange(500):
95
... t += random.random()*24*3600*30
96
... o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
97
... date = format_highres_date(t, o)
98
... t2, o2 = unpack_highres_date(date)
99
... if t != t2 or o != o2:
100
... print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
78
104
# Weekday parsing is locale sensitive, so drop the weekday
79
105
space_loc = date.find(' ')
113
139
if offset % 60 != 0:
114
140
raise ValueError(
115
"can't represent timezone %s offset by fractional minutes" % offset)
141
"can't represent timezone %s offset by fractional minutes" % offset)
116
142
# so that we don't need to do calculations on pre-epoch times,
117
143
# which doesn't work with win32 python gmtime, we always
118
144
# give the epoch in utc
121
147
if secs + offset < 0:
122
148
from warnings import warn
123
149
warn("gmtime of negative time (%s, %s) may not work on Windows" %
125
151
return osutils.format_date(secs, offset=offset,
126
date_fmt='%Y-%m-%d %H:%M:%S')
129
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
130
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
131
RE_PATCHDATE = re.compile(
132
"(\\d+-\\d+-\\d+\\s+\\d+:\\d+:\\d+)\\s*([+-]\\d\\d)(\\d\\d)$")
133
RE_PATCHDATE_NOOFFSET = re.compile("\\d+-\\d+-\\d+\\s+\\d+:\\d+:\\d+$")
152
date_fmt='%Y-%m-%d %H:%M:%S')
136
155
def parse_patch_date(date_str):
139
158
Inverse of format_patch_date.
141
match = RE_PATCHDATE.match(date_str)
143
if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
144
raise ValueError("time data %r is missing a timezone offset"
147
raise ValueError("time data %r does not match format " % date_str +
148
"'%Y-%m-%d %H:%M:%S %z'")
149
secs_str = match.group(1)
150
offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
151
if abs(offset_hours) >= 24 or offset_mins >= 60:
152
raise ValueError("invalid timezone %r" %
153
(match.group(2) + match.group(3)))
154
offset = offset_hours * 3600 + offset_mins * 60
160
secs_str = date_str[:-6]
161
offset_str = date_str[-5:]
162
if len(offset_str) != 5:
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
155
167
tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
156
168
# adjust seconds according to offset before converting to POSIX
157
169
# timestamp, to avoid edge problems