Edgewall Software
Modify

Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#9420 closed defect (fixed)

to_datetime fails with negative fractional timestamps

Reported by: Jun Omae Owned by: Jun Omae
Priority: normal Milestone: 0.12
Component: general Version: 0.12dev
Severity: normal Keywords:
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description

make test failed with CentOS 5.5 and Python 2.4.3.

======================================================================
ERROR: test_to_datetime_microsecond_negative_timestamps (trac.util.tests.datefmt.DateFormatTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jun66j5/src/trac.git/trac/util/tests/datefmt.py", line 76, in test_to_datetime_microsecond_negative_timestamps
    datefmt.localtz)
ValueError: microsecond must be in 0..999999

----------------------------------------------------------------------
Ran 1103 tests in 47.497s

FAILED (errors=1)
make: *** [unit-test] Error 1

The issue is known bug of Python 2.4. cf. Issue 1646728: datetime.fromtimestamp fails with negative fractional times - Python tracker

Created the patch with the helps of it.

  • trac/util/datefmt.py

    diff --git a/trac/util/datefmt.py b/trac/util/datefmt.py
    index 9def1a7..4328118 100644
    a b  
    1818#         Matthew Good <trac@matt-good.net>
    1919
    2020import locale
     21import math
    2122import re
    2223import sys
    2324import time
    24 from datetime import tzinfo, timedelta, datetime, date
     25from datetime import tzinfo, timedelta, datetime, date, timedelta
    2526
    2627from trac.core import TracError
    2728from trac.util.text import to_unicode
    def to_datetime(t, tzinfo=None):  
    5253        if not (_min_ts <= t <= _max_ts):
    5354            # Handle microsecond timestamps for 0.11 compatibility
    5455            t = t * 0.000001
     56        if t < 0:
     57            # negative fractional times bug
     58            # http://bugs.python.org/issue1646728
     59            integer = math.floor(t)
     60            frac = t - integer
     61            return datetime.fromtimestamp(integer, tzinfo or localtz) \
     62                   + timedelta(seconds=frac)
    5563        return datetime.fromtimestamp(t, tzinfo or localtz)
    5664    raise TypeError('expecting datetime, int, long, float, or None; got %s' %
    5765                    type(t))
  • trac/util/tests/datefmt.py

    diff --git a/trac/util/tests/datefmt.py b/trac/util/tests/datefmt.py
    index 63b45da..bc959f7 100644
    a b class DateFormatTestCase(unittest.TestCase):  
    7272        self.assertEqual(datefmt.to_datetime(2345678912.0), expected)
    7373
    7474    def test_to_datetime_microsecond_negative_timestamps(self):
    75         expected = datetime.datetime.fromtimestamp(-2345.678912,
    76                                                    datefmt.localtz)
     75        expected = datetime.datetime.fromtimestamp(-2345, datefmt.localtz) \
     76                   - datetime.timedelta(seconds=.678912)
     77
     78        self.assertEqual(datefmt.to_datetime(-2345678912).microsecond,
     79                         321088) # 1000000 - 678912
    7780        self.assertEqual(datefmt.to_datetime(-2345678912), expected)
    7881        self.assertEqual(datefmt.to_datetime(-2345678912L), expected)
    7982        self.assertEqual(datefmt.to_datetime(-2345678912.0), expected)

Attachments (0)

Change History (2)

comment:1 by Remy Blank, 14 years ago

Resolution: fixed
Status: newclosed

Slightly simpler patch applied in [9855]. Thanks!

comment:2 by Remy Blank, 14 years ago

Owner: set to Jun Omae

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Jun Omae to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.