github.com/sandwich-go/boost@v1.3.29/xtime/beginning_end_test.go (about) 1 package xtime 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 var ( 9 format = "2006-01-02 15:04:05.999999999" 10 locationCaracas *time.Location 11 locationBerlin *time.Location 12 timeCaracas time.Time 13 ) 14 15 func init() { 16 var err error 17 if locationCaracas, err = time.LoadLocation("America/Caracas"); err != nil { 18 panic(err) 19 } 20 21 if locationBerlin, err = time.LoadLocation("Europe/Berlin"); err != nil { 22 panic(err) 23 } 24 25 timeCaracas = time.Date(2016, 1, 1, 12, 10, 0, 0, locationCaracas) 26 } 27 28 func assertT(t *testing.T) func(time.Time, string, string) { 29 return func(actual time.Time, expected string, msg string) { 30 actualStr := actual.Format(format) 31 if actualStr != expected { 32 t.Errorf("Failed %s: actual: %v, expected: %v", msg, actualStr, expected) 33 } 34 } 35 } 36 37 func TestBeginningOf(t *testing.T) { 38 assert := assertT(t) 39 40 n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC) 41 assert(BeginningOfMinute(n), "2013-11-18 17:51:00", "BeginningOfMinute") 42 43 WeekStartDay = time.Monday 44 assert(BeginningOfWeek(n), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday") 45 46 WeekStartDay = time.Tuesday 47 assert(BeginningOfWeek(n), "2013-11-12 00:00:00", "BeginningOfWeek, FirstDayTuesday") 48 49 WeekStartDay = time.Wednesday 50 assert(BeginningOfWeek(n), "2013-11-13 00:00:00", "BeginningOfWeek, FirstDayWednesday") 51 52 WeekStartDay = time.Thursday 53 assert(BeginningOfWeek(n), "2013-11-14 00:00:00", "BeginningOfWeek, FirstDayThursday") 54 55 WeekStartDay = time.Friday 56 assert(BeginningOfWeek(n), "2013-11-15 00:00:00", "BeginningOfWeek, FirstDayFriday") 57 58 WeekStartDay = time.Saturday 59 assert(BeginningOfWeek(n), "2013-11-16 00:00:00", "BeginningOfWeek, FirstDaySaturday") 60 61 WeekStartDay = time.Sunday 62 assert(BeginningOfWeek(n), "2013-11-17 00:00:00", "BeginningOfWeek, FirstDaySunday") 63 64 assert(BeginningOfHour(n), "2013-11-18 17:00:00", "BeginningOfHour") 65 66 assert(BeginningOfHour(timeCaracas), "2016-01-01 12:00:00", "BeginningOfHour Caracas") 67 68 assert(BeginningOfDay(n), "2013-11-18 00:00:00", "BeginningOfDay") 69 70 location, err := time.LoadLocation("Japan") 71 if err != nil { 72 t.Fatalf("Error loading location: %v", err) 73 } 74 beginningOfDay := time.Date(2015, 05, 01, 0, 0, 0, 0, location) 75 assert(BeginningOfDay(beginningOfDay), "2015-05-01 00:00:00", "BeginningOfDay") 76 77 // DST 78 dstBeginningOfDay := time.Date(2017, 10, 29, 10, 0, 0, 0, locationBerlin) 79 assert(BeginningOfDay(dstBeginningOfDay), "2017-10-29 00:00:00", "BeginningOfDay DST") 80 81 assert(BeginningOfWeek(n), "2013-11-17 00:00:00", "BeginningOfWeek") 82 83 dstBegginingOfWeek := time.Date(2017, 10, 30, 12, 0, 0, 0, locationBerlin) 84 assert(BeginningOfWeek(dstBegginingOfWeek), "2017-10-29 00:00:00", "BeginningOfWeek") 85 86 dstBegginingOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin) 87 assert(BeginningOfWeek(dstBegginingOfWeek), "2017-10-29 00:00:00", "BeginningOfWeek") 88 89 WeekStartDay = time.Monday 90 assert(BeginningOfWeek(n), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday") 91 dstBegginingOfWeek = time.Date(2017, 10, 24, 12, 0, 0, 0, locationBerlin) 92 assert(BeginningOfWeek(dstBegginingOfWeek), "2017-10-23 00:00:00", "BeginningOfWeek, FirstDayMonday") 93 94 dstBegginingOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin) 95 assert(BeginningOfWeek(dstBegginingOfWeek), "2017-10-23 00:00:00", "BeginningOfWeek, FirstDayMonday") 96 97 WeekStartDay = time.Sunday 98 99 assert(BeginningOfMonth(n), "2013-11-01 00:00:00", "BeginningOfMonth") 100 101 // DST 102 dstBeginningOfMonth := time.Date(2017, 10, 31, 0, 0, 0, 0, locationBerlin) 103 assert(BeginningOfMonth(dstBeginningOfMonth), "2017-10-01 00:00:00", "BeginningOfMonth DST") 104 105 assert(BeginningOfYear(timeCaracas), "2016-01-01 00:00:00", "BeginningOfYear Caracas") 106 } 107 108 func TestEndOf(t *testing.T) { 109 assert := assertT(t) 110 111 n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC) 112 113 assert(EndOfMinute(n), "2013-11-18 17:51:59.999999999", "EndOfMinute") 114 115 assert(EndOfHour(n), "2013-11-18 17:59:59.999999999", "EndOfHour") 116 117 assert(EndOfHour(timeCaracas), "2016-01-01 12:59:59.999999999", "EndOfHour Caracas") 118 119 assert(EndOfDay(n), "2013-11-18 23:59:59.999999999", "EndOfDay") 120 121 dstEndOfDay := time.Date(2017, 10, 29, 1, 0, 0, 0, locationBerlin) 122 assert(EndOfDay(dstEndOfDay), "2017-10-29 23:59:59.999999999", "EndOfDay DST") 123 124 WeekStartDay = time.Tuesday 125 assert(EndOfWeek(n), "2013-11-18 23:59:59.999999999", "EndOfWeek, FirstDayTuesday") 126 127 WeekStartDay = time.Wednesday 128 assert(EndOfWeek(n), "2013-11-19 23:59:59.999999999", "EndOfWeek, FirstDayWednesday") 129 130 WeekStartDay = time.Thursday 131 assert(EndOfWeek(n), "2013-11-20 23:59:59.999999999", "EndOfWeek, FirstDayThursday") 132 133 WeekStartDay = time.Friday 134 assert(EndOfWeek(n), "2013-11-21 23:59:59.999999999", "EndOfWeek, FirstDayFriday") 135 136 WeekStartDay = time.Saturday 137 assert(EndOfWeek(n), "2013-11-22 23:59:59.999999999", "EndOfWeek, FirstDaySaturday") 138 139 WeekStartDay = time.Sunday 140 assert(EndOfWeek(n), "2013-11-23 23:59:59.999999999", "EndOfWeek, FirstDaySunday") 141 142 WeekStartDay = time.Monday 143 assert(EndOfWeek(n), "2013-11-24 23:59:59.999999999", "EndOfWeek, FirstDayMonday") 144 145 dstEndOfWeek := time.Date(2017, 10, 24, 12, 0, 0, 0, locationBerlin) 146 assert(EndOfWeek(dstEndOfWeek), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday") 147 148 dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin) 149 assert(EndOfWeek(dstEndOfWeek), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday") 150 151 WeekStartDay = time.Sunday 152 assert(EndOfWeek(n), "2013-11-23 23:59:59.999999999", "EndOfWeek") 153 154 dstEndOfWeek = time.Date(2017, 10, 29, 0, 0, 0, 0, locationBerlin) 155 assert(EndOfWeek(dstEndOfWeek), "2017-11-04 23:59:59.999999999", "EndOfWeek") 156 157 dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin) 158 assert(EndOfWeek(dstEndOfWeek), "2017-11-04 23:59:59.999999999", "EndOfWeek") 159 160 assert(EndOfMonth(n), "2013-11-30 23:59:59.999999999", "EndOfMonth") 161 162 assert(EndOfYear(n), "2013-12-31 23:59:59.999999999", "EndOfYear") 163 164 n1 := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC) 165 assert(EndOfMonth(n1), "2013-02-28 23:59:59.999999999", "EndOfMonth for 2013/02") 166 167 n2 := time.Date(1900, 02, 18, 17, 51, 49, 123456789, time.UTC) 168 assert(EndOfMonth(n2), "1900-02-28 23:59:59.999999999", "EndOfMonth") 169 } 170 171 func TestMondayAndSunday(t *testing.T) { 172 assert := assertT(t) 173 174 n := time.Date(2013, 11, 19, 17, 51, 49, 123456789, time.UTC) 175 n2 := time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.UTC) 176 nDst := time.Date(2017, 10, 29, 10, 0, 0, 0, locationBerlin) 177 178 assert(Monday(n), "2013-11-18 00:00:00", "Monday") 179 180 assert(Monday(n2), "2013-11-18 00:00:00", "Monday") 181 182 assert(Monday(timeCaracas), "2015-12-28 00:00:00", "Monday Caracas") 183 184 assert(Monday(nDst), "2017-10-23 00:00:00", "Monday DST") 185 186 assert(Sunday(n), "2013-11-24 00:00:00", "Sunday") 187 188 assert(Sunday(n2), "2013-11-24 00:00:00", "Sunday") 189 190 assert(Sunday(timeCaracas), "2016-01-03 00:00:00", "Sunday Caracas") 191 192 assert(Sunday(nDst), "2017-10-29 00:00:00", "Sunday DST") 193 194 assert(BeginningOfWeek(n), "2013-11-17 00:00:00", "BeginningOfWeek, FirstDayMonday") 195 WeekStartDay = time.Monday 196 assert(BeginningOfWeek(n), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday") 197 } 198 199 func TestSameDay(t *testing.T) { 200 n := time.Date(2013, 11, 19, 17, 51, 49, 123456789, time.UTC) 201 if !IsToday(BeginningOfDay(n).Unix(), n) { 202 t.Errorf("Failed IsToday: actual: false, expected: true") 203 } 204 if !IsTomorrow(EndOfDay(n).Unix()+10, n) { 205 t.Errorf("Failed IsTomorrow: actual: false, expected: true") 206 } 207 if !IsYesterday(BeginningOfDay(n).Unix()-10, n) { 208 t.Errorf("Failed IsYesterday: actual: false, expected: true") 209 } 210 if !IsThisWeek(BeginningOfDay(n).Unix(), n) { 211 t.Errorf("Failed IsThisWeek: actual: false, expected: true") 212 } 213 { 214 v := SubDays(n.Unix(), n.Unix()+10) 215 if v != 0 { 216 t.Errorf("Failed SubDays: actual: %d, expected: 0", v) 217 } 218 } 219 { 220 v := SubDays(n.Unix(), n.Unix()+24*60*60) 221 if v != 1 { 222 t.Errorf("Failed SubDays: actual: %d, expected: 1", v) 223 } 224 } 225 { 226 v := DaysInMonth(n) 227 if DaysInMonth(n) != 30 { 228 t.Errorf("Failed DaysInMonth: actual: %d, expected: 30", v) 229 } 230 } 231 }