Skip to content

Commit 675d63e

Browse files
author
Piotr Gulbinowicz
committed
use date package to calculate date diff
1 parent d74d94e commit 675d63e

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

pwa/flush/components.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"errors"
66
"fmt"
77
"log"
8-
"math"
98
"strconv"
109
"strings"
1110
"time"
1211

1312
"github.com/maxence-charriere/go-app/v10/pkg/app"
13+
d "github.com/rickb777/date/v2"
1414
)
1515

1616
const (
@@ -594,24 +594,24 @@ func FormatFlushTime(timeStart time.Time, timeEnd time.Time) string {
594594
0,
595595
64,
596596
)
597+
dayNow := d.NewAt(time.Now())
598+
dayFlushed := d.NewAt(timeStart)
599+
daysDiff := dayNow.Midnight().Sub(dayFlushed.Midnight()).Hours() / 24
597600
durationPrefix := duration + " min, 📅 "
598-
hoursAgo := math.Ceil(time.Now().Sub(timeStart).Hours())
599-
daysAgo := int(hoursAgo) / 24
600-
601601
log.Println("timeStart: ", timeStart)
602602
log.Println("timeEnd: ", timeEnd)
603-
log.Println("DaysAgo: ", daysAgo)
604-
log.Println("Hours ago: ", hoursAgo)
603+
log.Println("daysDiff: ", daysDiff)
604+
log.Println("daysDiff int: ", int(daysDiff))
605605
switch {
606-
case daysAgo <= 7:
607-
dayStr := fmt.Sprintf("%v days ago, ", daysAgo)
608-
if daysAgo == 0 {
606+
case daysDiff <= 7:
607+
dayStr := fmt.Sprintf("%v days ago, ", daysDiff)
608+
if daysDiff == 0 {
609609
dayStr = "today, "
610610
}
611-
if daysAgo == 1 {
611+
if daysDiff == 1 {
612612
dayStr = "yesterday, "
613613
}
614-
if daysAgo == 7 {
614+
if daysDiff == 7 {
615615
dayStr = "week ago, "
616616
}
617617
return durationPrefix + dayStr + timeStart.Format(

0 commit comments

Comments
 (0)