base-golang/pkg/time_parse/time_parse.go

125 lines
3.4 KiB
Go
Raw Normal View History

2024-07-23 10:23:43 +08:00
package time_parse
import (
"github.com/jinzhu/now"
"math"
"net/http"
"time"
)
var (
cst = time.Local
)
func SetPRCLocation() error {
var err error
if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
return err
}
return nil
}
func Now() *now.Now {
timeFormats := append(append(now.TimeFormats, time.DateTime), time.DateOnly)
c := &now.Config{
WeekStartDay: time.Monday,
TimeLocation: cst,
TimeFormats: timeFormats,
}
return c.With(time.Now().In(cst))
}
// RFC3339ToCSTLayout convert rfc3339 value to China standard time layout
// 2020-11-08T08:18:46+08:00 => 2020-11-08 08:18:46
func RFC3339ToCSTLayout(value string) (string, error) {
ts, err := time.Parse(time.RFC3339, value)
if err != nil {
return "", err
}
return ts.In(cst).Format(time.DateTime), nil
}
// GetMilliTimestamp 获取CST毫秒时间戳
func GetMilliTimestamp() int64 {
return time.Now().In(cst).UnixMilli()
}
// GetTimestamp 获取CST时间戳
func GetTimestamp() int64 {
return time.Now().In(cst).Unix()
}
// CSTLayoutString 格式化时间
// 返回 "2006-01-02 15:04:05" 格式的时间
func CSTLayoutString() string {
return time.Now().In(cst).Format(time.DateTime)
}
// ParseCSTInLocation 格式化时间
func ParseCSTInLocation(date string) (time.Time, error) {
return time.ParseInLocation(time.DateTime, date, cst)
}
// CSTLayoutStringToUnix 返回 unix 时间戳
// 2020-01-24 21:11:11 => 1579871471
func CSTLayoutStringToUnix(cstLayoutString string) (int64, error) {
stamp, err := time.ParseInLocation(time.DateTime, cstLayoutString, cst)
if err != nil {
return 0, err
}
return stamp.Unix(), nil
}
// GMTLayoutString 格式化时间
// 返回 "Mon, 02 Jan 2006 15:04:05 GMT" 格式的时间
func GMTLayoutString() string {
return time.Now().In(cst).Format(http.TimeFormat)
}
// ParseGMTInLocation 格式化时间
func ParseGMTInLocation(date string) (time.Time, error) {
return time.ParseInLocation(http.TimeFormat, date, cst)
}
// SubInLocation 计算时间差
func SubInLocation(ts time.Time) float64 {
return math.Abs(time.Now().In(cst).Sub(ts).Seconds())
}
// GetFirstDateOfMonth 获取传入的时间所在月份的第一天即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
func GetFirstDateOfMonth(d time.Time) time.Time {
d = d.AddDate(0, 0, -d.Day()+1)
return GetStartDayTime(d)
}
// GetLastDateOfMonth 获取传入的时间所在月份的最后一天即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
func GetLastDateOfMonth(d time.Time) time.Time {
return GetEndDayTime(GetFirstDateOfMonth(d).AddDate(0, 1, -1))
}
// GetStartDayTime 获取某一天的开始时间
func GetStartDayTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, cst)
}
// GetEndDayTime 获取某一天的结束时间
func GetEndDayTime(d time.Time) time.Time {
return GetStartDayTime(d).Add(24 * time.Hour).Add(-time.Second)
}
// GetStartYesterdayTime 获取昨天的起始时间
func GetStartYesterdayTime() time.Time {
d, _ := time.ParseDuration("-24h")
return GetStartDayTime(time.Now().Add(d))
}
// SubInLocationDays 计算两个日期相差的天数
func SubInLocationDays(t1, t2 time.Time) float64 {
t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, cst)
t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, cst)
return math.Abs(t1.Sub(t2).Hours() / 24)
}