~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/timestamp.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2008, 2009, 2011 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import calendar
18
18
import time
47
47
    >>> format_highres_date(1152428738.867522, 19800)
48
48
    'Sun 2006-07-09 12:35:38.867522001 +0530'
49
49
    """
50
 
    assert isinstance(t, float)
 
50
    if not isinstance(t, float):
 
51
        raise ValueError(t)
51
52
 
52
53
    # This has to be formatted for "original" date, so that the
53
54
    # revision XML entry will be reproduced faithfully.
55
56
        offset = 0
56
57
    tt = time.gmtime(t + offset)
57
58
 
58
 
    return (time.strftime("%a %Y-%m-%d %H:%M:%S", tt)
 
59
    return (osutils.weekdays[tt[6]] +
 
60
            time.strftime(" %Y-%m-%d %H:%M:%S", tt)
59
61
            # Get the high-res seconds, but ignore the 0
60
62
            + ('%.9f' % (t - int(t)))[1:]
61
63
            + ' %+03d%02d' % (offset / 3600, (offset / 60) % 60))
70
72
    :param date: A date formated by format_highres_date
71
73
    :type date: string
72
74
 
73
 
    >>> import time, random
74
 
    >>> unpack_highres_date('Thu 2005-06-30 12:38:52.350850105 -0500')
75
 
    (1120153132.3508501, -18000)
76
 
    >>> unpack_highres_date('Thu 2005-06-30 17:38:52.350850105 +0000')
77
 
    (1120153132.3508501, 0)
78
 
    >>> unpack_highres_date('Thu 2005-06-30 19:38:52.350850105 +0200')
79
 
    (1120153132.3508501, 7200)
80
 
    >>> unpack_highres_date('Sun 2006-07-09 12:35:38.867522001 +0530')
81
 
    (1152428738.867522, 19800)
82
 
    >>> from bzrlib.osutils import local_time_offset
83
 
    >>> t = time.time()
84
 
    >>> o = local_time_offset()
85
 
    >>> t2, o2 = unpack_highres_date(format_highres_date(t, o))
86
 
    >>> t == t2
87
 
    True
88
 
    >>> o == o2
89
 
    True
90
 
    >>> t -= 24*3600*365*2 # Start 2 years ago
91
 
    >>> o = -12*3600
92
 
    >>> for count in xrange(500):
93
 
    ...   t += random.random()*24*3600*30
94
 
    ...   o = ((o/3600 + 13) % 25 - 12)*3600 # Add 1 wrap around from [-12, 12]
95
 
    ...   date = format_highres_date(t, o)
96
 
    ...   t2, o2 = unpack_highres_date(date)
97
 
    ...   if t != t2 or o != o2:
98
 
    ...      print 'Failed on date %r, %s,%s diff:%s' % (date, t, o, t2-t)
99
 
    ...      break
100
 
 
101
75
    """
 
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:
 
79
        raise ValueError(
 
80
            'Date string does not contain a day of week: %r' % date)
102
81
    # Up until the first period is a datestamp that is generated
103
82
    # as normal from time.strftime, so use time.strptime to
104
83
    # parse it
106
85
    if dot_loc == -1:
107
86
        raise ValueError(
108
87
            'Date string does not contain high-precision seconds: %r' % date)
109
 
    base_time = time.strptime(date[:dot_loc], "%a %Y-%m-%d %H:%M:%S")
 
88
    base_time = time.strptime(date[space_loc:dot_loc], " %Y-%m-%d %H:%M:%S")
110
89
    fract_seconds, offset = date[dot_loc:].split()
111
90
    fract_seconds = float(fract_seconds)
112
91
 
129
108
 
130
109
    Inverse of parse_patch_date.
131
110
    """
132
 
    assert offset % 60 == 0, \
133
 
        "can't represent timezone %s offset by fractional minutes" % offset
134
 
    # so that we don't need to do calculations on pre-epoch times, 
 
111
    if offset % 60 != 0:
 
112
        raise ValueError(
 
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,
135
115
    # which doesn't work with win32 python gmtime, we always
136
116
    # give the epoch in utc
137
117
    if secs == 0:
151
131
    """
152
132
    secs_str = date_str[:-6]
153
133
    offset_str = date_str[-5:]
154
 
    assert len(offset_str) == 5, \
155
 
            "invalid timezone %r" % offset_str
 
134
    if len(offset_str) != 5:
 
135
        raise ValueError(
 
136
            "invalid timezone %r" % offset_str)
156
137
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
157
138
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
158
139
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')