github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/man/times_test.go (about) 1 package man 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 ) 8 9 func TestPast(t *testing.T) { 10 now := time.Now() 11 testList{ 12 {"now", Time(now), "now"}, 13 {"1 second ago", Time(now.Add(-1 * time.Second)), "1 second ago"}, 14 {"12 seconds ago", Time(now.Add(-12 * time.Second)), "12 seconds ago"}, 15 {"30 seconds ago", Time(now.Add(-30 * time.Second)), "30 seconds ago"}, 16 {"45 seconds ago", Time(now.Add(-45 * time.Second)), "45 seconds ago"}, 17 {"1 minute ago", Time(now.Add(-63 * time.Second)), "1 minute ago"}, 18 {"15 minutes ago", Time(now.Add(-15 * time.Minute)), "15 minutes ago"}, 19 {"1 hour ago", Time(now.Add(-63 * time.Minute)), "1 hour ago"}, 20 {"2 hours ago", Time(now.Add(-2 * time.Hour)), "2 hours ago"}, 21 {"21 hours ago", Time(now.Add(-21 * time.Hour)), "21 hours ago"}, 22 {"1 day ago", Time(now.Add(-26 * time.Hour)), "1 day ago"}, 23 {"2 days ago", Time(now.Add(-49 * time.Hour)), "2 days ago"}, 24 {"3 days ago", Time(now.Add(-3 * Day)), "3 days ago"}, 25 {"1 week ago (1)", Time(now.Add(-7 * Day)), "1 week ago"}, 26 {"1 week ago (2)", Time(now.Add(-12 * Day)), "1 week ago"}, 27 {"2 weeks ago", Time(now.Add(-15 * Day)), "2 weeks ago"}, 28 {"1 month ago", Time(now.Add(-39 * Day)), "1 month ago"}, 29 {"3 months ago", Time(now.Add(-99 * Day)), "3 months ago"}, 30 {"1 year ago (1)", Time(now.Add(-365 * Day)), "1 year ago"}, 31 {"1 year ago (1)", Time(now.Add(-400 * Day)), "1 year ago"}, 32 {"2 years ago (1)", Time(now.Add(-548 * Day)), "2 years ago"}, 33 {"2 years ago (2)", Time(now.Add(-725 * Day)), "2 years ago"}, 34 {"2 years ago (3)", Time(now.Add(-800 * Day)), "2 years ago"}, 35 {"3 years ago", Time(now.Add(-3 * Year)), "3 years ago"}, 36 {"long ago", Time(now.Add(-LongTime)), "a long while ago"}, 37 }.validate(t) 38 } 39 40 func TestReltimeOffbyone(t *testing.T) { 41 testList{ 42 {"1w-1", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, -1), "ago", ""), "6 days ago"}, 43 {"1w±0", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, 0), "ago", ""), "1 week ago"}, 44 {"1w+1", RelTime(time.Unix(0, 0), time.Unix(7*24*60*60, 1), "ago", ""), "1 week ago"}, 45 {"2w-1", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, -1), "ago", ""), "1 week ago"}, 46 {"2w±0", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, 0), "ago", ""), "2 weeks ago"}, 47 {"2w+1", RelTime(time.Unix(0, 0), time.Unix(14*24*60*60, 1), "ago", ""), "2 weeks ago"}, 48 }.validate(t) 49 } 50 51 func TestFuture(t *testing.T) { 52 // Add a little time so that these things properly line up in 53 // the future. 54 now := time.Now().Add(time.Millisecond * 250) 55 testList{ 56 {"now", Time(now), "now"}, 57 {"1 second from now", Time(now.Add(+1 * time.Second)), "1 second from now"}, 58 {"12 seconds from now", Time(now.Add(+12 * time.Second)), "12 seconds from now"}, 59 {"30 seconds from now", Time(now.Add(+30 * time.Second)), "30 seconds from now"}, 60 {"45 seconds from now", Time(now.Add(+45 * time.Second)), "45 seconds from now"}, 61 {"15 minutes from now", Time(now.Add(+15 * time.Minute)), "15 minutes from now"}, 62 {"2 hours from now", Time(now.Add(+2 * time.Hour)), "2 hours from now"}, 63 {"21 hours from now", Time(now.Add(+21 * time.Hour)), "21 hours from now"}, 64 {"1 day from now", Time(now.Add(+26 * time.Hour)), "1 day from now"}, 65 {"2 days from now", Time(now.Add(+49 * time.Hour)), "2 days from now"}, 66 {"3 days from now", Time(now.Add(+3 * Day)), "3 days from now"}, 67 {"1 week from now (1)", Time(now.Add(+7 * Day)), "1 week from now"}, 68 {"1 week from now (2)", Time(now.Add(+12 * Day)), "1 week from now"}, 69 {"2 weeks from now", Time(now.Add(+15 * Day)), "2 weeks from now"}, 70 {"1 month from now", Time(now.Add(+30 * Day)), "1 month from now"}, 71 {"1 year from now", Time(now.Add(+365 * Day)), "1 year from now"}, 72 {"2 years from now", Time(now.Add(+2 * Year)), "2 years from now"}, 73 {"a while from now", Time(now.Add(+LongTime)), "a long while from now"}, 74 }.validate(t) 75 } 76 77 func TestRange(t *testing.T) { 78 start := time.Time{} 79 end := time.Unix(math.MaxInt64, math.MaxInt64) 80 x := RelTime(start, end, "ago", "from now") 81 if x != "a long while from now" { 82 t.Errorf("Expected a long while from now, got %q", x) 83 } 84 } 85 86 func TestCustomRelTime(t *testing.T) { 87 now := time.Now().Add(time.Millisecond * 250) 88 magnitudes := []RelTimeMagnitude{ 89 {time.Second, "now", time.Second}, 90 {2 * time.Second, "1 second %s", 1}, 91 {time.Minute, "%d seconds %s", time.Second}, 92 {Day - time.Second, "%d minutes %s", time.Minute}, 93 {Day, "%d hours %s", time.Hour}, 94 {2 * Day, "1 day %s", 1}, 95 {Week, "%d days %s", Day}, 96 {2 * Week, "1 week %s", 1}, 97 {6 * Month, "%d weeks %s", Week}, 98 {Year, "%d months %s", Month}, 99 } 100 customRelTime := func(then time.Time) string { 101 return CustomRelTime(then, time.Now(), "ago", "from now", magnitudes) 102 } 103 testList{ 104 {"now", customRelTime(now), "now"}, 105 {"1 second from now", customRelTime(now.Add(+1 * time.Second)), "1 second from now"}, 106 {"12 seconds from now", customRelTime(now.Add(+12 * time.Second)), "12 seconds from now"}, 107 {"30 seconds from now", customRelTime(now.Add(+30 * time.Second)), "30 seconds from now"}, 108 {"45 seconds from now", customRelTime(now.Add(+45 * time.Second)), "45 seconds from now"}, 109 {"15 minutes from now", customRelTime(now.Add(+15 * time.Minute)), "15 minutes from now"}, 110 {"2 hours from now", customRelTime(now.Add(+2 * time.Hour)), "120 minutes from now"}, 111 {"21 hours from now", customRelTime(now.Add(+21 * time.Hour)), "1260 minutes from now"}, 112 {"1 day from now", customRelTime(now.Add(+26 * time.Hour)), "1 day from now"}, 113 {"2 days from now", customRelTime(now.Add(+49 * time.Hour)), "2 days from now"}, 114 {"3 days from now", customRelTime(now.Add(+3 * Day)), "3 days from now"}, 115 {"1 week from now (1)", customRelTime(now.Add(+7 * Day)), "1 week from now"}, 116 {"1 week from now (2)", customRelTime(now.Add(+12 * Day)), "1 week from now"}, 117 {"2 weeks from now", customRelTime(now.Add(+15 * Day)), "2 weeks from now"}, 118 {"1 month from now", customRelTime(now.Add(+30 * Day)), "4 weeks from now"}, 119 {"6 months from now", customRelTime(now.Add(+6*Month - time.Second)), "25 weeks from now"}, 120 {"1 year from now", customRelTime(now.Add(+365 * Day)), "12 months from now"}, 121 {"2 years from now", customRelTime(now.Add(+2 * Year)), "24 months from now"}, 122 {"a while from now", customRelTime(now.Add(+LongTime)), "444 months from now"}, 123 }.validate(t) 124 }