From cdad63208287eb2ef26cd84e6c8176d0668a8cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 17 Jan 2020 14:41:41 +0200 Subject: [PATCH] Show sub-second durations with higher precision, 2 digits after decimal point. Fixes #321 --- CHANGELOG.md | 1 + hc/lib/date.py | 5 ++++- hc/lib/tests/test_date.py | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38b941d5..89f3d414 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. - createsuperuser management command requires an unique email address (#318) - For superusers, show "Site Administration" in top navigation, note in README (#317) - Make Ping.body size limit configurable (#301) +- Show sub-second durations with higher precision, 2 digits after decimal point (#321) ### Bug Fixes - Increase the allowable length of Matrix room alias to 100 (#320) diff --git a/hc/lib/date.py b/hc/lib/date.py index 6f445dc3..d95f6bc8 100644 --- a/hc/lib/date.py +++ b/hc/lib/date.py @@ -37,8 +37,11 @@ def format_duration(td): def format_hms(td): - total_seconds = int(td.total_seconds()) + total_seconds = td.total_seconds() + if 0.01 <= total_seconds < 1: + return "%.2f sec" % total_seconds + total_seconds = int(total_seconds) result = [] mins, secs = divmod(total_seconds, 60) diff --git a/hc/lib/tests/test_date.py b/hc/lib/tests/test_date.py index 541a6367..2a017f04 100644 --- a/hc/lib/tests/test_date.py +++ b/hc/lib/tests/test_date.py @@ -5,6 +5,10 @@ from hc.lib.date import format_hms, choose_next_report_date class DateFormattingTestCase(TestCase): + def test_sub_second_works(self): + s = format_hms(td(seconds=0.12)) + self.assertEqual(s, "0.12 sec") + def test_mins_secs_work(self): s = format_hms(td(seconds=0)) self.assertEqual(s, "0 sec")