github.com/sandwich-go/boost@v1.3.29/xtime/beginning_end.go (about) 1 package xtime 2 3 import ( 4 "time" 5 ) 6 7 // WeekStartDay set week start day, default is sunday 8 var WeekStartDay = time.Sunday 9 10 // ToLocal to local time 11 func ToLocal(tt time.Time, offset int) time.Time { 12 return tt.In(time.FixedZone("", offset)) 13 } 14 15 // BeginningOfMinute beginning of minute 16 func BeginningOfMinute(tt time.Time) time.Time { 17 return tt.Truncate(time.Minute) 18 } 19 20 // BeginningOfHour beginning of hour 21 func BeginningOfHour(tt time.Time) time.Time { 22 return BeginningOfHourSpec(tt, tt.Hour()) 23 } 24 25 // BeginningOfHourSpec beginning of specified hour 26 func BeginningOfHourSpec(tt time.Time, hourSpec int) time.Time { 27 y, m, d := tt.Date() 28 return time.Date(y, m, d, hourSpec, 0, 0, 0, tt.Location()) 29 } 30 31 // BeginningOfDay beginning of day 32 func BeginningOfDay(tt time.Time) time.Time { 33 y, m, d := tt.Date() 34 return time.Date(y, m, d, 0, 0, 0, 0, tt.Location()) 35 } 36 37 // BeginningOfWeek beginning of week 38 func BeginningOfWeek(tt time.Time) time.Time { 39 t := BeginningOfDay(tt) 40 weekday := int(t.Weekday()) 41 42 if WeekStartDay != time.Sunday { 43 weekStartDayInt := int(WeekStartDay) 44 if weekday < weekStartDayInt { 45 weekday = weekday + 7 - weekStartDayInt 46 } else { 47 weekday = weekday - weekStartDayInt 48 } 49 } 50 return t.AddDate(0, 0, -weekday) 51 } 52 53 // BeginningOfMonth beginning of month 54 func BeginningOfMonth(tt time.Time) time.Time { 55 y, m, _ := tt.Date() 56 return time.Date(y, m, 1, 0, 0, 0, 0, tt.Location()) 57 } 58 59 // BeginningOfYear beginning of year 60 func BeginningOfYear(tt time.Time) time.Time { 61 y, _, _ := tt.Date() 62 return time.Date(y, time.January, 1, 0, 0, 0, 0, tt.Location()) 63 } 64 65 // EndOfMinute end of minute 66 func EndOfMinute(tt time.Time) time.Time { 67 return BeginningOfMinute(tt).Add(time.Minute - time.Nanosecond) 68 } 69 70 // EndOfHour end of hour 71 func EndOfHour(tt time.Time) time.Time { 72 return BeginningOfHour(tt).Add(time.Hour - time.Nanosecond) 73 } 74 75 // EndOfDay end of day 76 func EndOfDay(tt time.Time) time.Time { 77 y, m, d := tt.Date() 78 return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), tt.Location()) 79 } 80 81 // EndOfWeek end of week 82 func EndOfWeek(tt time.Time) time.Time { 83 return BeginningOfWeek(tt).AddDate(0, 0, 7).Add(-time.Nanosecond) 84 } 85 86 // EndOfMonth end of month 87 func EndOfMonth(tt time.Time) time.Time { 88 return BeginningOfMonth(tt).AddDate(0, 1, 0).Add(-time.Nanosecond) 89 } 90 91 // EndOfYear end of year 92 func EndOfYear(tt time.Time) time.Time { 93 return BeginningOfYear(tt).AddDate(1, 0, 0).Add(-time.Nanosecond) 94 } 95 96 // Monday the monday of tt 97 func Monday(tt time.Time) time.Time { 98 t := BeginningOfDay(tt) 99 weekday := int(t.Weekday()) 100 if weekday == 0 { 101 weekday = 7 102 } 103 return t.AddDate(0, 0, -weekday+1) 104 } 105 106 // Sunday the sunday of tt 107 func Sunday(tt time.Time) time.Time { 108 t := BeginningOfDay(tt) 109 weekday := int(t.Weekday()) 110 if weekday == 0 { 111 return t 112 } 113 return t.AddDate(0, 0, 7-weekday) 114 } 115 116 // DaysInMonth days of current month 117 func DaysInMonth(tt time.Time) int { 118 switch tt.Month() { 119 case time.April, time.June, time.September, time.November: 120 return 30 121 case time.February: 122 if IsLeapYear(tt.Year()) { 123 return 29 124 } 125 return 28 126 default: 127 return 31 128 } 129 } 130 131 // IsLeapYear year is leap year 132 func IsLeapYear(year int) bool { 133 return year%4 == 0 && (year%100 != 0 || year%400 == 0) 134 } 135 136 // IsToday tt is today 137 func IsToday(ts int64, tt time.Time) bool { 138 y1, m1, d1 := time.Unix(ts, 0).In(tt.Location()).Date() 139 y2, m2, d2 := tt.Date() 140 return y1 == y2 && m1 == m2 && d1 == d2 141 } 142 143 // IsTomorrow tt is tomorrow 144 func IsTomorrow(ts int64, tt time.Time) bool { 145 return IsToday(ts, tt.AddDate(0, 0, 1)) 146 } 147 148 // IsYesterday tt is yesterday 149 func IsYesterday(ts int64, tt time.Time) bool { 150 return IsToday(ts, tt.AddDate(0, 0, -1)) 151 } 152 153 // IsThisWeek tt is current week 154 func IsThisWeek(ts int64, tt time.Time) (isThisWeek bool) { 155 y1, w1 := tt.ISOWeek() 156 y2, w2 := time.Unix(ts, 0).In(tt.Location()).ISOWeek() 157 return y1 == y2 && w1 == w2 158 } 159 160 // SubDays days of between start and end 161 func SubDays(start, end int64) int64 { 162 if start == end { 163 return 0 164 } 165 return int64(time.Unix(end, 0).Sub(time.Unix(start, 0)).Hours() / 24) 166 }