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
57
57
tt = time.gmtime(t + offset)
59
return (osutils.weekdays[tt[6]] +
60
time.strftime(" %Y-%m-%d %H:%M:%S", tt)
59
return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
61
60
# Get the high-res seconds, but ignore the 0
62
61
+ ('%.9f' % (t - int(t)))[1:]
63
62
+ ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
72
71
:param date: A date formated by format_highres_date
74
>>> import time, random
75
>>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
76
(1120153132.3508501, -18000)
77
>>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
78
(1120153132.3508501, 0)
79
>>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
80
(1120153132.3508501, 7200)
81
>>> unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530')
82
(1152428738.867522, 19800)
83
>>> from bzrlib.osutils import local_time_offset
85
>>> o = local_time_offset()
86
>>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
91
>>> t -= 24*3600*365*2 # Start 2 years ago
93
>>> for count in xrange(500):
94
... t += random.random()*24*3600*30
95
... o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
96
... date = format_highres_date(t, o)
97
... t2, o2 = unpack_highres_date(date)
98
... if t != t2 or o != o2:
99
... 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
103
# Up until the first period is a datestamp that is generated
82
104
# as normal from time.strftime, so use time.strptime to
87
109
'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")
110
base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
89
111
fract_seconds, offset = date[dot_loc:].split()
90
112
fract_seconds = float(fract_seconds)
111
133
if offset % 60 != 0:
112
134
raise ValueError(
113
135
"can't represent timezone %s offset by fractional minutes" % offset)
114
# so that we don't need to do calculations on pre-epoch times,
136
# so that we don't need to do calculations on pre-epoch times,
115
137
# which doesn't work with win32 python gmtime, we always
116
138
# give the epoch in utc