1
0

Improve duration format of overdue tasks in reminders

This commit is contained in:
kolaente
2021-04-18 14:25:50 +02:00
parent 410a7426cf
commit 7ff7b0d743
5 changed files with 135 additions and 213 deletions

View File

@ -0,0 +1,66 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package utils
import (
"fmt"
"math"
"strings"
"time"
)
// HumanizeDuration formats a time.Duration in a human-friendly format.
// Based on https://gist.github.com/harshavardhana/327e0577c4fed9211f65
func HumanizeDuration(duration time.Duration) string {
years := int64(duration.Hours() / 24 / 365)
days := int64(duration.Hours()/24) - years*365
weeks := days / 7
days -= weeks * 7
hours := int64(math.Mod(duration.Hours(), 24))
minutes := int64(math.Mod(duration.Minutes(), 60))
chunks := []struct {
singularName string
amount int64
}{
{"year", years},
{"week", weeks},
{"day", days},
{"hour", hours},
{"minute", minutes},
}
parts := []string{}
for _, chunk := range chunks {
switch chunk.amount {
case 0:
continue
case 1:
parts = append(parts, fmt.Sprintf("one %s", chunk.singularName))
default:
parts = append(parts, fmt.Sprintf("%d %ss", chunk.amount, chunk.singularName))
}
}
if len(parts) > 1 {
return strings.Join(parts[:len(parts)-1], ", ") + " and " + parts[len(parts)-1]
}
return strings.Join(parts, ", ")
}

View File

@ -0,0 +1,64 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package utils
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHumanizeDuration(t *testing.T) {
t.Run("one part", func(t *testing.T) {
d := 1 * time.Hour
dur := HumanizeDuration(d)
assert.Equal(t, "one hour", dur)
})
t.Run("amount > 1", func(t *testing.T) {
d := 2 * time.Hour
dur := HumanizeDuration(d)
assert.Equal(t, "2 hours", dur)
})
t.Run("2 parts", func(t *testing.T) {
d := 2*time.Hour + 48*time.Hour
dur := HumanizeDuration(d)
assert.Equal(t, "2 days and 2 hours", dur)
})
t.Run("multiple parts", func(t *testing.T) {
d := 2*time.Hour + 24*15*time.Hour
dur := HumanizeDuration(d)
assert.Equal(t, "2 weeks, one day and 2 hours", dur)
})
t.Run("years", func(t *testing.T) {
day := 24 * time.Hour
d := 2*time.Hour + 365*day + 14*day
dur := HumanizeDuration(d)
assert.Equal(t, "one year, 2 weeks and 2 hours", dur)
})
t.Run("ignore seconds", func(t *testing.T) {
d := 2*time.Hour + 48*time.Hour + 23*time.Second
dur := HumanizeDuration(d)
assert.Equal(t, "2 days and 2 hours", dur)
})
}