github.com/codingeasygo/util@v0.0.0-20231206062002-1ce2f004b7d9/xtime/xtime.go (about) 1 package xtime 2 3 import "time" 4 5 func Now() int64 { 6 return time.Now().Local().UnixNano() / 1e6 7 } 8 9 // TimeStartOfToday return 00:00:00 of today 10 func TimeStartOfToday() time.Time { 11 now := time.Now() 12 return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) 13 } 14 15 // TimeStartOfWeek return 00:00:00 of week 16 func TimeStartOfWeek() time.Time { 17 now := time.Now() 18 today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) 19 start := today.Add(-24 * time.Hour * time.Duration(today.Weekday())) 20 return start 21 } 22 23 // TimeStartOfMonth return 00:00:00 of month 24 func TimeStartOfMonth() time.Time { 25 now := time.Now() 26 return time.Date(now.Year(), now.Month(), 0, 0, 0, 0, 0, now.Location()).Add(24 * time.Hour) 27 } 28 29 func Timestamp(t time.Time) int64 { 30 return t.Local().UnixNano() / 1e6 31 } 32 33 // TimeUnix will return time by timestamp 34 func TimeUnix(timestamp int64) time.Time { 35 return time.Unix(0, timestamp*1e6) 36 } 37 38 func TimeNow() int64 { 39 return Timestamp(time.Now()) 40 }