From d8a982bf0744abb48366bd5fa5ac0cf711ec68d8 Mon Sep 17 00:00:00 2001 From: wangxudong123 <834971685@qq.com> Date: Thu, 17 Mar 2022 19:44:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=20(#25)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: wangxd01 --- datetime/conversion.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 datetime/conversion.go diff --git a/datetime/conversion.go b/datetime/conversion.go new file mode 100644 index 0000000..367c5a0 --- /dev/null +++ b/datetime/conversion.go @@ -0,0 +1,52 @@ +// Copyright 2021 dudaodong@gmail.com. All rights reserved. +// Use of this source code is governed by MIT license. + +package datetime + +import "time" + +type theTime struct { + unix int64 +} + +func NewUnixNow() *theTime { + return &theTime{unix: time.Now().Unix()} +} + +func NewUnix(unix int64) *theTime { + return &theTime{unix: unix} +} + +func NewFormat(t string) (*theTime, error) { + timeLayout := "2006-01-02 15:04:05" + loc := time.FixedZone("CST", 8*3600) + tt, err := time.ParseInLocation(timeLayout, t, loc) + if err != nil { + return nil, err + } + return &theTime{unix: tt.Unix()}, nil +} + +func NewISO8601(iso8601 string) (*theTime, error) { + t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC) + if err != nil { + return nil, err + } + return &theTime{unix: t.Unix()}, nil +} + +func (t *theTime) ToUnix() int64 { + return t.unix +} + +func (t *theTime) ToFormat() string { + return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05") +} + +func (t *theTime) ToFormatForTpl(tpl string) string { + return time.Unix(t.unix, 0).Format(tpl) +} + +func (t *theTime) ToIso8601() string { + return time.Unix(t.unix, 0).Format(time.RFC3339) +}