Re: how to round durations to most significant unit ? (5 secs, 889 ms, and 811 μs => 5 secs)

Timothee Cour thelastmammoth at gmail.com
Fri Feb 14 15:43:37 PST 2014


thanks!


On Fri, Feb 14, 2014 at 2:14 PM, Jonathan M Davis <jmdavisProg at gmx.com>wrote:

> On Thursday, February 13, 2014 23:37:13 Timothee Cour wrote:
> > Is there a function to do this?
> > If not and I/someone writes it, is there interest to add it to
> std.datetime?
> >
> > Duration t = ...;
> > t.to!string => 5 secs, 889 ms, and 811 μs
> > t.round.to!string    => 5 secs
> >
> > t=...;
> > t.to!string => 889 ms, and 811 μs
> > t.round.to!string    => 889 secs
> >
> > Use case: shorter logs.
>
> There is no function for that, and no one's ever asked for anything like
> it,
> so I don't know how worthwhile it is to add. However, one possible
> implementation would be
>
> auto roundToLargest(Duration d)
> {
>     foreach(units; TypeTuple!("weeks", "days", "hours", "minutes",
>                               "seconds", "msecs", "usecs"))
>     {
>         immutable value = d.total!units();
>         if(value != 0)
>             return dur!units(value);
>     }
>
>     return d;
> }
>
> - Jonathan M Davis
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140214/2c7273dc/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list