github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbtime/humanize_test.go (about)

     1  package kbtime
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func mustAddLongDuration(t *testing.T, tval time.Time, s string) time.Time {
    11  	ret, err := AddLongDuration(tval, s)
    12  	require.NoError(t, err)
    13  	return ret
    14  }
    15  
    16  func TestRelTime(t *testing.T) {
    17  	now := time.Date(2020, time.January, 10, 14, 0, 0, 0, time.UTC)
    18  
    19  	var tests = []struct {
    20  		a   time.Time
    21  		b   time.Time
    22  		out string
    23  	}{
    24  		{now, mustAddLongDuration(t, now, "1s"), "1 second"},
    25  		{now, mustAddLongDuration(t, now, "29s"), "29 seconds"},
    26  		{now, mustAddLongDuration(t, now, "1m"), "1 minute"},
    27  		{now, mustAddLongDuration(t, now, "14m"), "14 minutes"},
    28  		{now, mustAddLongDuration(t, now, "59m"), "59 minutes"},
    29  		{now, mustAddLongDuration(t, now, "61m"), "1 hour"},
    30  		{now, mustAddLongDuration(t, now, "110m"), "1 hour"},
    31  		{now, mustAddLongDuration(t, now, "1h"), "1 hour"},
    32  		{now, mustAddLongDuration(t, now, "19h"), "19 hours"},
    33  		{now, mustAddLongDuration(t, now, "46h"), "1 day"},
    34  		{now, mustAddLongDuration(t, now, "1D"), "1 day"},
    35  		{now, mustAddLongDuration(t, now, "6D"), "6 days"},
    36  		{now, mustAddLongDuration(t, now, "13D"), "1 week"},
    37  		{now, mustAddLongDuration(t, now, "25D"), "3 weeks"},
    38  		{now, mustAddLongDuration(t, now, "1M"), "1 month"},
    39  		{now, mustAddLongDuration(t, now, "11M"), "11 months"},
    40  		{now, mustAddLongDuration(t, now, "13M"), "1 year"},
    41  		{now, mustAddLongDuration(t, now, "19M"), "1 year"},
    42  		{now, mustAddLongDuration(t, now, "23M"), "1 year"},
    43  		{now, mustAddLongDuration(t, now, "63M"), "5 years"},
    44  		{now, mustAddLongDuration(t, now, "1Y"), "1 year"},
    45  		{now, mustAddLongDuration(t, now, "2Y"), "2 years"},
    46  		{now, mustAddLongDuration(t, now, "3Y"), "3 years"},
    47  		{now, mustAddLongDuration(t, now, "10Y"), "10 years"},
    48  		{now, mustAddLongDuration(t, now, "100Y"), "100 years"},
    49  		{now, mustAddLongDuration(t, now, "1000Y"), "1000 years"},
    50  		{now, mustAddLongDuration(t, now, "10000Y"), "10000 years"},
    51  		{now, mustAddLongDuration(t, now, "100000Y"), "100000 years"},
    52  		{now, mustAddLongDuration(t, now, "1000000Y"), "1000000 years"},
    53  		{now, mustAddLongDuration(t, now, "1000000000Y"), "1000000000 years"},
    54  	}
    55  	for _, tt := range tests {
    56  		tt := tt
    57  		t.Run(tt.out, func(t *testing.T) {
    58  			actual := RelTime(tt.a, tt.b, "", "")
    59  			require.Equal(t, tt.out, actual)
    60  		})
    61  	}
    62  }