From faafa8ee2a7985be5a8e7d94554ff892c1ef30ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 4 Sep 2023 20:35:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=85=E8=A3=85=E7=B1=BB=E5=9E=8B=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=BD=AC=E4=B8=BA=E6=97=B6=E9=97=B4=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 12 ++++++++++++ int.go | 13 +++++++++++++ string.go | 16 ++++++++++++++++ 3 files changed, 41 insertions(+) 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<白茶清欢>