0
votes

Is there a standard or accepted best practice for how times should be displayed as HH:MM when the source time has HH:MM:SS precision? Should seconds be truncated or rounded to the nearest minute?

Socially, if I looked at a digital clock and saw that it was 4:00:45, I would never tell someone it was 4:01. But I didn't know if this convention is universal or if it applies in computing too.

Also, rounding to the nearest minute might produce unexpected behavior, e.g. if the rounding causes the hour or date to change. This doesn't necessarily apply to the particular use-case we're dealing with today, but I can easily imagine another use case where a list of "Sales in January" includes a sale on 31-Jan 23:59:59 that would be displayed as 1-Feb 00:00

If context is relevant to the answer, this use-case is a SQL Server app that converts a datetime to a smalldatetime, which SQL Server will round to the nearest minute. The result will be displayed as HH:MM in a C# web application. The conversion happens in legacy code that we can't change right now, but we can force truncation of seconds instead of rounding.

But I'm not sure if we should do this.

2
Interesting...never really thought about it, to be honest; I imagine the reason why we "round up" has more to do with the underlying representation being basically a big 'ol long value that we "round off" to specific precision.JerKimball
Truncate. Think bigger scale. If you were only displaying the month and year, would you round up from "December 2012" to "January 2013" just because you're halfway through December?JosephHirn
you provide your own answer, if you dont go up with 4:00:45 why go up with 23:59:59. that time would still be 23:59Jastill
Write a program that displays three clocks. One displays hours minutes seconds normally. One displays hours and minutes, truncating. One displays hours and minutes, rounding to nearest. Watch it for a few minutes and you will quickly see why truncating is superior to rounding.Eric Lippert
And incidentally, there used to be a bug in Windows whereby in an extremely unlikely scenario it was possible for the "round to the nearest second" algorithm to round two days wrong. Be careful if you decide to go with a rounding algorithm.Eric Lippert

2 Answers

1
votes

In general with dates and times it's best to round down.

To me "1 week ago" means "at least 7 days, but less than 14 days". A similar idea applies to times.

0
votes

Unless it's critical for ordering of the parent object I really wouldn't bother. If you don't need the seconds; they can be considered low-priority. I'd personally round it because I like the precision, even if fractal.

If you're aware that it can cause unexpected behaviour; whomever interprets the data should account for it - but you can make it easier on them by adding a note in your code that it can cause this so that they are aware.