Re: how to round durations to most significant unit ? (5 secs, 889 ms, and 811 μs = 5 secs)
On Saturday, February 15, 2014 22:13:07 Steven Schveighoffer wrote: On Fri, 14 Feb 2014 17:14:03 -0500, Jonathan M Davis jmdavisp...@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; } You are doing the calculation for every test. Better: if(d = dur!units(1)) ... Yeah. That would be better. Now, I think that the dur!units(1) should be statically determined, but if not, I think you could make it statically determined via enum. It'll only be statically determined if it's in a context that requires it. Unless something has changed recently, the compiler never tries to do CTFE if it doesn't have to. So, you'd need to use an enum to force it. - Jonathan M Davis
Re: how to round durations to most significant unit ? (5 secs, 889 ms, and 811 μs = 5 secs)
On Fri, 14 Feb 2014 17:14:03 -0500, Jonathan M Davis jmdavisp...@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; } You are doing the calculation for every test. Better: if(d = dur!units(1)) ... Now, I think that the dur!units(1) should be statically determined, but if not, I think you could make it statically determined via enum. -Steve
Re: how to round durations to most significant unit ? (5 secs, 889 ms, and 811 μs = 5 secs)
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
Re: how to round durations to most significant unit ? (5 secs, 889 ms, and 811 μs = 5 secs)
thanks! On Fri, Feb 14, 2014 at 2:14 PM, Jonathan M Davis jmdavisp...@gmx.comwrote: 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
how to round durations to most significant unit ? (5 secs, 889 ms, and 811 μs = 5 secs)
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. Similarly, it would be nice to have a module in phobos for representing other units such as bytes, and have a similar rounding scheme as in unix command 'ls -lh' which shows 532K, 12M etc)