ifms_go/pkg/util/date.go
2025-06-10 17:50:46 +08:00

112 lines
3.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package util
import (
"fmt"
"time"
)
var (
cst *time.Location
)
// CSTLayout China Standard Time Layout
const CSTLayout = "2006-01-02 15:04:05"
// 微信有不同的返回的日期格式v1v2是20221108081846 ,v3是2020-11-08T08:18:46+08:00
const Date1Layout = "20060102150405"
func init() {
var err error
if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
panic(err)
}
}
type JsonShortTimeMonth time.Time
type JsonShortTime time.Time
type JsonLongTime time.Time
// 实现它的json序列化方法
func (this JsonShortTime) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format("2006-01-02")) // Format内即是你想转换的格式
return []byte(stamp), nil
}
func (this JsonLongTime) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format(CSTLayout)) // Format内即是你想转换的格式
return []byte(stamp), nil
}
func (this JsonShortTimeMonth) MarshalJSON() ([]byte, error) {
var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format("2006-01")) // Format内即是你想转换的格式
return []byte(stamp), nil
}
// 将微信的日期RFC3339标准 2020-11-08T08:18:46+08:00 转成标准日期 2020-11-08 08:18:46
func SimpleDate(value string) (string, error) {
ts, err := time.Parse(time.RFC3339, value)
if err != nil {
return GetCurrentLongDate(), err
}
return ts.In(cst).Format(Date1Layout), nil
}
func SimpleCSTDate(value string) (string, error) {
ts, err := time.Parse(time.RFC3339, value)
if err != nil {
return GetCurrentLongDate(), err
}
return ts.In(cst).Format(CSTLayout), nil
}
// 将微信的日期转成标准golang日期 , 微信的日期为yyyyMMddHHmmss格式
func ConvertWeiXinDateTime(wxdatetime string) time.Time {
t, _ := time.ParseInLocation("20060102150405", wxdatetime, cst)
return t
}
func ConvertStr2DateTimeWithDef(strdt string, def string) time.Time {
if strdt == "" {
strdt = def
}
local, _ := time.LoadLocation("Asia/Shanghai")
r, _ := time.ParseInLocation("2006-01-02 15:04:05", strdt, local)
return r
}
func GetMiniBizDate() time.Time {
strdt := "2000-01-01 00:00:00"
local, _ := time.LoadLocation("Asia/Shanghai")
r, _ := time.ParseInLocation("2006-01-02 15:04:05", strdt, local)
return r
}
func GetCurrentDate() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func GetMiniDateStr() string {
strdt := "2000-01-01 00:00:00"
local, _ := time.LoadLocation("Asia/Shanghai")
r, _ := time.ParseInLocation("2006-01-02 15:04:05", strdt, local)
return r.Format("2006-01-02")
}
func GetTodayStr() string {
t := time.Now()
addTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
return addTime.Format("2006-01-02")
}
func GetYesterdayStr() string {
ts := time.Now().AddDate(0, 0, -1)
timeYesterday := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location()).Unix()
return time.Unix(timeYesterday, 0).Format("2006-01-02")
}
func GetTomorrowStr() string {
ts := time.Now().AddDate(0, 0, 1)
timeYesterday := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location()).Unix()
return time.Unix(timeYesterday, 0).Format("2006-01-02")
}