From 9a15fabd0687940611bc214353afe55c61e26afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Tue, 24 May 2016 11:20:56 +0300 Subject: [PATCH] It's more precise to say "30 days" than "1 month" --- hc/front/templatetags/hc_extras.py | 7 +++++-- hc/front/tests/test_hc_extras.py | 4 ++-- static/js/checks.js | 8 ++++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index 10ebcbee..3182cad3 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -13,7 +13,6 @@ MINUTE = Unit("minute", 60) HOUR = Unit("hour", MINUTE.nsecs * 60) DAY = Unit("day", HOUR.nsecs * 24) WEEK = Unit("week", DAY.nsecs * 7) -MONTH = Unit("month", DAY.nsecs * 30) @register.filter @@ -21,7 +20,11 @@ def hc_duration(td): remaining_seconds = int(td.total_seconds()) result = [] - for unit in (MONTH, WEEK, DAY, HOUR, MINUTE): + for unit in (WEEK, DAY, HOUR, MINUTE): + if unit == WEEK and remaining_seconds % unit.nsecs != 0: + # Say "8 days" instead of "1 week 1 day" + continue + v, remaining_seconds = divmod(remaining_seconds, unit.nsecs) if v == 1: result.append("1 %s" % unit.name) diff --git a/hc/front/tests/test_hc_extras.py b/hc/front/tests/test_hc_extras.py index da1d90cb..5e46105f 100644 --- a/hc/front/tests/test_hc_extras.py +++ b/hc/front/tests/test_hc_extras.py @@ -14,8 +14,8 @@ class HcExtrasTestCase(TestCase): (86400, "1 day"), (604800, "1 week"), (2419200, "4 weeks"), - (2592000, "1 month"), - (3801600, "1 month 2 weeks") + (2592000, "30 days"), + (3801600, "44 days") ] for seconds, expected_result in samples: diff --git a/static/js/checks.js b/static/js/checks.js index c50fc9e2..c1fc75aa 100644 --- a/static/js/checks.js +++ b/static/js/checks.js @@ -4,13 +4,17 @@ $(function () { var HOUR = {name: "hour", nsecs: MINUTE.nsecs * 60}; var DAY = {name: "day", nsecs: HOUR.nsecs * 24}; var WEEK = {name: "week", nsecs: DAY.nsecs * 7}; - var MONTH = {name: "month", nsecs: DAY.nsecs * 30}; - var UNITS = [MONTH, WEEK, DAY, HOUR, MINUTE]; + var UNITS = [WEEK, DAY, HOUR, MINUTE]; var secsToText = function(total) { var remainingSeconds = Math.floor(total); var result = ""; for (var i=0, unit; unit=UNITS[i]; i++) { + if (unit === WEEK && remainingSeconds % unit.nsecs != 0) { + // Say "8 days" instead of "1 week 1 day" + continue + } + var count = Math.floor(remainingSeconds / unit.nsecs); remainingSeconds = remainingSeconds % unit.nsecs;