Skip to content

Commit

Permalink
调整单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 17, 2023
1 parent 882ec35 commit b4a7814
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
16 changes: 9 additions & 7 deletions dateTime/dt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -153,15 +155,15 @@ 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)
}

// ToTime 获取time.Time类型
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)
}

Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions exception/webException.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
}
}
}
7 changes: 7 additions & 0 deletions test/dateTime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit b4a7814

Please sign in to comment.