go-jamming/common/time.go

36 lines
782 B
Go
Raw Normal View History

package common
2021-04-10 16:34:09 +02:00
import (
"time"
)
// https://labs.yulrizka.com/en/stubbing-time-dot-now-in-golang/
// None of the above are very appealing. For now, just use the lazy way.
var Now = time.Now
2021-04-10 10:17:38 +02:00
const (
IsoFormat = "2006-01-02T15:04:05.000Z"
)
// TimeToIso converts time to ISO string format, up to seconds.
func TimeToIso(theTime time.Time) string {
return theTime.Format(IsoFormat)
}
// IsoToTime converts an ISO time string into a time.Time object
// As produced by clients using day.js - e.g. 2021-04-09T15:51:43.732Z
func IsoToTime(date string) time.Time {
return ToTime(date, IsoFormat)
}
func ToTime(date string, format string) time.Time {
if date == "" {
2021-04-10 16:34:09 +02:00
return time.Time{}
}
t, err := time.Parse(format, date)
2021-04-10 10:17:38 +02:00
if err != nil {
return time.Time{}
2021-04-10 10:17:38 +02:00
}
return t
}