Advertisement
jxsl13

Go/Golang time formatting

Nov 15th, 2020 (edited)
1,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.17 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "time"
  6. )
  7.  
  8. var (
  9.     months = map[time.Month]string{
  10.         1:  "Jan",
  11.         2:  "Feb",
  12.         3:  "Mar",
  13.         4:  "Apr",
  14.         5:  "May",
  15.         6:  "Jun",
  16.         7:  "Jul",
  17.         8:  "Aug",
  18.         9:  "Sep",
  19.         10: "Oct",
  20.         11: "Nov",
  21.         12: "Dec",
  22.     }
  23.     tests = []Tuple{
  24.         {time.Unix(int64(1605389729), 0), time.Now()},
  25.         {time.Now().Add(-30 * time.Minute), time.Now()},
  26.         {time.Now().Add(-31 * time.Minute), time.Now()},
  27.         {time.Now().Add(-24 * time.Hour), time.Now()},
  28.         {time.Now().Add(-48 * time.Hour), time.Now()},
  29.     }
  30. )
  31.  
  32. func init() {
  33.  
  34.     // edge case test
  35.     todayMorning, err := time.Parse(time.RFC3339, "2020-11-15T00:20:50.52Z")
  36.     if err != nil {
  37.         panic(err)
  38.     }
  39.     yesterdayNight, err := time.Parse(time.RFC3339, "2020-11-14T23:55:21.52Z")
  40.  
  41.     if err != nil {
  42.         panic(err)
  43.     }
  44.  
  45.     tests = append(tests, Tuple{yesterdayNight, todayMorning})
  46.  
  47.     // TEST #2
  48.     // edge case test
  49.     todayMorning, err = time.Parse(time.RFC3339, "2020-11-15T00:20:50.52Z")
  50.     if err != nil {
  51.         panic(err)
  52.     }
  53.     // this should be shown as "Yesterday ..."
  54.     yesterdayNight, err = time.Parse(time.RFC3339, "2020-11-14T23:30:21.52Z")
  55.  
  56.     if err != nil {
  57.         panic(err)
  58.     }
  59.     tests = append(tests, Tuple{yesterdayNight, todayMorning})
  60. }
  61.  
  62. // Tuple of two times that are compared
  63. type Tuple struct {
  64.     Date time.Time
  65.     Now  time.Time
  66. }
  67.  
  68. func log(s string, date, now time.Time) {
  69.     //fmt.Println(s)
  70.     fmt.Printf("%-20s\t(date:%s, now:%s)\n", s, date, now)
  71. }
  72.  
  73. func main() {
  74.  
  75.     for idx, t := range tests {
  76.         fmt.Printf("%3d:\t", idx)
  77.         Test(t.Date, t.Now)
  78.     }
  79.  
  80. }
  81.  
  82. // Test allows to change the current "now" time in order to test the functionality
  83. func Test(date, now time.Time) {
  84.  
  85.     if !date.Before(now) {
  86.         log("Date is in the future", date, now)
  87.         return
  88.     }
  89.  
  90.     const aMomentAgoMinutes = 30
  91.     const shortDuration = aMomentAgoMinutes * time.Minute
  92.  
  93.     // now
  94.     hours, mins, secs := now.Clock()
  95.     durationPassedToday := time.Duration(hours)*time.Hour + time.Duration(mins)*time.Minute + time.Duration(secs)*time.Second
  96.  
  97.     if now.Sub(date) <= shortDuration && durationPassedToday <= shortDuration {
  98.         // edge case, where yesterday is less than 30 minutes ago
  99.         // e.g. today at 0:21 and there the date is e.g. 23:55
  100.         log("A moment ago.", date, now)
  101.         return
  102.     }
  103.  
  104.     // today 0:00
  105.     yesterdayMidnight := now.Add(-durationPassedToday)
  106.  
  107.     // yesterday 0:00
  108.     dayBeforeYesterdayMidnight := yesterdayMidnight.Add(-24 * time.Hour)
  109.  
  110.     fStr := ""
  111.     switch unixTime := date.Unix(); {
  112.     case unixTime < dayBeforeYesterdayMidnight.Unix():
  113.         // Nov 14, 2020 at 22:53
  114.         // epoch(0) <= unixTime < yesterday 0:00
  115.         fStr = fmt.Sprintf("%s %d, %d at %02d:%02d",
  116.             months[date.Month()],
  117.             date.Day(),
  118.             date.Year(),
  119.             date.Hour(),
  120.             date.Minute(),
  121.         )
  122.     case unixTime < yesterdayMidnight.Unix():
  123.         // Yesterday
  124.         // unixTime < today 0:00
  125.         fStr = fmt.Sprintf("Yesterday at %02d:%02d", date.Hour(), date.Minute())
  126.     case unixTime < now.Add(-shortDuration).Unix():
  127.         // Today
  128.         // today 0:00 <= unixTime < now - 30 minutes
  129.         fStr = fmt.Sprintf("Today at %02d:%02d", date.Hour(), date.Minute())
  130.     default:
  131.         // A moment ago
  132.         // now - 30 minutes <= unixTime < now
  133.         fStr = "A moment ago."
  134.     }
  135.     log(fStr, date, now)
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement