GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `time.strftime(arg)` fails when arg is not valid format string · Issue #4157 · RustPython/RustPython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
time.strftime(arg) fails when arg is not valid format string#4157
Copy link
Labels
good first issueGood for newcomersGood for newcomersz-ca-2022Tag to track contrubution-academy 2022Tag to track contrubution-academy 2022

Description

@jopemachine
jopemachine
opened on Sep 12, 2022
Issue body actions

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
    jopemachine
    on Sep 12, 2022
    ContributorAuthor
    More actions

    I think this could be related to the below issue.

    chronotope/chrono#815

  2. youknowone
    added
    z-ca-2022Tag to track contrubution-academy 2022Tag to track contrubution-academy 2022
    on Sep 19, 2022
  3. jopemachine commented on Oct 4, 2022

    @jopemachine
    jopemachine
    on Oct 4, 2022
    ContributorAuthor
    More actions

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

    chronotope/chrono#827

  4. itsankitkp commented on Jan 26, 2023

    @itsankitkp
    itsankitkp
    on Jan 26, 2023
    Contributor
    More actions

    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

  5. DimitrisJim commented on Jan 27, 2023

    @DimitrisJim
    DimitrisJim
    on Jan 27, 2023
    Member
    More actions

    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.

  6. DimitrisJim
    added
    good first issueGood for newcomersGood for newcomers
    on Jan 27, 2023
  7. itsankitkp commented on Jan 27, 2023

    @itsankitkp
    itsankitkp
    on Jan 27, 2023
    Contributor
    More actions

    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.

  8. fanninpm commented on Jan 27, 2023

    @fanninpm
    fanninpm
    on Jan 27, 2023
    Contributor
    More actions

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

  9. itsankitkp commented on Jan 28, 2023

    @itsankitkp
    itsankitkp
    on Jan 28, 2023
    Contributor
    More actions

    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
    
  10. DimitrisJim commented on Jan 30, 2023

    @DimitrisJim
    DimitrisJim
    on Jan 30, 2023
    Member
    More actions

    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)

  11. DimitrisJim
    linked a pull request that will close this issueFix #4157: handle error in case of unsupported format #4474on Jan 30, 2023
  12. itsankitkp commented on Jan 31, 2023

    @itsankitkp
    itsankitkp
    on Jan 31, 2023
    Contributor
    More actions

    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.

  13. fanninpm commented on Feb 23, 2023

    @fanninpm
    fanninpm
    on Feb 23, 2023
    Contributor
    More actions

    Did #4530 fix this?

  14. DimitrisJim commented on Feb 24, 2023

    @DimitrisJim
    DimitrisJim
    on Feb 24, 2023
    Member
    More actions

    Did #4530 fix this?

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

  15. youknowone
    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
  16. kong-peter commented on Apr 13, 2025

    @kong-peter
    kong-peter
    on Apr 13, 2025
    More actions

    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?

  17. youknowone commented on Dec 25, 2025

    @youknowone
    youknowone
    on Dec 25, 2025
    Member
    More actions

    This is fixed, thank you all!

  18. youknowone
    closed this as completedon Dec 25, 2025
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 newcomersGood for newcomersz-ca-2022Tag to track contrubution-academy 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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.