Browse Source

It's more precise to say "30 days" than "1 month"

pull/64/head
Pēteris Caune 9 years ago
parent
commit
9a15fabd06
3 changed files with 13 additions and 6 deletions
  1. +5
    -2
      hc/front/templatetags/hc_extras.py
  2. +2
    -2
      hc/front/tests/test_hc_extras.py
  3. +6
    -2
      static/js/checks.js

+ 5
- 2
hc/front/templatetags/hc_extras.py View File

@ -13,7 +13,6 @@ MINUTE = Unit("minute", 60)
HOUR = Unit("hour", MINUTE.nsecs * 60) HOUR = Unit("hour", MINUTE.nsecs * 60)
DAY = Unit("day", HOUR.nsecs * 24) DAY = Unit("day", HOUR.nsecs * 24)
WEEK = Unit("week", DAY.nsecs * 7) WEEK = Unit("week", DAY.nsecs * 7)
MONTH = Unit("month", DAY.nsecs * 30)
@register.filter @register.filter
@ -21,7 +20,11 @@ def hc_duration(td):
remaining_seconds = int(td.total_seconds()) remaining_seconds = int(td.total_seconds())
result = [] 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) v, remaining_seconds = divmod(remaining_seconds, unit.nsecs)
if v == 1: if v == 1:
result.append("1 %s" % unit.name) result.append("1 %s" % unit.name)


+ 2
- 2
hc/front/tests/test_hc_extras.py View File

@ -14,8 +14,8 @@ class HcExtrasTestCase(TestCase):
(86400, "1 day"), (86400, "1 day"),
(604800, "1 week"), (604800, "1 week"),
(2419200, "4 weeks"), (2419200, "4 weeks"),
(2592000, "1 month"),
(3801600, "1 month 2 weeks")
(2592000, "30 days"),
(3801600, "44 days")
] ]
for seconds, expected_result in samples: for seconds, expected_result in samples:


+ 6
- 2
static/js/checks.js View File

@ -4,13 +4,17 @@ $(function () {
var HOUR = {name: "hour", nsecs: MINUTE.nsecs * 60}; var HOUR = {name: "hour", nsecs: MINUTE.nsecs * 60};
var DAY = {name: "day", nsecs: HOUR.nsecs * 24}; var DAY = {name: "day", nsecs: HOUR.nsecs * 24};
var WEEK = {name: "week", nsecs: DAY.nsecs * 7}; 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 secsToText = function(total) {
var remainingSeconds = Math.floor(total); var remainingSeconds = Math.floor(total);
var result = ""; var result = "";
for (var i=0, unit; unit=UNITS[i]; i++) { 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); var count = Math.floor(remainingSeconds / unit.nsecs);
remainingSeconds = remainingSeconds % unit.nsecs; remainingSeconds = remainingSeconds % unit.nsecs;


Loading…
Cancel
Save