Rfcs: Add as_millis function to std::time::Duration

Created on 16 Mar 2016  路  14Comments  路  Source: rust-lang/rfcs

Hello,

It's funny because there's a from_millis function but not an as_millis one.

What do you guys think? It's a simple change.

Cheers

T-libs

Most helpful comment

I think the time handling in rust a pretty serious ergonomic issue. To easily do a System.currentTimeMillis() or Date.now(), you have to pull in 3rd party crates or write several lines of code.

So whilst I agree rust std should stop short of full blown chronos time handling/formatting, milliseconds (and epoch) are so common they ought to be there.

All 14 comments

You cannot convert duration to milliseconds losslessly whereas you can do that in the other direction.

@nagisa of course not, but it would still be very useful, nonetheless. An as_nanos would be very useful too.

The duration RFC explicitly mentions acceptable losses:

In general, this proposal considers it acceptable to reduce the granularity of timeouts (eliminating nanosecond granularity if only milliseconds are supported) and to truncate very large timeouts.

So it is surprising that everyone has to implement the reductions manually.

Yes, this patttern is virtually everywhere.

Looks like with https://github.com/rust-lang/rust/issues/35118 (u128) landing, this would be possible without having to truncate large values.

The rounding problem is silly. .as_millis() can express 584.9 million years in u64.

What is a realistic use case for needing millisecond precision on a duration longer than half a billion years? (but also doesn't need duration longer than 500 billion years)

Perhaps we could offer subsec_millis and subsec_micros instead? It'd be nice to have something like that.

I think the time handling in rust a pretty serious ergonomic issue. To easily do a System.currentTimeMillis() or Date.now(), you have to pull in 3rd party crates or write several lines of code.

So whilst I agree rust std should stop short of full blown chronos time handling/formatting, milliseconds (and epoch) are so common they ought to be there.

Totally agree. It's an ergonomic issue. Call me stupid, but this is the code I had to fix today:

let sec = (elapsed.as_secs() as f64) + f64::from(elapsed.subsec_nanos()) / 1000_000.0;

See the issue? It worked fine until our queries became too long. 馃ぅ

Any progress on this ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

torkleyy picture torkleyy  路  3Comments

mahkoh picture mahkoh  路  3Comments

3442853561 picture 3442853561  路  3Comments

mqudsi picture mqudsi  路  3Comments

camden-smallwood-zz picture camden-smallwood-zz  路  3Comments