package timeutil import "time" var ( validTimeLayouts = []string{ "15:04:05.999999999", "15:04:05", "15:04", "3:04:05PM", "3:04PM", "3PM", } ) type Time struct { Hour int Minute int Second int Nanosecond int } func ParseTime(value string) (Time, error) { var t time.Time for _, layout := range validTimeLayouts { var err error if t, err = time.Parse(layout, value); err == nil { return Time{ Hour: t.Hour(), Minute: t.Minute(), Second: t.Second(), Nanosecond: t.Nanosecond(), }, nil } } return Time{}, &time.ParseError{ Value: value, Message: "invalid time", } } func Now() Time { t := time.Now() return Time{ Hour: t.Hour(), Minute: t.Minute(), Second: t.Second(), Nanosecond: t.Nanosecond(), } } func (t Time) After(other Time) bool { return other.Before(t) } func (t Time) Before(other Time) bool { if t.Hour == other.Hour { if t.Minute == other.Minute { if t.Second == other.Second { return t.Nanosecond < other.Nanosecond } return t.Second < other.Second } return t.Minute < other.Minute } return t.Hour < other.Hour } func (t Time) Eq(other Time) bool { return t.Hour == other.Hour && t.Minute == other.Minute && t.Second == other.Second && t.Nanosecond == other.Nanosecond }