~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/timestamp.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
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))
99
101
    ...      break
100
102
 
101
103
    """
 
104
    # Weekday parsing is locale sensitive, so drop the weekday
 
105
    space_loc = date.find(' ')
 
106
    if space_loc == -1 or date[:space_loc] not in osutils.weekdays:
 
107
        raise ValueError(
 
108
            'Date string does not contain a day of week: %r' % date)
102
109
    # Up until the first period is a datestamp that is generated
103
110
    # as normal from time.strftime, so use time.strptime to
104
111
    # parse it
106
113
    if dot_loc == -1:
107
114
        raise ValueError(
108
115
            '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")
 
116
    base_time = time.strptime(date[space_loc:dot_loc], " %Y-%m-%d %H:%M:%S")
110
117
    fract_seconds, offset = date[dot_loc:].split()
111
118
    fract_seconds = float(fract_seconds)
112
119
 
129
136
 
130
137
    Inverse of parse_patch_date.
131
138
    """
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, 
 
139
    if offset % 60 != 0:
 
140
        raise ValueError(
 
141
        "can't represent timezone %s offset by fractional minutes" % offset)
 
142
    # so that we don't need to do calculations on pre-epoch times,
135
143
    # which doesn't work with win32 python gmtime, we always
136
144
    # give the epoch in utc
137
145
    if secs == 0:
151
159
    """
152
160
    secs_str = date_str[:-6]
153
161
    offset_str = date_str[-5:]
154
 
    assert len(offset_str) == 5, \
155
 
            "invalid timezone %r" % offset_str
 
162
    if len(offset_str) != 5:
 
163
        raise ValueError(
 
164
            "invalid timezone %r" % offset_str)
156
165
    offset_hours, offset_mins = offset_str[:3], offset_str[3:]
157
166
    offset = int(offset_hours) * 3600 + int(offset_mins) * 60
158
167
    tm_time = time.strptime(secs_str, '%Y-%m-%d %H:%M:%S')