Skip to content

time.strftime(arg) fails when arg is not valid format string #4157

Closed

Description

@jopemachine

Expected Result

In cpython, when arg is not a valid format string, strftime returns arg itself.

>>> import time
>>> time.strftime("%Y")
'2022'
>>> time.strftime("%4Y")
'4Y'

Actual Result

In RustPython, strftime raise panic when arg is not a valid format string

>>>>> import time
>>>>> time.strftime("%Y")
'2022'
>>>>> time.strftime("%4Y")
thread 'main' panicked at 'a Display implementation returned an error unexpectedly: Error', /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/alloc/src/string.rs:2490:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Activity

  1. jopemachine commented on Sep 12, 2022

    @jopemachine
    ContributorAuthor

    I think this could be related to the below issue.

    chronotope/chrono#815

  2. jopemachine commented on Oct 4, 2022

    @jopemachine
    ContributorAuthor

    Now, I'm waiting for the below PR to be merged and v0.5 to be released.

    chronotope/chrono#827

  3. itsankitkp commented on Jan 26, 2023

    @itsankitkp
    Contributor

    Should we add a validation check in fn strptime for supported formats?
    This would require a list valid_time_formats = [..] which we can get from https://docs.python.org/3/library/time.html#time.strftime
    if validation succeeds then we can continue normal flow otherwise we can return an arg

  4. DimitrisJim commented on Jan 27, 2023

    @DimitrisJim
    Member

    Should we add a validation check in fn strptime for supported formats?

    There's a simpler solution for now, we can use write! to create the formatted date instead of to_string, that returns a result which we can handle.

    Do you want to give it a shot? If so, lmk and I'll assign you.

  5. itsankitkp commented on Jan 27, 2023

    @itsankitkp
    Contributor

    Did some more digging. CPython uses wcsftime for formatting time (if system supports it)

    After building latest python 3.12, I got following

    time.strftime("%4Y")
    '2023'

    Same for Python 3.10 as well

    Playing around with wcsftime for same input also yields same result
    https://en.cppreference.com/w/c/chrono/wcsftime

    Now, as for chrono in rust:
    I attempted write!
    write!(&mut formatted_time, "{}", instant.format(format.as_str())).unwrap();
    It panicked at %4Y (worked at other cases so at least I am not passing it wrong)

    I played around chrono too, it doesn't support %4Y at all.

    What can be done now is to add exception handling so that rustpython don't panic if there is an exception in chrono.
    In case of exception, it can return output as arg. Whenever chrono gets updated, we will get correct results.

    Do you want to give it a shot? If so, lmk and I'll assign you.

    Sure, I have raised PR #4474
    which includes above solution with a test case

    Pending: one test case is failing viz embedded null character
    I think null characters should have been handled in earlier layers (not related to change in this PR), I will check this.

  6. fanninpm commented on Jan 27, 2023

    @fanninpm
    Contributor

    After reading a bit more, it seems that the "%4Y" format is platform-specific.

  7. itsankitkp commented on Jan 28, 2023

    @itsankitkp
    Contributor

    After reading a bit more, it seems that the "%4Y" format is platform-specific.

    +1 and on compiler version

    /* Define to 1 if you have the `wcsftime' function. */
    #if defined(_MSC_VER) && _MSC_VER >= 1310
    #define HAVE_WCSFTIME 1
    #endif
    
  8. DimitrisJim commented on Jan 30, 2023

    @DimitrisJim
    Member

    yeah, crono should be the one handling the platform specific stuff. Our main goal should be to avoid the panic for now and try and return the format as CPython does (i.e they probably remove the leading % on return if I remember correctly last I checked it)

  9. itsankitkp commented on Jan 31, 2023

    @itsankitkp
    Contributor

    yeah, crono should be the one handling the platform specific stuff. Our main goal should be to avoid the panic for now and try and return the format as CPython does (i.e they probably remove the leading % on return if I remember correctly last I checked it)

    Yes, current version implements this behavior.

  10. fanninpm commented on Feb 23, 2023

    @fanninpm
    Contributor

    Did #4530 fix this?

  11. DimitrisJim commented on Feb 24, 2023

    @DimitrisJim
    Member

    Did #4530 fix this?

    it did go from panicking to simply failing so partially yes? 😄

  12. changed the title [-]`time.strftime(arg)` raise panic when arg is not valid format string[/-] [+]`time.strftime(arg)` fails when arg is not valid format string[/+] on Feb 25, 2023
  13. kong-peter commented on Apr 13, 2025

    @kong-peter

    Hi, new to this project and just reviewing the "good first issue" label and i cannot reproduce the issue!
    On my Mac, RustPython 3.13.0 now does:

    time.strftime("%4Y")
    '%4Y'
    Perhaps #4530 resolved the issue?

  14. youknowone commented on Dec 25, 2025

    @youknowone
    Member

    This is fixed, thank you all!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersz-ca-2022Tag to track contrubution-academy 2022

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions