Skip to content

Commit

Permalink
Merge pull request #448 from Leif160519/dev
Browse files Browse the repository at this point in the history
重写GetTimeDuration方法,将持续时间为开始时间与当前时间的间隔改为开始时间和结束时间的间隔(默认)
  • Loading branch information
feiyu563 authored Oct 16, 2024
2 parents 6d8f4c1 + 686de23 commit 810d3ee
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions controllers/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,46 @@ func GetTime(timeStr interface{}, timeFormat ...string) string {
}

// 转换时间为持续时长
func GetTimeDuration(date string) string {
func GetTimeDuration(startTime string,endTime string) string {
var tm = "N/A"
if date != "" {
T1 := date[0:10]
T2 := date[11:19]
T3 := T1 + " " + T2
tm2, _ := time.Parse("2006-01-02 15:04:05", T3)
sub := time.Now().UTC().Sub(tm2.UTC())
if startTime != "" && endTime != "" {
starT1 := startTime[0:10]
starT2 := startTime[11:19]
starT3 := starT1 + " " + starT2
startm2, err := time.Parse("2006-01-02 15:04:05", starT3)
if err != nil {
return tm // 如果解析失败,则返回N/A
}

endT1 := endTime[0:10]
endT2 := endTime[11:19]
endT3 := endT1 + " " + endT2
endm2, err := time.Parse("2006-01-02 15:04:05", endT3)
if err != nil {
return tm // 如果解析失败,则返回N/A
}

sub := endm2.UTC().Sub(startm2.UTC())

t := int64(sub.Seconds())
if t > 86400 {
tm = fmt.Sprintf("%dd%dh", t/86400, t%86400/3600)
} else if t > 3600 {
tm = fmt.Sprintf("%dh%dm", t/3600, t%3600/60)
} else if t > 60 {
tm = fmt.Sprintf("%dh%dm", t/60, t%60)
if t >= 86400 {
days := t / 86400
hours := (t % 86400) / 3600
tm = fmt.Sprintf("%dd%dh", days, hours)
} else {
tm = fmt.Sprintf("%ds", t)
hours := t / 3600
minutes := (t % 3600) / 60
if hours > 0 {
tm = fmt.Sprintf("%dh%dm", hours, minutes)
} else {
// 如果小时为0,则只显示分钟和秒
seconds := t % 60
tm = fmt.Sprintf("%dm%ds", minutes, seconds)
if minutes == 0 {
// 如果分钟也为0,则只显示秒
tm = fmt.Sprintf("%ds", seconds)
}
}
}
}
return tm
Expand Down

0 comments on commit 810d3ee

Please sign in to comment.