Skip to content

Inconsistency in datetime.utcfromtimestamp(Decimal) #67795

Backlog

Description

@serhiy-storchaka
BPO 23607
Nosy @malemburg, @abalkin, @vstinner, @skrah, @serhiy-storchaka, @pganssle, @shlomoa, @remilapeyre
Files
  • issue23607_python.diff
  • datetime_fromtimestamp_decimal.patch
  • 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

    Activity

    1. serhiy-storchaka commented on Feb 27, 2019

      @serhiy-storchaka
      MemberAuthor

      No, the bug is not fixed, and this is not easy issue. You should use non-integer Decimals to reproduce it. In 3.8 this emits a deprecation warning:

      >>> import datetime
      >>> from decimal import Decimal as D
      >>> datetime.datetime.utcfromtimestamp(D(1425808327.307651))
      <stdin>:1: DeprecationWarning: an integer is required (got type decimal.Decimal).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
      datetime.datetime(2015, 3, 8, 9, 52, 7)
    2. transferred this issue fromon Apr 10, 2022
    3. added
      extension-modules
      and removed
      3.8 (EOL)
      on Jul 8, 2025
    4. StanFromIreland commented on Jul 9, 2025

      @StanFromIreland
      Member

      Second, it works only with C implementation. With Python implementation Decimals are not supported at all.

      Ironically it is the opposite now.

      >>> #import datetime
      ... import _pydatetime as datetime
      ... from decimal import Decimal as D
      ... datetime.datetime.utcfromtimestamp(D(1425808327.307651))
      ... 
      <python-input-5>:4: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(t, datetime.UTC).
      datetime.datetime(2015, 3, 8, 9, 52, 7, 307651)
      >>> import datetime
      ... #import _pydatetime as datetime
      ... from decimal import Decimal as D
      ... datetime.datetime.utcfromtimestamp(D(1425808327.307651))
      ... 
      <python-input-6>:4: DeprecationWarning: datetime.datetime.utcfromtimestamp() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.fromtimestamp(timestamp, datetime.UTC).
      Traceback (most recent call last):
        File "<python-input-6>", line 4, in <module>
          datetime.datetime.utcfromtimestamp(D(1425808327.307651))
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
      TypeError: argument must be int or float, not decimal.Decimal

      Furthermore, utcfromtimestamp is deprecated, and it seems no one has actually used this, since I can find no complaints, I propose we simply close this effort. Only accepting int/float is fine, converting a Decimal is not difficult.

    5. added a commit that references this issue on Sep 22, 2025
    6. serhiy-storchaka commented on Sep 22, 2025

      @serhiy-storchaka
      MemberAuthor

      I only used utcfromtimestamp() in examples because it produces the same result independently from the time zone. All of the above is applied to fromtimestamp() too.

      The original issue was because the C code first checked if the argument is float, and used conversion to int (which called __int__) for other types. Now __int__ no longer called in implicit conversion to int, so we get a TypeError instead of truncation.

      On other hand, the Python code now uses different method to split a number on the integer and fractional parts -- math.modf(). It accepts any real numbers and converts them to float.

      This is a one of rare cases where float is accepted, but other real numbers are not. Usually, real numbers are implicitly converted to float if float is accepted. It is not so difficult to make any real numbers be accepted as timestamp and timeout arguments which will be implicitly converted to floats. Although using more precise numbers will not improve precision. The hard part is updating documentation and adding many new tests for functions that directly use such conversion. #139224 implements this part.

      Preserving precision of more precise numbers is more difficult task. My initial patch tried to do this by using arithmetic operations on precise numbers, for example, the fractional part can be calculated as t - int(t) or t % 1. It works iff all operations on more precise numbers has comparable precision. Unfortunately, for Decimal the result of arithmetic operation depends on the local context, not on the precision of operands. It is also no longer work because the half-to-even rounding is now used for timestamp, it is more difficult to implement.

      Other way -- using as_integer_ratio() (or numerator and denumerator) to represent the number as rational, and then perform exact operations on rationals.

    7. added 2 commits that reference this issue on Sep 23, 2025
    8. serhiy-storchaka commented on Sep 23, 2025

      @serhiy-storchaka
      MemberAuthor

      #139273 implements the hard part -- now you can specify exact time value using Decimal or Fraction.

    9. added a commit that references this issue on Sep 23, 2025
    10. added a commit that references this issue on Nov 30, 2026
    11. encukou commented on Nov 30, 2026

      @encukou
      Member

      Since the PR was merged, buildbots fail on ARM Raspbian and x86 Debian.

    12. added a commit that references this issue on Dec 1, 2026
    13. added 5 commits that reference this issue on Dec 1, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    extension-modulesstdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions