github.heygears.com/openimsdk/tools@v0.0.49/utils/timeutil/time_format.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package timeutil 16 17 import ( 18 "strconv" 19 "time" 20 21 "github.com/openimsdk/tools/errs" 22 ) 23 24 const ( 25 TimeOffset = 8 * 3600 //8 hour offset 26 HalfOffset = 12 * 3600 //Half-day hourly offset 27 ) 28 29 // Get the current timestamp by Second 30 func GetCurrentTimestampBySecond() int64 { 31 return time.Now().Unix() 32 } 33 34 // Convert timestamp to time.Time type 35 func UnixSecondToTime(second int64) time.Time { 36 return time.Unix(second, 0) 37 } 38 39 // Convert nano timestamp to time.Time type 40 func UnixNanoSecondToTime(nanoSecond int64) time.Time { 41 return time.Unix(0, nanoSecond) 42 } 43 44 // UnixMillSecondToTime convert millSecond to time.Time type 45 func UnixMillSecondToTime(millSecond int64) time.Time { 46 return time.Unix(0, millSecond*1e6) 47 } 48 49 // Get the current timestamp by Nano 50 func GetCurrentTimestampByNano() int64 { 51 return time.Now().UnixNano() 52 } 53 54 // Get the current timestamp by Mill 55 func GetCurrentTimestampByMill() int64 { 56 return time.Now().UnixNano() / 1e6 57 } 58 59 // Get the timestamp at 0 o'clock of the day 60 func GetCurDayZeroTimestamp() int64 { 61 timeStr := time.Now().Format("2006-01-02") 62 t, _ := time.Parse("2006-01-02", timeStr) 63 return t.Unix() - TimeOffset 64 } 65 66 // Get the timestamp at 12 o'clock on the day 67 func GetCurDayHalfTimestamp() int64 { 68 return GetCurDayZeroTimestamp() + HalfOffset 69 70 } 71 72 // Get the formatted time at 0 o'clock of the day, the format is "2006-01-02_00-00-00" 73 func GetCurDayZeroTimeFormat() string { 74 return time.Unix(GetCurDayZeroTimestamp(), 0).Format("2006-01-02_15-04-05") 75 } 76 77 // Get the formatted time at 12 o'clock of the day, the format is "2006-01-02_12-00-00" 78 func GetCurDayHalfTimeFormat() string { 79 return time.Unix(GetCurDayZeroTimestamp()+HalfOffset, 0).Format("2006-01-02_15-04-05") 80 } 81 82 // GetTimeStampByFormat convert string to unix timestamp 83 func GetTimeStampByFormat(datetime string) string { 84 timeLayout := "2006-01-02 15:04:05" 85 loc, _ := time.LoadLocation("Local") 86 tmp, _ := time.ParseInLocation(timeLayout, datetime, loc) 87 timestamp := tmp.Unix() 88 return strconv.FormatInt(timestamp, 10) 89 } 90 91 // TimeStringFormatTimeUnix convert string to unix timestamp 92 func TimeStringFormatTimeUnix(timeFormat string, timeSrc string) int64 { 93 tm, _ := time.Parse(timeFormat, timeSrc) 94 return tm.Unix() 95 } 96 97 // TimeStringToTime convert string to time.Time 98 func TimeStringToTime(timeString string) (time.Time, error) { 99 t, err := time.Parse("2006-01-02", timeString) 100 return t, errs.WrapMsg(err, "timeStringToTime failed", "timeString", timeString) 101 } 102 103 // TimeToString convert time.Time to string 104 func TimeToString(t time.Time) string { 105 return t.Format("2006-01-02") 106 } 107 108 func GetCurrentTimeFormatted() string { 109 return time.Now().Format("2006-01-02 15:04:05") 110 } 111 112 // GetTimestampByTimezone get specific timestamp by timezone 113 func GetTimestampByTimezone(timezone string) (int64, error) { 114 // set time zone 115 location, err := time.LoadLocation(timezone) 116 if err != nil { 117 return 0, errs.New("error loading location:", "error:", err) 118 } 119 // get current time 120 currentTime := time.Now().In(location) 121 // get timestamp 122 timestamp := currentTime.Unix() 123 return timestamp, nil 124 } 125 126 func DaysBetweenTimestamps(timezone string, timestamp int64) (int, error) { 127 // set time zone 128 location, err := time.LoadLocation(timezone) 129 if err != nil { 130 return 0, errs.New("error loading location:", "error:", err) 131 } 132 // get current time 133 now := time.Now().In(location) 134 // timestamp to time 135 givenTime := time.Unix(timestamp, 0) 136 // calculate duration 137 duration := now.Sub(givenTime) 138 // change to days 139 days := int(duration.Hours() / 24) 140 return days, nil 141 } 142 143 // IsSameWeekday judge current day and specific day is the same of a week. 144 func IsSameWeekday(timezone string, timestamp int64) (bool, error) { 145 // set time zone 146 location, err := time.LoadLocation(timezone) 147 if err != nil { 148 return false, errs.New("error loading location:", "error:", err) 149 } 150 // get current weekday 151 currentWeekday := time.Now().In(location).Weekday() 152 // change timestamp to weekday 153 givenTime := time.Unix(timestamp, 0) 154 givenWeekday := givenTime.Weekday() 155 // compare two days 156 return currentWeekday == givenWeekday, nil 157 } 158 159 func IsSameDayOfMonth(timezone string, timestamp int64) (bool, error) { 160 // set time zone 161 location, err := time.LoadLocation(timezone) 162 if err != nil { 163 return false, errs.New("error loading location:", "error:", err) 164 } 165 // Get the current day of the month 166 currentDay := time.Now().In(location).Day() 167 // Convert the timestamp to time and get the day of the month 168 givenDay := time.Unix(timestamp, 0).Day() 169 // Compare the days 170 return currentDay == givenDay, nil 171 } 172 173 func IsWeekday(timestamp int64) bool { 174 // Convert the timestamp to time 175 givenTime := time.Unix(timestamp, 0) 176 // Get the day of the week 177 weekday := givenTime.Weekday() 178 // Check if the day is between Monday (1) and Friday (5) 179 return weekday >= time.Monday && weekday <= time.Friday 180 } 181 182 func IsNthDayCycle(timezone string, startTimestamp int64, n int) (bool, error) { 183 // set time zone 184 location, err := time.LoadLocation(timezone) 185 if err != nil { 186 return false, errs.New("error loading location:", "error:", err) 187 } 188 // Parse the start date 189 startTime := time.Unix(startTimestamp, 0) 190 if err != nil { 191 return false, errs.New("invalid start date format:", "error:", err) 192 } 193 // Get the current time 194 now := time.Now().In(location) 195 // Calculate the difference in days between the current time and the start time 196 diff := now.Sub(startTime).Hours() / 24 197 // Check if the difference in days is a multiple of n 198 return int(diff)%n == 0, nil 199 } 200 201 // IsNthWeekCycle checks if the current day is part of an N-week cycle starting from a given start timestamp. 202 func IsNthWeekCycle(timezone string, startTimestamp int64, n int) (bool, error) { 203 // set time zone 204 location, err := time.LoadLocation(timezone) 205 if err != nil { 206 return false, errs.New("error loading location:", "error:", err) 207 } 208 209 // Get the current time 210 now := time.Now().In(location) 211 212 // Parse the start timestamp 213 startTime := time.Unix(startTimestamp, 0) 214 if err != nil { 215 return false, errs.New("invalid start timestamp format:", "error:", err) 216 } 217 218 // Calculate the difference in days between the current time and the start time 219 diff := now.Sub(startTime).Hours() / 24 220 221 // Convert days to weeks 222 weeks := int(diff) / 7 223 224 // Check if the difference in weeks is a multiple of n 225 return weeks%n == 0, nil 226 } 227 228 // IsNthMonthCycle checks if the current day is part of an N-month cycle starting from a given start timestamp. 229 func IsNthMonthCycle(timezone string, startTimestamp int64, n int) (bool, error) { 230 // set time zone 231 location, err := time.LoadLocation(timezone) 232 if err != nil { 233 return false, errs.New("error loading location:", "error:", err) 234 } 235 236 // Get the current date 237 now := time.Now().In(location) 238 239 // Parse the start timestamp 240 startTime := time.Unix(startTimestamp, 0) 241 if err != nil { 242 return false, errs.New("invalid start timestamp format:", "error:", err) 243 } 244 245 // Calculate the difference in months between the current time and the start time 246 yearsDiff := now.Year() - startTime.Year() 247 monthsDiff := int(now.Month()) - int(startTime.Month()) 248 totalMonths := yearsDiff*12 + monthsDiff 249 250 // Check if the difference in months is a multiple of n 251 return totalMonths%n == 0, nil 252 }