~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/timestamp.py

  • Committer: Martin Pool
  • Date: 2008-07-14 07:39:30 UTC
  • mto: This revision was merged to the branch mainline in revision 3537.
  • Revision ID: mbp@sourcefrog.net-20080714073930-z8nl2c44jal0eozs
Update test for knit.check() to expect it to recurse into fallback vfs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008, 2009, 2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
2
2
#
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
12
12
#
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
16
 
 
17
 
from __future__ import absolute_import
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
16
 
19
17
import calendar
20
18
import time
21
 
import re
22
19
 
23
20
from bzrlib import osutils
24
21
 
59
56
        offset = 0
60
57
    tt = time.gmtime(t + offset)
61
58
 
62
 
    return (osutils.weekdays[tt[6]] +
63
 
            time.strftime(" %Y-%m-%d %H:%M:%S", tt)
 
59
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
64
60
            # Get the high-res seconds, but ignore the 0
65
61
            + ('%.9f' % (t - int(t)))[1:]
66
62
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
75
71
    :param date: A date formated by format_highres_date
76
72
    :type date: string
77
73
 
 
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
 
84
    >>> t = time.time()
 
85
    >>> o = local_time_offset()
 
86
    >>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
 
87
    >>> t == t2
 
88
    True
 
89
    >>> o == o2
 
90
    True
 
91
    >>> t -= 24*3600*365*2 # Start 2 years ago
 
92
    >>> o = -12*3600
 
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)
 
100
    ...      break
 
101
 
78
102
    """
79
 
    # Weekday parsing is locale sensitive, so drop the weekday
80
 
    space_loc = date.find(' ')
81
 
    if space_loc == -1 or date[:space_loc] not in osutils.weekdays:
82
 
        raise ValueError(
83
 
            'Date string does not contain a day of week: %r' % date)
84
103
    # Up until the first period is a datestamp that is generated
85
104
    # as normal from time.strftime, so use time.strptime to
86
105
    # parse it
88
107
    if dot_loc == -1:
89
108
        raise ValueError(
90
109
            'Date string does not contain high-precision seconds: %r' % date)
91
 
    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")
92
111
    fract_seconds, offset = date[dot_loc:].split()
93
112
    fract_seconds = float(fract_seconds)
94
113
 
114
133
    if offset % 60 != 0:
115
134
        raise ValueError(
116
135
        "can't represent timezone %s offset by fractional minutes" % offset)
117
 
    # 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, 
118
137
    # which doesn't work with win32 python gmtime, we always
119
138
    # give the epoch in utc
120
139
    if secs == 0:
127
146
            date_fmt='%Y-%m-%d %H:%M:%S')
128
147
 
129
148
 
130
 
# Format for patch dates: %Y-%m-%d %H:%M:%S [+-]%H%M
131
 
# Groups: 1 = %Y-%m-%d %H:%M:%S; 2 = [+-]%H; 3 = %M
132
 
RE_PATCHDATE = re.compile("(\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+$")
134
 
 
135
149
def parse_patch_date(date_str):
136
150
    """Parse a patch-style date into a POSIX timestamp and offset.
137
151
 
138
152
    Inverse of format_patch_date.
139
153
    """
140
 
    match = RE_PATCHDATE.match(date_str)
141
 
    if match is None:
142
 
        if RE_PATCHDATE_NOOFFSET.match(date_str) is not None:
143
 
            raise ValueError("time data %r is missing a timezone offset"
144
 
                % date_str)
145
 
        else:
146
 
            raise ValueError("time data %r does not match format " % date_str
147
 
                + "'%Y-%m-%d %H:%M:%S %z'")
148
 
    secs_str = match.group(1)
149
 
    offset_hours, offset_mins = int(match.group(2)), int(match.group(3))
150
 
    if abs(offset_hours) >= 24 or offset_mins >= 60:
151
 
        raise ValueError("invalid timezone %r" %
152
 
            (match.group(2) + match.group(3)))
153
 
    offset = offset_hours * 3600 + offset_mins * 60
 
154
    secs_str = date_str[:-6]
 
155
    offset_str = date_str[-5:]
 
156
    if len(offset_str) != 5:
 
157
        raise ValueError(
 
158
            "invalid timezone %r" % offset_str)
 
159
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
 
160
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
154
161
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')
155
162
    # adjust seconds according to offset before converting to POSIX
156
163
    # timestamp, to avoid edge problems