From b4a7814e01c86953dd519ed4353effd1064cfc57 Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Sun, 17 Dec 2023 17:21:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dateTime/dt.go | 16 +++++++++------- exception/{expamle_test.go => _expamle_test.go} | 0 exception/webException.go | 5 ++--- test/dateTime_test.go | 7 +++++++ 4 files changed, 18 insertions(+), 10 deletions(-) rename exception/{expamle_test.go => _expamle_test.go} (100%) diff --git a/dateTime/dt.go b/dateTime/dt.go index 9aae630c9..0bcdbc3a9 100644 --- a/dateTime/dt.go +++ b/dateTime/dt.go @@ -30,7 +30,7 @@ func New(time time.Time) DateTime { // New 初始化 func NewUnix(sec int64) DateTime { return DateTime{ - time: time.Unix(sec,0), + time: time.Unix(sec, 0), } } @@ -82,10 +82,12 @@ func (receiver DateTime) Minute() int { return receiver.time.Minute() } // Second 获取秒 func (receiver DateTime) Second() int { return receiver.time.Second() } -// TotalSeconds 获取总秒数 -func (receiver DateTime) TotalSeconds() float64 { - return receiver.time.Sub(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local)).Seconds() -} +//// TotalSeconds 获取总秒数 +//func (receiver DateTime) TotalSeconds() float64 { +// m, _ := time.ParseDuration(receiver.time.String()) +// return m.Seconds() +// //return receiver.time.Sub(time.Date(0, 0, 0, 0, 0, 0, 0, time.Local)).Seconds() +//} // Duration 得到Duration func (receiver DateTime) Duration() time.Duration { @@ -153,7 +155,7 @@ func (receiver DateTime) AddMillisecond(millisecond int) DateTime { } // Sub 时间相减 -func (receiver DateTime) Sub(dt DateTime) time.Duration{ +func (receiver DateTime) Sub(dt DateTime) time.Duration { return receiver.time.Sub(dt.time) } @@ -161,7 +163,7 @@ func (receiver DateTime) Sub(dt DateTime) time.Duration{ func (receiver DateTime) ToTime() time.Time { return receiver.time } // After 是否比dt时间大(晚) -func (receiver DateTime) After(dt DateTime) bool{ +func (receiver DateTime) After(dt DateTime) bool { return receiver.time.After(dt.time) } diff --git a/exception/expamle_test.go b/exception/_expamle_test.go similarity index 100% rename from exception/expamle_test.go rename to exception/_expamle_test.go diff --git a/exception/webException.go b/exception/webException.go index 37d75b991..0716537d6 100644 --- a/exception/webException.go +++ b/exception/webException.go @@ -32,10 +32,9 @@ func ThrowWebExceptionfBool(isTrue bool, statusCode int, format string, a ...any } } - // ThrowWebExceptionError 抛出WebException异常 func ThrowWebExceptionError(statusCode int, err error) { - if err!=nil { + if err != nil { panic(WebException{StatusCode: statusCode, Message: err.Error()}) } -} \ No newline at end of file +} diff --git a/test/dateTime_test.go b/test/dateTime_test.go index abf9f632e..8ee69cb0d 100644 --- a/test/dateTime_test.go +++ b/test/dateTime_test.go @@ -45,8 +45,15 @@ func TestDateTime_ToString(t *testing.T) { assert.Equal(t, "2022-09-06 21:13:25", dt.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")) assert.Equal(t, "2022-09-06 21:14:27", dt.AddSeconds(2).ToString("yyyy-MM-dd HH:mm:ss")) assert.Equal(t, "2022-09-06 21:14:24", dt.AddSeconds(-1).ToString("yyyy-MM-dd HH:mm:ss")) + assert.Equal(t, "2022-09-06 21:14:26", dt.AddMillisecond(1000).ToString("yyyy-MM-dd HH:mm:ss")) + assert.Equal(t, "2022-09-06 21:14:24", dt.AddMillisecond(-1000).ToString("yyyy-MM-dd HH:mm:ss")) + assert.Equal(t, float64(24), dt.Sub(dateTime.New(time.Date(2022, 9, 05, 21, 14, 25, 0, time.Local))).Hours()) assert.Equal(t, "2023-11-09 21:14:25", dt.AddDate(1, 2, 3).ToString("yyyy-MM-dd HH:mm:ss")) assert.Equal(t, "2022-09-06 22:16:28", dt.AddTime(1, 2, 3).ToString("yyyy-MM-dd HH:mm:ss")) + assert.Equal(t, time.Unix(100, 0).String(), dateTime.NewUnix(100).ToTime().String()) + assert.Equal(t, time.UnixMilli(100000).String(), dateTime.NewUnixMilli(100000).ToTime().String()) + dateTime.Since(dateTime.Now()) + dateTime.Now().Duration().String() }