Repository navigation
Done
Labels
extension-modulesC modules in the Modules dirpendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Description
| BPO | 23607 |
|---|---|
| Nosy | @malemburg, @abalkin, @vstinner, @skrah, @serhiy-storchaka, @pganssle, @shlomoa, @remilapeyre |
| Files |
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:
assignee = 'https://github.com/serhiy-storchaka'
closed_at = None
created_at = <Date 2015-03-08.10:35:01.714>
labels = ['3.8', 'type-bug', 'library']
title = 'Inconsistency in datetime.utcfromtimestamp(Decimal)'
updated_at = <Date 2019-02-27.17:14:23.100>
user = 'https://github.com/serhiy-storchaka'bugs.python.org fields:
activity = <Date 2019-02-27.17:14:23.100>
actor = 'remi.lapeyre'
assignee = 'serhiy.storchaka'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2015-03-08.10:35:01.714>
creator = 'serhiy.storchaka'
dependencies = []
files = ['38388', '38736']
hgrepos = []
issue_num = 23607
keywords = ['patch']
message_count = 11.0
messages = ['237527', '237541', '237801', '239593', '239595', '239597', '239608', '239613', '336769', '336771', '336772']
nosy_count = 10.0
nosy_names = ['lemburg', 'belopolsky', 'vstinner', 'SilentGhost', 'skrah', 'serhiy.storchaka', 'p-ganssle', 'anglister', 'remi.lapeyre', 'Steven Davidson']
pr_nums = []
priority = 'low'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue23607'
versions = ['Python 3.8']Linked PRs
- gh-67795: Accept any real numbers as timestamp and timeout #139224
- gh-67795: Improve precision of non-float timestamp and timeout arguments #139273
- gh-67795: Skip test values below the time_t minimum on platforms with 32-bit time_t #162878
- gh-67795: Add pytime_double_to_time_t() helper function #162904
- gh-67795: Remove _PyTime_FromSecondsDouble() function #162906
- gh-67795: Rewrite pytime_from_pyfloat() #162940
- gh-67795: Move PyTime C API tests to test_capi.test_time #163043
- [3.15] gh-67795: Move PyTime C API tests to test_capi.test_time (#163043) #163089
- [3.14] gh-67795: Move PyTime C API tests to test_capi.test_time #163102
Metadata
Metadata
Assignees
Labels
extension-modulesC modules in the Modules dirpendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
serhiy-storchaka commentedon Mar 8, 2015
First, datetime.utcfromtimestamp() drops fractional part of Decimal argument:
Second, it works only with C implementation. With Python implementation Decimals are not supported at all.
In 2.7 Decimals are accepted and are not truncated.
So this can be considered as a regression.
SilentGhost commentedon Mar 8, 2015
Here is the python-only fix that eliminates TypeError and produces correct number of milliseconds. The problem with C implementation lies in _PyTime_ObjectToDenominator function where there is an explicit check for a float. This check could be relaxed to include decimals as the rest of the code seem to work fine with decimal. I'm not entirely sure how that could be done though.
serhiy-storchaka commentedon Mar 10, 2015
The fix for Python implementation LGTM, but a fix for C implementation is needed too.
serhiy-storchaka commentedon Mar 30, 2015
Here is a patch that adds Decimal support for datetime.fromtimestamp() and datetime.utcfromtimestamp(). As side effect Decimal timestamps now are supported in few other places, e.g. in os.utime().
vstinner commentedon Mar 30, 2015
I reviewed datetime_fromtimestamp_decimal.patch.
It would be more consistent to support decimal.Decimal nowhere or "everywhere". IMO the new _PyTime_FromSecondsObject() (very close to _PyTime_ObjectToDenominator, but using time_t) should also be patched.
Please add some tests for decimal.Decimal in test_time directly. Usually, I try to test rounding and overflow. Testing for overflow is not always possible because it may depend on the platform.
--
See also the PEP-410: "PEP-410 - Use decimal.Decimal type for timestamps". The PEP was rejected. The compromise was a new timestamp format for os.utime(): a number of nanoseconds since the UNIX epoch (1970-01-01). I partially implemented a private API for this PEP in the issue bpo-22117: the new _PyTime_t is a number of timestamp using an arbitrary resolution (currently, it's a number of nanoseconds using a 64-bit signed integer type).
skrah commentedon Mar 30, 2015
I just looked at this very briefly: Is the patch context insensitive?
IOW, do things still work if you change the thread-local context:
serhiy-storchaka commentedon Mar 30, 2015
The problem is that the Decimal *was* supported in 2.7. So this issue can be considered not as adding new feature, but as fixing a regression. But changes are too large for just bugfix.
Will add Decimal support in all functions in Python/pytime.c that support floats.
Will do.
No, the patch is context sensitive. I think the end user is responsible to set an appropriate context if it want to create a datetime from Decimal timestamp. At least until we add functions that return Decimal.
skrah commentedon Mar 30, 2015
I think we should try to avoid depending on global state in
the stdlib, at least in new code. Also, if something is not
really a decimal computation, Decimal itself tries to ignore
the context (like Decimal.__repr__).
At least I would expect this datetime function to be context
independent.
pganssle commentedon Feb 27, 2019
I'm not sure if either of these patches got merged, but at some point this was fixed:
Python 3.7.2 (default, Feb 9 2019, 13:18:43) [GCC 8.2.1 20181127] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from datetime import datetime >>> from decimal import Decimal >>> datetime.utcfromtimestamp(Decimal(123456.0)) datetime.datetime(1970, 1, 2, 10, 17, 36)I recommend that someone add some regression tests to this, then we can close the issue.
I'm also going to mark this as "easy" since adding tests for this particular example should be pretty simple.
pganssle commentedon Feb 27, 2019
Oh actually that's my mistake. I can't reproduce the failure in the constructor in the Python version of the module, and also it seems to be fixed in the pure Python version as of at least 3.6:
But the truncation behavior is still present in the C version as of Python 3.8.0a1+:
I still think we need a test for the constructor behavior, but I'm going to remove "easy", since we still need to fix truncation in the C version.
18 remaining items
gh-67795: Skip test values below the time_t minimum on platforms with…
pythongh-67795: Refactor pytime.c code handling double
pythongh-67795: Remove _PyTime_FromSecondsDouble() function
gh-67795: Remove _PyTime_FromSecondsDouble() function (#162906)
pythongh-67795: Add pytime_double_to_time_t() helper function
gh-67795: Add pytime_double_to_time_t() helper function (#162904)
gh-67795: Rewrite pytime_from_pyfloat() (#162940)
vstinner commentedon Dec 3, 2026
@serhiy-storchaka: I suggest closing this issue. I see your change #139273 as an enhancement (better precision), rather as a bugfix, and so I would prefer to not backport it (unless you want to backport it?).
vstinner commentedon Dec 3, 2026
Oh, I didn't notice that you already closed the issue, and it was reopened when tests failed. Tests have been fixed, so I close the issue again.
gh-67795: Move PyTime C API tests to test_capi.test_time (#163043)
[3.15] gh-67795: Move PyTime C API tests to test_capi.test_time (#163089
[3.14] gh-67795: Move PyTime C API tests to test_capi.test_time (#163102