SimpleCloudNotifier/scnserver/models/time.go
Mike Schwörer 7546c2a1a4
All checks were successful
Build Docker and Deploy / Build Docker Container (push) Successful in 1m17s
Build Docker and Deploy / Run Unit-Tests (push) Successful in 2m38s
Build Docker and Deploy / Deploy to Server (push) Successful in 9s
Fix test pipeline
2024-09-20 21:23:26 +02:00

66 lines
1.2 KiB
Go

package models
import (
"encoding/json"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rfctime"
"time"
)
type SCNTime time.Time
func (t SCNTime) MarshalToDB(v SCNTime) (int64, error) {
return v.Time().UnixMilli(), nil
}
func (t SCNTime) UnmarshalToModel(v int64) (SCNTime, error) {
return NewSCNTime(time.UnixMilli(v)), nil
}
func (t SCNTime) Time() time.Time {
return time.Time(t)
}
func (t *SCNTime) UnmarshalJSON(data []byte) error {
str := ""
if err := json.Unmarshal(data, &str); err != nil {
return err
}
t0, err := time.Parse(time.RFC3339Nano, str)
if err != nil {
return err
}
*t = SCNTime(t0)
return nil
}
func (t SCNTime) MarshalJSON() ([]byte, error) {
str := t.Time().In(time.UTC).Format(time.RFC3339Nano)
return json.Marshal(str)
}
func NewSCNTime(t time.Time) SCNTime {
return SCNTime(t)
}
func NewSCNTimePtr(t *time.Time) *SCNTime {
if t == nil {
return nil
}
return langext.Ptr(SCNTime(*t))
}
func NowSCNTime() SCNTime {
return SCNTime(time.Now())
}
func tt(v rfctime.AnyTime) time.Time {
if r, ok := v.(time.Time); ok {
return r
}
if r, ok := v.(rfctime.RFCTime); ok {
return r.Time()
}
return time.Unix(0, v.UnixNano()).In(v.Location())
}