Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre-Parse strftime string #1057

Closed
Kixiron opened this issue May 9, 2023 · 7 comments
Closed

Pre-Parse strftime string #1057

Kixiron opened this issue May 9, 2023 · 7 comments

Comments

@Kixiron
Copy link

Kixiron commented May 9, 2023

A way to pre-parse a format/parse string would be really nice, I call DateTime::parse_from_str in a hot loop and I'd like not to re-parse the format string in every iteration

@pitdicker
Copy link
Collaborator

It is possible, but not well-documented yet. With StrftimeItems you can parse the format string, and then use it with parse() and Parsed::to_datetime. I don't think the API is great though 😄.

Just happened to figure this out for myself yesterday, with an example in this comment #1049 (comment).

@Kixiron
Copy link
Author

Kixiron commented May 9, 2023

I guess StrftimeItems lazily parses the string and then allows you to later reuse it? I don't see any sort of .reset() method on it, how do you restore it to a state that allows you to call parse() with it again?

@pitdicker
Copy link
Collaborator

Ai, it doesn't even allow reuse?
Is it for you an option to manually create an array with formatting items (again, pretty much undocumented)? https://docs.rs/chrono/latest/chrono/format/enum.Item.html

In Numeric and Literal you can find the items that would make up the fields of the format string. parse can accept an iterator over such an array.

@pitdicker
Copy link
Collaborator

Collecting the elements of the StrftimeItems iterator should also get you that array.

@jtmoon79
Copy link
Contributor

@Kixiron if you figure it out, would you please repost the reduced recipe here?

@Kixiron
Copy link
Author

Kixiron commented Jun 4, 2023

You just do what pitdicker said, StrftimeItems::new("%Y-%m-%d").collect::<Vec<_>>()

@djc djc closed this as not planned Won't fix, can't repro, duplicate, stale Jun 5, 2023
@djc
Copy link
Member

djc commented Jun 5, 2023

@Kixiron thanks for following up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants