forked from JointCloud/pcm-coordinator
110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
/*
|
|
|
|
Copyright (c) [2023] [pcm]
|
|
[pcm-coordinator] is licensed under Mulan PSL v2.
|
|
You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
You may obtain a copy of Mulan PSL v2 at:
|
|
http://license.coscl.org.cn/MulanPSL2
|
|
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
See the Mulan PSL v2 for more details.
|
|
|
|
*/
|
|
|
|
package timeutils
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
var timeTemplates = []string{
|
|
"2006-01-02T15:04:05Z07:00", //RFC3339
|
|
"2006-01-02 15:04:05", //常规类型
|
|
"2006/01/02T15:04:05Z07:00", //RFC3339
|
|
"2006/01/02 15:04:05",
|
|
"2006-01-02",
|
|
"2006/01/02",
|
|
"15:04:05",
|
|
}
|
|
|
|
func GetTimeDurationString(startTime string, endTime string) string {
|
|
end, _ := time.Parse("2006-01-02 15:04:05", endTime)
|
|
start, _ := time.Parse("2006-01-02 15:04:05", startTime)
|
|
difference := end.Sub(start)
|
|
return difference.Truncate(time.Second).String()
|
|
}
|
|
|
|
func DurationToDateTime(timeData uint64) time.Time {
|
|
timeString := time.Unix(int64(timeData), 0).Format("2006-01-02 15:04:05")
|
|
return TimeStringToGoTime(timeString)
|
|
}
|
|
|
|
// TimeStringToGoTime 时间格式字符串转换
|
|
func TimeStringToGoTime(tm string) time.Time {
|
|
for i := range timeTemplates {
|
|
t, err := time.ParseInLocation(timeTemplates[i], tm, time.Local)
|
|
if nil == err && !t.IsZero() {
|
|
return t
|
|
}
|
|
}
|
|
return time.Time{}
|
|
}
|
|
|
|
func TimeStringRemoveZone(tm string) string {
|
|
timeZone, _ := time.Parse("2006-01-02 15:04:05 -0700 MST", tm)
|
|
return timeZone.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
func TimeRemoveZone(tm time.Time) time.Time {
|
|
parse, err := time.Parse("2006-01-02 15:04:05", tm.Format("2006-01-02 15:04:05"))
|
|
if err != nil {
|
|
return time.Time{}
|
|
}
|
|
return parse
|
|
}
|
|
|
|
func StringToUnixTime(str string) int64 {
|
|
dt, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return dt.Unix()
|
|
}
|
|
|
|
func UnixTimeToString(ut int64) string {
|
|
t := time.Unix(ut, 0)
|
|
|
|
return t.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
// MillisecondsToUTCString 将毫秒时间戳转换为UTC时间的字符串表示
|
|
func MillisecondsToUTCString(milliseconds int64, layout string) string {
|
|
// 将毫秒转换为秒
|
|
timestamp := milliseconds / 1000
|
|
// 创建time.Time对象
|
|
timeObj := time.Unix(timestamp, (milliseconds%1000)*int64(time.Millisecond))
|
|
// 使用RFC3339格式返回UTC时间字符串
|
|
return timeObj.Local().Format(layout)
|
|
}
|
|
|
|
// SecondsToUTCString 将秒时间戳转换为UTC时间的字符串表示
|
|
func SecondsToUTCString(seconds int64, layout string) string {
|
|
// 创建time.Time对象
|
|
timeObj := time.Unix(seconds, 0)
|
|
// 使用RFC3339格式返回UTC时间字符串
|
|
return timeObj.Local().Format(layout)
|
|
}
|
|
|
|
// MillisecondsToAddDurationToUTCString 将毫秒时间戳加上持续时间毫秒后转换为UTC时间的字符串表示
|
|
func MillisecondsToAddDurationToUTCString(milliseconds int64, durationMilliseconds int64, layout string) string {
|
|
// 将毫秒时间戳转换为秒
|
|
timestamp := milliseconds / 1000
|
|
// 创建time.Time对象
|
|
timeObj := time.Unix(timestamp, (milliseconds%1000)*int64(time.Millisecond))
|
|
// 添加持续时间
|
|
duration := time.Duration(durationMilliseconds) * time.Millisecond
|
|
// 返回加上持续时间后的UTC时间字符串
|
|
return timeObj.Add(duration).Local().Format(layout)
|
|
}
|