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

format a Duration #197

Open
Eh2406 opened this issue Dec 13, 2017 · 8 comments
Open

format a Duration #197

Eh2406 opened this issue Dec 13, 2017 · 8 comments

Comments

@Eh2406
Copy link
Contributor

Eh2406 commented Dec 13, 2017

I am new to the library But I am not seeing how to format a Duration, nor how to convert to any of the other types with formatting abilities.

@quodlibetor
Copy link
Contributor

quodlibetor commented Dec 14, 2017

Our duration is a re-export of time::Duration, which has both Display and various num_* methods (e.g. .num_weeks()) if you want a custom format.

but just using format!("{}", some_duration) should do it, formatting as an ISO Duration. playground example

@Eh2406
Copy link
Contributor Author

Eh2406 commented Dec 15, 2017

The relationship between time::Duration vs. chrono::Duration was not clear to me, thanks for explaining. The section that the top of the docs explaining it, actually got me more confused. It makes it clear that time::Duration is referred to as OldDuration, and newer, standard type for duration, is referred to as Duration. So I assumed that chrono::Duration is that newer, standard type for duration,

I can definitely roll my own from the num_* methods, but from the ISO you linked I may find what I need with text trimming the standard. I think the playground example is just a link to the playground. What example did you have in mind?

I had bean hoping that there would be a .format("fully custom thing %d:%H:%M:%S") Like there is for the other types.

@quodlibetor
Copy link
Contributor

Yeah actually I think that we are incorrectly exposing time::Duration as chrono::Duration, it was supposed to be an intermediate step on our way to exposing std::Duration. I just forgot :-(

I had bean hoping that there would be a .format("fully custom thing %d:%H:%M:%S") Like there is for the other types.

We could probably make that happen with our own format trait, and IMO that does belong in this lib. I'll add that as a wishlist item.

@Eh2406
Copy link
Contributor Author

Eh2406 commented Dec 15, 2017

@burkadurka came up with some clever workarounds.
https://www.reddit.com/r/rust/comments/7j09f1/hey_rustaceans_got_an_easy_question_ask_here/drar4ht/

@quodlibetor
Copy link
Contributor

That's nifty. I'm tempted to add that as a utility method to chrono as a quick way to use strptime formatting with durations.

@shaladdle
Copy link

Sorry to necro an old issue, but I wanted to throw in my 2c. @quodlibetor's suggestion of allowing a formatting string would be a step in the right direction, but I think it would be even better to support (in addition to) a more human-readable format, like the one used by Google's absl::FormatDuration and absl::ParseDuration here. I use this library frequently and it's by far the most easy to use time library I've used in any language

I think a large part of it is because it's easy to print and parse, which are critical for debugging and normal usage of most types. Additionally, command line apps really benefit from being able to parse that format, since it's quite human-readable.

@Veetaha
Copy link

Veetaha commented Oct 26, 2020

I wish chrono had means of custom formatting for durations with the strftime syntax.
I have a problem porting some code that uses JavaScript's moment-duration-format library

For example something like this JavaScript API with the template string would be very helpful:

moment.duration(123, "minutes").format("h:mm");
// "2:03"

@nkaretnikov
Copy link

I'd suggest at least adding this example to the docs (from the Reddit link posted earlier):
https://play.rust-lang.org/?gist=f8b913eb26a2929ab2adfb4c8a2b6139&version=stable

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

No branches or pull requests

6 participants