Skip to content

DateTime: Conversion to timedelta fails with bad error message if values are too large #5834

Closed

Description

@pekkaklarck

For example, Convert Time 1e14 timedelta fails with this error:

OverflowError: days=1157407407; must have magnitude <= 999999999

Using even larger values like 1e15 changes the error to this:

OverflowError: Python int too large to convert to C int

The root cause is that timedelta requires values to be between 999999999 and 999999999 days. The first error is somewhat helpful, but the latter is rather useless. We can easily make the situation better by catching the OverflowError and raising a ValueError with a better message. I have a fix ready locally that changes the first error to this:

ValueError: 1157407407 days 9 hours 46 minutes 40 seconds cannot be represented as a timedelta object. Values must be between -999999999 and 999999999 days.

This is a spinoff from issue #5819 and PR #5820. As the discussion explains, argument conversion uses DateTime internally for datetime, date and timedelta conversion and the resulting OverflowError currently causes problems also there. Those problems will be fixed as part of the aforementioned issue and PR, but it still makes sense to enhance DateTime to produce better error messages.

Activity

  1. added this to the v7.5.1 milestone on Oct 28, 2026
  2. self-assigned this
    on Oct 28, 2026
  3. added theissue type on Oct 28, 2026
  4. added a commit that references this issue on Oct 28, 2026
    5df1f04
  5. added a commit that references this issue on Oct 28, 2026
  6. d-biehl commented on Oct 29, 2026

    @d-biehl
    Contributor

    As discussed in #5820, here are some ideas for this issue: related cases that still fail with bad errors. I collected and tested them with the help of Claude Code, using the current master and Python 3.13 on Linux. I haven't looked into possible fixes yet, so please take this as a starting point rather than a complete analysis.

    1. Infinite and NaN values: Convert Time inf result_format=timedelta fails with OverflowError: cannot convert float infinity to integer. timedelta(seconds=inf) raises OverflowError, and secs_to_timestr(inf) in the new except block raises it again. The verbose, compact and timer formats fail the same way, and so does exclude_millis=yes even with the default number format, because the value is rounded. -inf and values that are too large for a float, like 1e400 or 1e308 days, fail the same way, because they are infinite too. nan fails with ValueError: cannot convert float NaN to integer.
    2. Too large integers: Convert Time ${{10**400}} fails with OverflowError: int too large to convert to float in timestr_to_secs.
    3. Time.timedelta: Add Time To Date 2024-01-01 1e20 and Get Current Date increment=1e20 fail with OverflowError: Python int too large to convert to C int. Date calculations use the Time.timedelta property, which creates the timedelta without the new handling.
    4. Dates out of range: Add Time To Date 2024-01-01 1e7 days, Subtract Time From Date 0001-01-01 1 day and Get Current Date increment=1e7 days fail with OverflowError: date value out of range. Here, the timedelta itself is fine, but the result is out of range. Rounding can also go out of range: Convert Date 9999-12-31 23:59:59.9999 exclude_millis=yes fails the same way.
    5. Epoch seconds as input: Convert Date ${1e20} fails with OverflowError: timestamp out of range for platform time_t, and Convert Date ${1e18} with OSError: [Errno 75] Value too large for defined data type. Both come from datetime.fromtimestamp in Date._epoch_seconds_to_datetime, so the limits and errors depend on the platform. Convert Date ${3e11} already fails with ValueError: year 11476 is out of range. BuiltIn's Get Time year ${1e20} has the same problem in robot.utils.robottime.get_time.
    6. Epoch seconds as output: Convert Date 0001-01-01 result_format=epoch fails with ValueError: year 0 is out of range. It is a ValueError, but the year in the message isn't the one in the input. Convert Date 9999-12-31 23:59:59 result_format=epoch fails with ValueError: year 10000 is out of range depending on the time zone. It fails with CET, but works with UTC.

    Some of these may not be worth fixing. For example, the epoch output cases in 6 already fail with ValueError, and only the message is misleading. With #5820, argument conversion reports all of these as normal conversion errors when it uses DateTime for timedelta, datetime and date. The keywords themselves still fail with the errors above.

  7. d-biehl commented on Oct 29, 2026

    @d-biehl
    Contributor

    This issue is already closed with the v7.5.1 milestone. Should these cases still go into 7.5.1, or later? And should they be fixed as part of this issue or get a separate one?

  8. pekkaklarck commented on Oct 30, 2026

    @pekkaklarck
    MemberAuthor

    I don't think any of the new items are severe enough that they must be in RF 7.5.1 that's already overdo. That said, cases where the error is really misleading could still be fixed. I can take a look at these later, but you can @d-biehl do that as well.

  9. pekkaklarck commented on Oct 30, 2026

    @pekkaklarck
    MemberAuthor

    For most parts the fixes should got to robot.utils.robottime and many of them are related to inf and nan. Fixes that go to RF 7.5.1 should only change cases where execution would anyway fail like elapsed_time_to_string(float('inf')). Changing functions that currently can return inf or nan to raise could cause backwards incompatibility issues. I think that would be fine in RF 7.6, but not in a bug fix release.

  10. pekkaklarck commented on Oct 30, 2026

    @pekkaklarck
    MemberAuthor

    I looked at some of these and it turned out that it would be much easier if we could just fail inf and nan in input. That would, however, be potentially backwards incompatible change so it's not a good idea now. I already changed secs_to_timestr and elapsed_time_to_string to raise ValueError with inf/NaN input and consider that safe, because there functions already failed but for OverflowError. I'll commit these changes along with related unit tests and updated DateTime tests, but otherwise I won't be making changes now.

    Do you @d-biehl see places where we could make safe changes now and do you have time and interest to make such changes?

    I'll anyway submit a separate issue about enhancing handling of inf/NaN and large numbers in DateTime in RF 7.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions