2023-06-15 21:22:51 +08:00
|
|
|
//
|
|
|
|
// time_helper.go
|
|
|
|
// Copyright (C) 2022 tiglog <me@tiglog.com>
|
|
|
|
//
|
|
|
|
// Distributed under terms of the MIT license.
|
|
|
|
//
|
|
|
|
|
|
|
|
package helper
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
func Format(ts int64, layout string) string {
|
|
|
|
return time.Unix(ts, 0).Format(layout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatDate(ts int64) string {
|
|
|
|
return Format(ts, "2006-01-02")
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatDt(ts int64) string {
|
|
|
|
return Format(ts, "2006-01-02 15:04")
|
|
|
|
}
|
|
|
|
|
|
|
|
func FormatDateTime(ts int64) string {
|
|
|
|
return Format(ts, "2006-01-02 15:04:05")
|
|
|
|
}
|
2023-09-19 15:30:20 +08:00
|
|
|
|
|
|
|
func IsUnixZero(t time.Time) bool {
|
|
|
|
return t.Unix() == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTimeUTCPointer() *time.Time {
|
|
|
|
t := time.Now().UTC()
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimeToPointer(t time.Time) *time.Time {
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnixToTimePointer(v int64) *time.Time {
|
|
|
|
if v == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return TimeToPointer(time.Unix(v, 0))
|
|
|
|
}
|