I'm surprised nobody has asked this before, but... How do I trivially print a NominalDiffTime
as hours, minutes and seconds? (And possibly days, if it happens to be that long...)
For reasons unknown, the Show
instance prints total seconds, which is obviously useless. (How long is 13,055.22 seconds? Is that a few minutes? A day? Half an hour? I have no idea!)
There's the FormatTime
class, but it doesn't apply to NominalDiffTime
.
It seems you can use the floor
method to get total seconds as an actual number, but then what do you do with it?
As far as I can tell, DiffTime
doesn't help either.
There must be a way to print time durations sanely...
You can print a DiffTime
-- which actually represents a duration, and is likely the type you should be using -- by going through TimeOfDay
. Getting your hands on a DiffTime
correctly is actually a little bit tricky; you'll need:
utcToTAITime
to convert to AbsoluteTime
, anddiffAbsoluteTime
to get a DiffTime
, andtimeToTimeOfDay
to have the library do your divmod's for you, and finallyformatTime
to print this.floor
[1] gives you (as you say) the number of seconds, which you can then use div
and mod
to convert into hours, minutes and seconds.
For example:
floor ndt `div` 60 -- minutes, which may be more than 59
floor ndt `mod` 60 -- seconds
[1] unlike fromEnum
, which is a "conversion function" but doesn't convert to seconds, contrary to what the documentation says.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With