diff --git a/define.go b/define.go index 1c03b51..f4e49c7 100644 --- a/define.go +++ b/define.go @@ -7,6 +7,8 @@ // Date : 2023-05-05 14:44 package wrapper +import "time" + const ( DataTypeUnknown = "unknown" DataTypeNil = "nil" @@ -477,6 +479,16 @@ type Float64SliceResult struct { Err error } +// DurationResult 时间转换结果 +// +// Author : zhangdeman001@ke.com<张德满> +// +// Date : 20:32 2023/9/4 +type DurationResult struct { + Value time.Duration + Err error +} + // StringSliceResult ... // // Author : go_developer@163.com<白茶清欢> diff --git a/int.go b/int.go index 4d6847e..a62fda4 100644 --- a/int.go +++ b/int.go @@ -10,6 +10,7 @@ package wrapper import ( "fmt" "math" + "time" ) // Int int类型 @@ -19,6 +20,18 @@ import ( // Date : 13:57 2023/5/5 type Int int64 +// ToDuration ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:33 2023/9/4 +func (i Int) ToDuration(timeUnit time.Duration) DurationResult { + return DurationResult{ + Value: time.Duration(i.ToInt64().Value) * timeUnit, + Err: nil, + } +} + // ToInt8 ... // // Author : go_developer@163.com<白茶清欢> diff --git a/string.go b/string.go index 4f804cc..5b1a440 100644 --- a/string.go +++ b/string.go @@ -950,6 +950,22 @@ func (str String) ToNumberSlice(splitChar ...string) Float64SliceResult { return str.ToFloat64Slice(splitChar...) } +// ToDuration 转换为时间格式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:31 2023/9/4 +func (str String) ToDuration(timeUnit time.Duration) DurationResult { + int64Val := str.ToInt64() + if nil != int64Val.Err { + return DurationResult{ + Value: 0, + Err: int64Val.Err, + } + } + return Int(int64Val.Value).ToDuration(timeUnit) +} + // ToStringSlice ... // // Author : go_developer@163.com<白茶清欢>