Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重写GetTimeDuration方法,将持续时间为开始时间与当前时间的间隔改为开始时间和结束时间的间隔(默认) #448

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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