1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package lib
- import (
- "strconv"
- "time"
- )
- /*
- 时间常量
- */
- const (
- //定义每分钟的秒数
- SecondsPerMinute = 60
- //定义每小时的秒数
- SecondsPerHour = SecondsPerMinute * 60
- //定义每天的秒数
- SecondsPerDay = SecondsPerHour * 24
- )
- /*
- 时间转换函数
- */
- func resolveTime(seconds int) (day int, hour int, minute int, second int) {
- //秒
- second = seconds % 60
- //天
- day = seconds / SecondsPerDay
- seconds -= day * SecondsPerDay
- //时
- hour = seconds / SecondsPerHour
- seconds -= hour * SecondsPerHour
- //分
- minute = seconds / SecondsPerMinute
- seconds -= minute * SecondsPerHour
- return
- }
- func MinuteToDataTime(t int) string {
- str := ""
- day, hour, minute, second := resolveTime(t)
- if day > 0 {
- str += strconv.Itoa(day) + "天 "
- }
- if hour > 0 {
- str += strconv.Itoa(hour) + "小时 "
- }
- if minute > 0 {
- str += strconv.Itoa(minute) + "分钟 "
- }
- if second > 0 {
- str += strconv.Itoa(second) + "秒"
- }
- return str
- }
- func TimeSinceToString(startTime, endTime time.Time) string {
- str := ""
- // 计算时间差
- duration := endTime.Sub(startTime)
- // 将时间差表示为天、小时和分钟
- days := int(duration.Hours() / 24)
- hours := int(duration.Hours()) % 24
- minutes := int(duration.Minutes()) % 60
- seconds := int(duration.Seconds()) % 60
- //fmt.Printf("时间差为 %d 天 %d 小时 %d 分钟\n", days, hours, minutes)
- if days > 0 {
- str += strconv.Itoa(days) + "天 "
- }
- if hours > 0 {
- str += strconv.Itoa(hours) + "小时 "
- }
- if minutes > 0 {
- str += strconv.Itoa(minutes) + "分钟 "
- }
- if seconds > 0 {
- str += strconv.Itoa(seconds) + "秒 "
- }
- return str
- }
|