mirror of
https://github.com/quarkcloudio/quark-go.git
synced 2025-09-26 20:11:11 +08:00
chore: 优化时间格式化
This commit is contained in:
@@ -38,17 +38,17 @@ func (t *Date) UnmarshalJSON(data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var now time.Time
|
||||
var now Date
|
||||
|
||||
// 自定义格式解析
|
||||
if now, err = time.ParseInLocation("2006-01-02", string(data), time.Local); err == nil {
|
||||
*t = Date{now}
|
||||
if now, err = ParseDate(string(data), "2006-01-02"); err == nil {
|
||||
*t = now
|
||||
return err
|
||||
}
|
||||
|
||||
// 带引号的自定义格式解析
|
||||
if now, err = time.ParseInLocation("\"2006-01-02\"", string(data), time.Local); err == nil {
|
||||
*t = Date{now}
|
||||
if now, err = ParseDate(string(data), "\"2006-01-02\""); err == nil {
|
||||
*t = now
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -76,7 +76,19 @@ func (t *Date) Scan(i interface{}) error {
|
||||
return errors.New("无法将值转换为时间戳")
|
||||
}
|
||||
|
||||
// 返回字符串
|
||||
func (t *Date) String() string {
|
||||
// 将Date类型转换为Time类型
|
||||
func (t Date) ToTime() Time {
|
||||
return Time{
|
||||
Time: t.Time,
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化为字符串
|
||||
func (t Date) ToString() string {
|
||||
return t.Format("2006-01-02")
|
||||
}
|
||||
|
||||
// 自定义格式字符串
|
||||
func (t Date) FormatToString(format string) string {
|
||||
return t.Format(format)
|
||||
}
|
||||
|
@@ -1,35 +1,59 @@
|
||||
package datetime
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 将字符串解析为日期
|
||||
func ParseDate(value string) (Date, error) {
|
||||
// ParseDate("2024-05-20") | ParseDate("2024/05/20", "2006/01/02")
|
||||
func ParseDate(values ...string) (Date, error) {
|
||||
|
||||
date, err := time.ParseInLocation("2006-01-02", value, time.Local)
|
||||
date, err := parseInLocation("2006-01-02", time.Local, values...)
|
||||
|
||||
return Date{Time: date}, err
|
||||
}
|
||||
|
||||
// 将字符串解析为日期
|
||||
func ParseDateInLocation(value string, location *time.Location) (Date, error) {
|
||||
// ParseDateInLocation(time.Local, "2024-05-20") | ParseDateInLocation(time.Local, "2024/05/20", "2006/01/02")
|
||||
func ParseDateInLocation(location *time.Location, values ...string) (Date, error) {
|
||||
|
||||
date, err := time.ParseInLocation("2006-01-02", value, location)
|
||||
date, err := parseInLocation("2006-01-02", location, values...)
|
||||
|
||||
return Date{Time: date}, err
|
||||
}
|
||||
|
||||
// 将字符串解析为时间
|
||||
func ParseTime(value string) (Time, error) {
|
||||
// ParseTime("2024-05-20 11:22:33") | ParseTime("2024/05/20 11:22:33", "2006/01/02 15:04:05")
|
||||
func ParseTime(values ...string) (Time, error) {
|
||||
|
||||
time, err := time.ParseInLocation("2006-01-02 15:04:05", value, time.Local)
|
||||
time, err := parseInLocation("2006-01-02 15:04:05", time.Local, values...)
|
||||
|
||||
return Time{Time: time}, err
|
||||
}
|
||||
|
||||
// 将字符串解析为时间
|
||||
func ParseTimeInLocation(value string, location *time.Location) (Time, error) {
|
||||
// ParseTimeInLocation(time.Local, "2024-05-20 11:22:33") | ParseTimeInLocation(time.Local, "2024/05/20 11:22:33", "2006/01/02 15:04:05")
|
||||
func ParseTimeInLocation(location *time.Location, values ...string) (Time, error) {
|
||||
|
||||
time, err := time.ParseInLocation("2006-01-02 15:04:05", value, location)
|
||||
time, err := parseInLocation("2006-01-02 15:04:05", location, values...)
|
||||
|
||||
return Time{Time: time}, err
|
||||
}
|
||||
|
||||
// 用于解析带有可选自定义格式的日期或时间字符串
|
||||
func parseInLocation(defaultLayout string, location *time.Location, values ...string) (time.Time, error) {
|
||||
|
||||
now := time.Now().Format(defaultLayout)
|
||||
layout := defaultLayout
|
||||
|
||||
if len(values) > 0 {
|
||||
now = strings.TrimSpace(values[0])
|
||||
}
|
||||
|
||||
if len(values) > 1 {
|
||||
layout = strings.TrimSpace(values[1])
|
||||
}
|
||||
|
||||
return time.ParseInLocation(layout, now, location)
|
||||
}
|
||||
|
@@ -38,28 +38,28 @@ func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var now time.Time
|
||||
var now Time
|
||||
|
||||
// 自定义格式解析
|
||||
if now, err = time.ParseInLocation("2006-01-02 15:04:05", string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
if now, err = ParseTime(string(data), "2006-01-02 15:04:05"); err == nil {
|
||||
*t = now
|
||||
return err
|
||||
}
|
||||
|
||||
// 带引号的自定义格式解析
|
||||
if now, err = time.ParseInLocation("\"2006-01-02 15:04:05\"", string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
if now, err = ParseTime(string(data), "\"2006-01-02 15:04:05\""); err == nil {
|
||||
*t = now
|
||||
return err
|
||||
}
|
||||
|
||||
// 默认格式解析
|
||||
if now, err = time.ParseInLocation(time.RFC3339, string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
if now, err = ParseTime(string(data), time.RFC3339); err == nil {
|
||||
*t = now
|
||||
return err
|
||||
}
|
||||
|
||||
if now, err = time.ParseInLocation("\""+time.RFC3339+"\"", string(data), time.Local); err == nil {
|
||||
*t = Time{now}
|
||||
if now, err = ParseTime(string(data), "\""+time.RFC3339+"\""); err == nil {
|
||||
*t = now
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -87,7 +87,19 @@ func (t *Time) Scan(i interface{}) error {
|
||||
return errors.New("无法将值转换为时间戳")
|
||||
}
|
||||
|
||||
// 返回字符串
|
||||
func (t *Time) String() string {
|
||||
// 将Time类型转换为Date类型
|
||||
func (t Time) ToDate() Date {
|
||||
return Date{
|
||||
Time: t.Time,
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化为字符串
|
||||
func (t Time) ToString() string {
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// 自定义格式字符串
|
||||
func (t Time) FormatToString(format string) string {
|
||||
return t.Format(format)
|
||||
}
|
||||
|
Reference in New Issue
Block a user