New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
datetime.fromtimestamp fails with negative fractional times #44515
Comments
The datetime.fromtimestamp() function works fine with integer timestamps and positive fractional timestamps, but fails if I pass a negative fractional timestamp. For example: >>> import datetime
>>> datetime.datetime.fromtimestamp(-1.05)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: microsecond must be in 0..999999 It should return the same result as datetime.fromtimestamp(-1) - timedelta(seconds=.5). The same bug can be triggered in datetime.utcfromtimestamp(). I have been able to reproduce this bug in Python 2.4.4 and Python 2.5 on Linux. |
Looks like a bug in the conversion from floats to ints. Anyone care to track it down more precisely? |
The problem seems to be in datetime_from_timestamp() from datetimemodule.c. It should probably be checking to see whether the microseconds value it calculates is negative, and adjust "timet" and "us" accordingly if so. |
Attached is a fix. If this is to your liking I'll check it in. |
I just tried the patch, and can confirm that it fixes the problem with datetime.fromtimestamp() and datetime.utcfromtimestamp(). The logic in the patch looks correct. |
Committed revision 54167. I'm leaving this open until it's been backported to the 2.5 branch. |
Georgbot backported this to 2.5. |
Though, the new tests seem to fail on Windows (I noticed that only after backporting, since most other buildbot failures were due to the cmp/key problem in setup.py). |
That's too bad. More details? |
Not from me, no Windows around. |
Hello, I'm user of Windows (Now building Python2.5 with VC6) >>> datetime.datetime.fromtimestamp(-1.05)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: timestamp out of range for platform localtime()/gmtime() function
>>> datetime.datetime.fromtimestamp(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: timestamp out of range for platform localtime()/gmtime() function I'll attach workaround for unittest. Probably there is better way Index: Lib/test/test_datetime.py --- Lib/test/test_datetime.py (revision 54194)
+++ Lib/test/test_datetime.py (working copy)
@@ -1428,9 +1428,17 @@
def test_negative_float_fromtimestamp(self):
# The result is tz-dependent; at least test that this doesn't
# fail (like it did before bug 1646728 was fixed).
+ try:
+ self.theclass.fromtimestamp(-1)
+ except ValueError: # cannot handle negative value
+ return
self.theclass.fromtimestamp(-1.05)
def test_negative_float_utcfromtimestamp(self):
+ try:
+ self.theclass.utcfromtimestamp(-1)
+ except ValueError: # cannot handle negative value
+ return
d = self.theclass.utcfromtimestamp(-1.05)
self.assertEquals(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000)) |
Thanks! I'm skipping these tests on Windows now. Georgbot, would you be so kind... :-) |
Certainly. Backported in rev. 54211. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: