Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "time"
- )
- var (
- months = map[time.Month]string{
- 1: "Jan",
- 2: "Feb",
- 3: "Mar",
- 4: "Apr",
- 5: "May",
- 6: "Jun",
- 7: "Jul",
- 8: "Aug",
- 9: "Sep",
- 10: "Oct",
- 11: "Nov",
- 12: "Dec",
- }
- tests = []Tuple{
- {time.Unix(int64(1605389729), 0), time.Now()},
- {time.Now().Add(-30 * time.Minute), time.Now()},
- {time.Now().Add(-31 * time.Minute), time.Now()},
- {time.Now().Add(-24 * time.Hour), time.Now()},
- {time.Now().Add(-48 * time.Hour), time.Now()},
- }
- )
- func init() {
- // edge case test
- todayMorning, err := time.Parse(time.RFC3339, "2020-11-15T00:20:50.52Z")
- if err != nil {
- panic(err)
- }
- yesterdayNight, err := time.Parse(time.RFC3339, "2020-11-14T23:55:21.52Z")
- if err != nil {
- panic(err)
- }
- tests = append(tests, Tuple{yesterdayNight, todayMorning})
- // TEST #2
- // edge case test
- todayMorning, err = time.Parse(time.RFC3339, "2020-11-15T00:20:50.52Z")
- if err != nil {
- panic(err)
- }
- // this should be shown as "Yesterday ..."
- yesterdayNight, err = time.Parse(time.RFC3339, "2020-11-14T23:30:21.52Z")
- if err != nil {
- panic(err)
- }
- tests = append(tests, Tuple{yesterdayNight, todayMorning})
- }
- // Tuple of two times that are compared
- type Tuple struct {
- Date time.Time
- Now time.Time
- }
- func log(s string, date, now time.Time) {
- //fmt.Println(s)
- fmt.Printf("%-20s\t(date:%s, now:%s)\n", s, date, now)
- }
- func main() {
- for idx, t := range tests {
- fmt.Printf("%3d:\t", idx)
- Test(t.Date, t.Now)
- }
- }
- // Test allows to change the current "now" time in order to test the functionality
- func Test(date, now time.Time) {
- if !date.Before(now) {
- log("Date is in the future", date, now)
- return
- }
- const aMomentAgoMinutes = 30
- const shortDuration = aMomentAgoMinutes * time.Minute
- // now
- hours, mins, secs := now.Clock()
- durationPassedToday := time.Duration(hours)*time.Hour + time.Duration(mins)*time.Minute + time.Duration(secs)*time.Second
- if now.Sub(date) <= shortDuration && durationPassedToday <= shortDuration {
- // edge case, where yesterday is less than 30 minutes ago
- // e.g. today at 0:21 and there the date is e.g. 23:55
- log("A moment ago.", date, now)
- return
- }
- // today 0:00
- yesterdayMidnight := now.Add(-durationPassedToday)
- // yesterday 0:00
- dayBeforeYesterdayMidnight := yesterdayMidnight.Add(-24 * time.Hour)
- fStr := ""
- switch unixTime := date.Unix(); {
- case unixTime < dayBeforeYesterdayMidnight.Unix():
- // Nov 14, 2020 at 22:53
- // epoch(0) <= unixTime < yesterday 0:00
- fStr = fmt.Sprintf("%s %d, %d at %02d:%02d",
- months[date.Month()],
- date.Day(),
- date.Year(),
- date.Hour(),
- date.Minute(),
- )
- case unixTime < yesterdayMidnight.Unix():
- // Yesterday
- // unixTime < today 0:00
- fStr = fmt.Sprintf("Yesterday at %02d:%02d", date.Hour(), date.Minute())
- case unixTime < now.Add(-shortDuration).Unix():
- // Today
- // today 0:00 <= unixTime < now - 30 minutes
- fStr = fmt.Sprintf("Today at %02d:%02d", date.Hour(), date.Minute())
- default:
- // A moment ago
- // now - 30 minutes <= unixTime < now
- fStr = "A moment ago."
- }
- log(fStr, date, now)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement