github.com/godaddy-x/freego@v1.0.156/utils/time.go (about) 1 package utils 2 3 import ( 4 "time" 5 _ "unsafe" 6 ) 7 8 //go:linkname now time.now 9 func now() (sec int64, nsec int32, mono int64) 10 11 // UnixNano 直接调用底层方法比time.Now()性能提升1倍 12 func UnixNano() int64 { 13 s, m, _ := now() 14 return s*1e9 + int64(m) 15 } 16 17 // 获取当前时间/秒 18 func UnixSecond() int64 { 19 s, _, _ := now() 20 return s 21 } 22 23 // 获取当前时间/毫秒 24 func UnixMilli() int64 { 25 return UnixNano() / 1e6 26 } 27 28 // 时间戳转time 29 func Int2Time(t int64) time.Time { 30 return time.Unix(0, t*1e6) 31 } 32 33 // 时间戳转格式字符串/毫秒, 例: 2023-07-22 08:47:27.379 34 func Time2Str(t int64) string { 35 return Int2Time(t).In(cst_sh).Format(time_fmt) 36 } 37 38 // 时间戳转格式字符串/毫秒 39 func Time2DateStr(t int64) string { 40 return Int2Time(t).In(cst_sh).Format(date_fmt) 41 } 42 43 // 格式字符串转时间戳/毫秒 44 // 2023-07-22 08:47:27.379 45 // 2023-07-22 08:47:27 46 // 2023-07-22 47 func Str2Time(s string) (int64, error) { 48 if len(s) == 10 { 49 s += " 00:00:00.000" 50 } else if len(s) == 19 { 51 s += ".000" 52 } 53 t, err := time.ParseInLocation(time_fmt, s, cst_sh) 54 if err != nil { 55 return 0, err 56 } 57 return t.UnixMilli(), nil 58 } 59 60 // 格式字符串转时间戳/毫秒 61 func Str2Date(s string) (int64, error) { 62 t, err := time.ParseInLocation(date_fmt, s, cst_sh) 63 if err != nil { 64 return 0, err 65 } 66 return t.UnixMilli(), nil 67 } 68 69 // 获取当前月份开始和结束时间 70 func GetMonthFirstAndLast() (int64, int64) { 71 now := time.Now() 72 currentYear, currentMonth, _ := now.Date() 73 firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, now.Location()) 74 lastOfMonth := firstOfMonth.AddDate(0, 1, -1) 75 return firstOfMonth.UnixMilli(), lastOfMonth.UnixMilli() + OneDay 76 } 77 78 // 获取指定月份开始和结束时间 79 func GetAnyMonthFirstAndLast(month int) (int64, int64) { 80 now := time.Now() 81 currentYear, currentMonth, _ := now.Date() 82 cmonth := int(currentMonth) 83 offset := month - cmonth 84 if month < 1 || month > 12 { 85 offset = 0 86 } 87 firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, now.Location()).AddDate(0, offset, 0) 88 lastOfMonth := firstOfMonth.AddDate(0, 1, -1) 89 return firstOfMonth.UnixMilli(), lastOfMonth.UnixMilli() + OneDay 90 } 91 92 // 获取当前星期开始和结束时间 93 func GetWeekFirstAndLast() (int64, int64) { 94 now := time.Now() 95 offset := int(time.Monday - now.Weekday()) 96 if offset > 0 { 97 offset = -6 98 } 99 start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, offset) 100 first := start.UnixMilli() 101 return first, first + OneWeek 102 } 103 104 // 获取当天开始和结束时间 105 func GetDayFirstAndLast() (int64, int64) { 106 now := time.Now() 107 start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) 108 first := start.UnixMilli() 109 return first, first + OneDay 110 } 111 112 // 获取x天开始和结束时间,最多30天 113 func GetAnyDayFirstAndLast(x int64) (int64, int64) { 114 if x < 0 { 115 x = 0 116 } 117 if x > 30 { 118 x = 30 119 } 120 now := time.Now() 121 start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) 122 first := start.UnixMilli() 123 before := x * OneDay 124 return first - before, first + OneDay - before 125 } 126 127 // 获取x天开始和当天结束时间,最多30天 128 func GetInDayFirstAndLast(x int64) (int64, int64) { 129 if x < 0 { 130 x = 0 131 } 132 if x > 30 { 133 x = 30 134 } 135 now := time.Now() 136 start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) 137 first := start.UnixMilli() 138 before := x * OneDay 139 return first - before, first + OneDay 140 } 141 142 // 获取时间的0点 143 func GetFmtDate(t int64) int64 { 144 now := Int2Time(t) 145 start := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) 146 first := start.UnixMilli() 147 return first 148 }