Modify ↓
#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 18 18 # Matthew Good <trac@matt-good.net> 19 19 20 20 import locale 21 import math 21 22 import re 22 23 import sys 23 24 import time 24 from datetime import tzinfo, timedelta, datetime, date 25 from datetime import tzinfo, timedelta, datetime, date, timedelta 25 26 26 27 from trac.core import TracError 27 28 from trac.util.text import to_unicode … … def to_datetime(t, tzinfo=None): 52 53 if not (_min_ts <= t <= _max_ts): 53 54 # Handle microsecond timestamps for 0.11 compatibility 54 55 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) 55 63 return datetime.fromtimestamp(t, tzinfo or localtz) 56 64 raise TypeError('expecting datetime, int, long, float, or None; got %s' % 57 65 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): 72 72 self.assertEqual(datefmt.to_datetime(2345678912.0), expected) 73 73 74 74 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 77 80 self.assertEqual(datefmt.to_datetime(-2345678912), expected) 78 81 self.assertEqual(datefmt.to_datetime(-2345678912L), expected) 79 82 self.assertEqual(datefmt.to_datetime(-2345678912.0), expected)
Attachments (0)
Change History (2)
comment:1 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:2 by , 15 years ago
Owner: | set to |
---|
Note:
See TracTickets
for help on using tickets.
Slightly simpler patch applied in [9855]. Thanks!