Files
2026-05-20 16:13:58 +08:00

182 lines
4.1 KiB
Go

package utils
import (
"time"
"github.com/jinzhu/now"
)
const (
DateISO = "2006-01-02"
TimeISO = "2006-01-02 15:04:05"
)
var (
LocBJ, _ = time.LoadLocation("Asia/Shanghai")
)
func init() {
time.Local = LocBJ
}
func Now() time.Time {
return time.Now().Local()
}
func NowMillisecond() int64 {
return Now().UnixNano() / int64(time.Millisecond)
}
func BeginOfYesterday() time.Time {
t := Now().AddDate(0, 0, -1)
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func BeginOfYesterday0() time.Time {
t := Now().AddDate(0, 0, -2)
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func BeginOfToday() time.Time {
t := Now()
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func BeginOfHour() time.Time {
t := Now()
year, month, day := t.Date()
return time.Date(year, month, day, t.Hour(), 0, 0, 0, t.Location())
}
func EndOfToday() time.Time {
t := Now()
year, month, day := t.Date()
return time.Date(year, month, day, 23, 59, 59, 0, t.Location())
}
func BeginTodayMillsecond() int64 {
return BeginOfToday().UnixNano() / int64(time.Millisecond)
}
// ServerCurrMillisecond 获得时间毫秒数 13位
func ServerCurrMillisecond(t time.Time) int64 {
return t.UnixNano() / 1000000
}
func BeginWithTime(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func BeginMonthWithTime(t time.Time) time.Time {
year, month, _ := t.Date()
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
}
func BeginWithTimeLocal(t time.Time) time.Time {
localTime := t.Local()
year, month, day := localTime.Date()
return time.Date(year, month, day, 0, 0, 0, 0, time.Local)
}
func EndWithTime(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 23, 59, 59, 0, t.Location())
}
func IsToday(t time.Time) bool {
if t.IsZero() {
return false
}
today := Now()
return t.Year() == today.Year() &&
t.Month() == today.Month() &&
t.Day() == today.Day()
}
// BeginningOfTomorrow 明天的开始
func BeginningOfTomorrow(t time.Time) time.Time {
return BeginningOfDay(t).AddDate(0, 0, 1)
}
// BeginningOfDay 一天的开始
func BeginningOfDay(t time.Time) time.Time {
return now.New(t).BeginningOfDay()
}
// StrTimeToTime 时间字符串转时间
func StrTimeToTime(strTime string) (t time.Time) {
timeLayout := "2006-01-02T15:04:05-07:00"
loc, _ := time.LoadLocation("Local")
t, _ = time.ParseInLocation(timeLayout, strTime, loc)
return
}
func RecentMinute(tim time.Time, scaleMinute int64) time.Time {
minute := int64(tim.Minute())
lave := minute % scaleMinute
formatMinute := minute - lave
return time.Date(tim.Year(), tim.Month(), tim.Day(), tim.Hour(), int(formatMinute), 0, 0, tim.Location())
}
var (
GlobalShardHour = time.Hour
GlobalShardDay = 24 * time.Hour
GlobalShardWeek = 7 * 24 * time.Hour
GlobalShardMonth = 30 * 24 * time.Hour
DefaultStatBeginTime, _ = time.ParseInLocation("2006-01-02", "2021-03-01", time.Local)
)
type TimeSegment struct {
Begin time.Time
End time.Time
}
type TimeSegmentList []TimeSegment
func CalcTimeSegments(start, end time.Time, step time.Duration) TimeSegmentList {
ret := make(TimeSegmentList, 0)
if start.After(end) {
return ret
}
if start.Add(step).After(end) {
ret = append(ret, TimeSegment{
Begin: start,
End: end,
})
return ret
}
b := start
for b.Before(end) {
e := b.Add(step)
if e.After(end) {
e = end
}
ret = append(ret, TimeSegment{Begin: b, End: e})
b = b.Add(step)
}
return ret
}
func BeginOfMonth(t time.Time) time.Time {
year, month, _ := t.In(time.Local).Date()
return time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
}
func EndOfMonth(t time.Time) time.Time {
return BeginOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
}
func MonthDateRange(t time.Time) (string, string) {
b := BeginOfMonth(t)
e := EndOfMonth(t)
return GetDateString(b), GetDateString(e)
}
func GetDateString(t time.Time) string {
return t.In(LocBJ).Format(DateISO)
}