2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-09-16 10:51:02 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-09-16 10:51:02 +08:00
|
|
|
|
|
|
|
package gconv
|
|
|
|
|
|
|
|
import (
|
2019-05-07 22:28:34 +08:00
|
|
|
"time"
|
2019-07-18 18:59:49 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2018-09-16 10:51:02 +08:00
|
|
|
)
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Time converts `any` to time.Time.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Time(any interface{}, format ...string) time.Time {
|
2020-06-09 14:19:23 +08:00
|
|
|
// It's already this type.
|
|
|
|
if len(format) == 0 {
|
2021-02-05 14:44:20 +08:00
|
|
|
if v, ok := any.(time.Time); ok {
|
2020-06-09 14:19:23 +08:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
if t := GTime(any, format...); t != nil {
|
2019-07-20 16:41:17 +08:00
|
|
|
return t.Time
|
|
|
|
}
|
|
|
|
return time.Time{}
|
2018-12-16 22:22:07 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// Duration converts `any` to time.Duration.
|
|
|
|
// If `any` is string, then it uses time.ParseDuration to convert it.
|
|
|
|
// If `any` is numeric, then it converts `any` as nanoseconds.
|
2021-02-05 14:44:20 +08:00
|
|
|
func Duration(any interface{}) time.Duration {
|
2020-06-09 14:19:23 +08:00
|
|
|
// It's already this type.
|
2021-02-05 14:44:20 +08:00
|
|
|
if v, ok := any.(time.Duration); ok {
|
2020-06-09 14:19:23 +08:00
|
|
|
return v
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
s := String(any)
|
2020-03-11 23:17:41 +08:00
|
|
|
if !utils.IsNumeric(s) {
|
2020-07-28 22:46:15 +08:00
|
|
|
d, _ := gtime.ParseDuration(s)
|
2019-05-07 22:28:34 +08:00
|
|
|
return d
|
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
return time.Duration(Int64(any))
|
2018-12-16 22:22:07 +08:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:26:39 +08:00
|
|
|
// GTime converts `any` to *gtime.Time.
|
|
|
|
// The parameter `format` can be used to specify the format of `any`.
|
2023-09-13 19:29:25 +08:00
|
|
|
// It returns the converted value that matched the first format of the formats slice.
|
2021-05-17 21:26:39 +08:00
|
|
|
// If no `format` given, it converts `any` using gtime.NewFromTimeStamp if `any` is numeric,
|
|
|
|
// or using gtime.StrToTime if `any` is string.
|
2021-02-05 14:44:20 +08:00
|
|
|
func GTime(any interface{}, format ...string) *gtime.Time {
|
|
|
|
if any == nil {
|
2019-09-30 17:23:23 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := any.(iGTime); ok {
|
2021-06-01 19:47:02 +08:00
|
|
|
return v.GTime(format...)
|
|
|
|
}
|
2020-06-09 14:19:23 +08:00
|
|
|
// It's already this type.
|
|
|
|
if len(format) == 0 {
|
2021-02-05 14:44:20 +08:00
|
|
|
if v, ok := any.(*gtime.Time); ok {
|
2020-06-09 14:19:23 +08:00
|
|
|
return v
|
|
|
|
}
|
2021-06-01 19:47:02 +08:00
|
|
|
if t, ok := any.(time.Time); ok {
|
|
|
|
return gtime.New(t)
|
|
|
|
}
|
|
|
|
if t, ok := any.(*time.Time); ok {
|
|
|
|
return gtime.New(t)
|
|
|
|
}
|
2020-06-09 14:19:23 +08:00
|
|
|
}
|
2021-02-05 14:44:20 +08:00
|
|
|
s := String(any)
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(s) == 0 {
|
|
|
|
return gtime.New()
|
|
|
|
}
|
|
|
|
// Priority conversion using given format.
|
|
|
|
if len(format) > 0 {
|
2023-09-13 19:29:25 +08:00
|
|
|
for _, item := range format {
|
|
|
|
t, err := gtime.StrToTimeFormat(s, item)
|
|
|
|
if t != nil && err == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-03-11 23:17:41 +08:00
|
|
|
if utils.IsNumeric(s) {
|
2019-06-19 09:06:52 +08:00
|
|
|
return gtime.NewFromTimeStamp(Int64(s))
|
|
|
|
} else {
|
|
|
|
t, _ := gtime.StrToTime(s)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|