提交 790ed5c9 authored 作者: mooncake's avatar mooncake

update

上级 872999fc
......@@ -24,6 +24,7 @@ const (
Layout_YYYYMMDD = "2006-01-02"
Layout_YYYYMMDD2 = "2006/01/02"
Layout_YYYYMMDD3 = "20060102"
Layout_YYYYMMDD4 = "2006年01月02日"
Layout_YYYYMMDDHHmmSS = "2006-01-02 15:04:05"
)
......@@ -44,7 +45,7 @@ const (
// MarshalJSON 自定义JSON序列化
func (dt DateTime) MarshalJSON() ([]byte, error) {
t := time.Time(dt)
t := dt.Time()
if t.IsZero() {
return []byte("null"), nil
}
......@@ -71,8 +72,8 @@ func (dt *DateTime) UnmarshalJSON(data []byte) error {
}
// MarshalYAML 自定义YAML序列化
func (dt DateTime) MarshalYAML() (interface{}, error) {
t := time.Time(dt)
func (dt DateTime) MarshalYAML() (any, error) {
t := dt.Time()
if t.IsZero() {
return nil, nil
}
......@@ -96,7 +97,7 @@ func (dt *DateTime) UnmarshalYAML(value *yaml.Node) error {
// String 返回日期时间的字符串表示
func (dt DateTime) String() string {
t := time.Time(dt)
t := dt.Time()
if t.IsZero() {
return ""
}
......@@ -123,7 +124,7 @@ func (dt *DateTime) parseString(s string) error {
var firstErr error
for _, format := range formats {
parsed, err := time.Parse(format, s)
parsed, err := time.ParseInLocation(format, s, LocBeiJing)
if err == nil {
*dt = DateTime(parsed)
return nil
......@@ -143,7 +144,7 @@ func (dt DateTime) Time() time.Time {
// IsZero 检查是否为零值
func (dt DateTime) IsZero() bool {
return time.Time(dt).IsZero()
return dt.Time().IsZero()
}
// IsNotZero 检查是否非零值
......@@ -153,12 +154,12 @@ func (dt DateTime) IsNotZero() bool {
// Format 使用自定义格式格式化日期时间
func (dt DateTime) Format(layout string) string {
return time.Time(dt).Format(layout)
return dt.Time().Format(layout)
}
// Value 实现 driver.Valuer 接口,用于数据库存储
func (dt DateTime) Value() (driver.Value, error) {
t := time.Time(dt)
t := dt.Time()
if t.IsZero() {
return nil, nil
}
......@@ -166,7 +167,7 @@ func (dt DateTime) Value() (driver.Value, error) {
}
// Scan 实现 sql.Scanner 接口,用于从数据库读取
func (dt *DateTime) Scan(value interface{}) error {
func (dt *DateTime) Scan(value any) error {
if value == nil {
*dt = DateTime(time.Time{})
return nil
......@@ -195,32 +196,32 @@ func (dt *DateTime) Scan(value interface{}) error {
// After 检查当前时间是否在另一个时间之后
func (dt DateTime) After(other DateTime) bool {
return time.Time(dt).After(time.Time(other))
return dt.Time().After(other.Time())
}
// Before 检查当前时间是否在另一个时间之前
func (dt DateTime) Before(other DateTime) bool {
return time.Time(dt).Before(time.Time(other))
return dt.Time().Before(other.Time())
}
// Equal 检查两个时间是否相等
func (dt DateTime) Equal(other DateTime) bool {
return time.Time(dt).Equal(time.Time(other))
return dt.Time().Equal(other.Time())
}
// Add 添加时间间隔
func (dt DateTime) Add(duration time.Duration) DateTime {
return DateTime(time.Time(dt).Add(duration))
return DateTime(dt.Time().Add(duration))
}
// Unix 返回Unix时间戳
func (dt DateTime) Unix() int64 {
return time.Time(dt).Unix()
return dt.Time().Unix()
}
// UnixNano 返回纳秒级Unix时间戳
func (dt DateTime) UnixNano() int64 {
return time.Time(dt).UnixNano()
return dt.Time().UnixNano()
}
// 构造函数
......@@ -373,7 +374,7 @@ func (t0 DateTime) MonthEnd() DateTime {
// AddDate 添加年、月、日,正确处理月份边界溢出
func (dt DateTime) AddDate(years, months, days int) DateTime {
t := time.Time(dt)
t := dt.Time()
y, m, d := t.Date()
h, min, s := t.Clock()
nsec := t.Nanosecond()
......@@ -530,27 +531,27 @@ func DaysBetween(t0, t1 DateTime) int {
// 时间组件获取方法
func (dt DateTime) Hour() int {
return time.Time(dt).Hour()
return dt.Time().Hour()
}
func (dt DateTime) Minute() int {
return time.Time(dt).Minute()
return dt.Time().Minute()
}
func (dt DateTime) Second() int {
return time.Time(dt).Second()
return dt.Time().Second()
}
func (dt DateTime) Year() int {
return time.Time(dt).Year()
return dt.Time().Year()
}
// func (dt DateTime) Month() time.Month {
// return time.Time(dt).Month()
// return dt.Time().Month()
// }
func (dt DateTime) Day() int {
return time.Time(dt).Day()
return dt.Time().Day()
}
// 月份常量(Go语言time包内置,直接使用)
......@@ -561,7 +562,7 @@ func (dt DateTime) Day() int {
// MarshalCSV 实现CSV序列化方法
func (d DateTime) MarshalCSV() (string, error) {
t := time.Time(d)
t := d.Time()
if t.IsZero() {
return "", nil
}
......
......@@ -215,6 +215,10 @@ func (t Time) Second() int {
return t.Time().Second()
}
func (t Time) Nanosecond() int {
return t.Time().Nanosecond()
}
// Before 检查当前时间是否在另一个时间之前
func (t Time) Before(other Time) bool {
return t.Time().Before(other.Time())
......@@ -305,3 +309,15 @@ func (t *Time) UnmarshalCSV(csv string) error {
return fmt.Errorf("无法解析时间格式: %s", csv)
}
func (x *Time) ToDuration() time.Duration {
hours := x.Hour()
minutes := x.Minute()
seconds := x.Second()
nanoseconds := x.Nanosecond()
return time.Duration(hours)*time.Hour +
time.Duration(minutes)*time.Minute +
time.Duration(seconds)*time.Second +
time.Duration(nanoseconds)
}
package sliceutils
import (
"fmt"
"strings"
"gitlab.wanzhuangkj.com/tush/opkg/pkg/errcode"
"gitlab.wanzhuangkj.com/tush/opkg/pkg/sf"
"gitlab.wanzhuangkj.com/tush/opkg/pkg/xset"
"github.com/jinzhu/copier"
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/opkg/pkg/xerrors/xerror"
)
// connector 连接符
......@@ -236,28 +233,6 @@ type ICode interface {
Code() int
}
func CompareSlice[ID sf.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice, errCode error) error {
if len(ids) == 0 {
return nil
}
if len(bizSlice) < len(ids) {
if len(bizSlice) == 0 {
if v, ok := errCode.(*errcode.Error); ok {
return xerror.NewC(v.Code(), v.Msg())
}
return xerror.New(errCode.Error())
}
deltaIDs := SetDifference(ids, GetIDs[ID](bizSlice))
if len(deltaIDs) > 0 {
if v, ok := errCode.(*errcode.Error); ok {
return xerror.NewC(v.Code(), fmt.Sprintf("%s[ids:%s]", v.Msg(), Join(deltaIDs, ",")))
}
return xerror.New(errCode.Error())
}
}
return nil
}
func ArrayUnique[T comparable](array []T) []T {
mp := make(map[T]struct{})
idx := 0
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论