github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/local_time_test.go (about) 1 package chrono_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/go-chrono/chrono" 8 ) 9 10 func TestLocalTime(t *testing.T) { 11 time := chrono.LocalTimeOf(12, 30, 59, 12345678) 12 13 hour, min, sec := time.Clock() 14 if hour != 12 { 15 t.Errorf("time.Clock() hour = %d, want 12", hour) 16 } 17 18 if min != 30 { 19 t.Errorf("time.Clock() min = %d, want 30", min) 20 } 21 22 if sec != 59 { 23 t.Errorf("time.Clock() sec = %d, want 59", sec) 24 } 25 26 if nsec := time.Nanosecond(); nsec != 12345678 { 27 t.Errorf("time.Nanosecond() = %d, want 12345678", nsec) 28 } 29 } 30 31 func TestLocalTime_String(t *testing.T) { 32 for _, tt := range []struct { 33 name string 34 time chrono.LocalTime 35 expected string 36 }{ 37 {"simple", chrono.LocalTimeOf(9, 0, 0, 0), "09:00:00"}, 38 {"nanoseconds", chrono.LocalTimeOf(9, 0, 0, 12345678), "09:00:00.012345678"}, 39 } { 40 t.Run(tt.name, func(t *testing.T) { 41 if output := tt.time.String(); output != tt.expected { 42 t.Errorf("LocalTime.String() = %s, want %s", output, tt.expected) 43 } 44 }) 45 } 46 } 47 48 func TestLocalTime_BusinessHour(t *testing.T) { 49 time := chrono.LocalTimeOf(25, 0, 0, 0) 50 51 if hour := time.BusinessHour(); hour != 25 { 52 t.Errorf("time.Hour() = %d, want 25", hour) 53 } 54 55 if hour, _, _ := time.Clock(); hour != 1 { 56 t.Errorf("time.Hour() = %d, want 1", hour) 57 } 58 } 59 60 func TestLocalTime_Sub(t *testing.T) { 61 for _, tt := range []struct { 62 t1 chrono.LocalTime 63 t2 chrono.LocalTime 64 diff chrono.Extent 65 }{ 66 {chrono.LocalTimeOf(12, 0, 0, 0), chrono.LocalTimeOf(6, 0, 0, 0), 6 * chrono.Hour}, 67 {chrono.LocalTimeOf(12, 0, 0, 22), chrono.LocalTimeOf(12, 0, 0, 40), -18 * chrono.Nanosecond}, 68 } { 69 t.Run(fmt.Sprintf("%s - %s", tt.t1, tt.t2), func(t *testing.T) { 70 if d := tt.t1.Sub(tt.t2); d != tt.diff { 71 t.Errorf("t1.Sub(t2) = %v, want %v", d, tt.diff) 72 } 73 }) 74 } 75 } 76 77 func TestLocalTime_Add(t *testing.T) { 78 for _, tt := range []struct { 79 t chrono.LocalTime 80 e chrono.Extent 81 expected chrono.LocalTime 82 }{ 83 {chrono.LocalTimeOf(12, 0, 0, 0), 29 * chrono.Minute, chrono.LocalTimeOf(12, 29, 0, 0)}, 84 {chrono.LocalTimeOf(14, 45, 0, 0), -22 * chrono.Minute, chrono.LocalTimeOf(14, 23, 0, 0)}, 85 {chrono.LocalTimeOf(5, 0, 0, 0), -7 * chrono.Hour, chrono.LocalTimeOf(22, 0, 0, 0)}, 86 {chrono.LocalTimeOf(5, 0, 0, 0), -31 * chrono.Hour, chrono.LocalTimeOf(22, 0, 0, 0)}, 87 } { 88 t.Run(fmt.Sprintf("%s + %v", tt.t, tt.e), func(t *testing.T) { 89 if ok := tt.t.CanAdd(tt.e); !ok { 90 t.Error("t.CanAdd(e) = false, want true") 91 } 92 93 if added := tt.t.Add(tt.e); added.Compare(tt.expected) != 0 { 94 t.Errorf("t.Add(e) = %s, want %s", added, tt.expected) 95 } 96 }) 97 } 98 99 for _, tt := range []struct { 100 name string 101 t chrono.LocalTime 102 e chrono.Extent 103 }{ 104 {"invalid duration", chrono.LocalTimeOf(0, 0, 0, 0), 200 * chrono.Hour}, 105 {"invalid time", chrono.LocalTimeOf(90, 0, 0, 0), 20 * chrono.Hour}, 106 } { 107 t.Run(tt.name, func(t *testing.T) { 108 if ok := tt.t.CanAdd(tt.e); ok { 109 t.Error("t.CanAdd(e) = true, want false") 110 } 111 112 func() { 113 defer func() { 114 if r := recover(); r == nil { 115 t.Error("expecting panic that didn't occur") 116 } 117 }() 118 119 tt.t.Add(tt.e) 120 }() 121 }) 122 } 123 } 124 125 func TestLocalTime_Compare(t *testing.T) { 126 for _, tt := range []struct { 127 name string 128 t chrono.LocalTime 129 t2 chrono.LocalTime 130 expected int 131 }{ 132 {"earlier", chrono.LocalTimeOf(11, 0, 0, 0), chrono.LocalTimeOf(12, 0, 0, 0), -1}, 133 {"later", chrono.LocalTimeOf(13, 30, 0, 0), chrono.LocalTimeOf(13, 29, 55, 0), 1}, 134 {"equal", chrono.LocalTimeOf(15, 0, 0, 1000), chrono.LocalTimeOf(15, 0, 0, 1000), 0}, 135 } { 136 t.Run(tt.name, func(t *testing.T) { 137 if v := tt.t.Compare(tt.t2); v != tt.expected { 138 t.Errorf("t.Compare(t2) = %d, want %d", v, tt.expected) 139 } 140 }) 141 } 142 } 143 144 func TestLocalTime_In(t *testing.T) { 145 time := chrono.LocalTimeOf(9, 0, 0, 0) 146 output := time.In(chrono.OffsetOf(2, 30)) 147 148 expected := chrono.OffsetTimeOf(9, 0, 0, 0, 2, 30) 149 if output.Compare(expected) != 0 { 150 t.Errorf("time.In = %s, want %s", output, expected) 151 } 152 } 153 154 func TestLocalTime_UTC(t *testing.T) { 155 time := chrono.LocalTimeOf(9, 0, 0, 0) 156 output := time.UTC() 157 158 expected := chrono.OffsetTimeOf(9, 0, 0, 0, 0, 0) 159 if output.Compare(expected) != 0 { 160 t.Errorf("time.UTC() = %s, want %s", output, expected) 161 } 162 }