Files
upstream-traggo/model/time.go
upstream-mirror cda7c1a474
Some checks failed
build / build (push) Failing after 3s
snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 05:24:45 +05:00

49 lines
1.0 KiB
Go

package model
import (
"fmt"
"io"
"time"
)
// Time scalar type for graphql
type Time time.Time
// Time returns the wrapped time
func (t Time) Time() time.Time {
return time.Time(t)
}
// OmitTimeZone omits the time zone and removes a utc date.
func (t Time) OmitTimeZone() time.Time {
x := t.Time()
return time.Date(x.Year(), x.Month(), x.Day(), x.Hour(), x.Minute(), x.Second(), x.Nanosecond(), time.UTC)
}
// UTC changes the timezone to utc.
func (t Time) UTC() time.Time {
return t.Time().UTC()
}
// MarshalGQL implements the graphql.Marshaler interface
func (t Time) MarshalGQL(w io.Writer) {
if _, err := w.Write([]byte(fmt.Sprintf(`"%s"`, t.Time().Format(time.RFC3339)))); err != nil {
panic(err)
}
}
// UnmarshalGQL implements the graphql.Unmarshaler interface
func (t *Time) UnmarshalGQL(v interface{}) error {
raw, ok := v.(string)
if !ok {
return fmt.Errorf("time must be a string")
}
parse, err := time.Parse(time.RFC3339, raw)
if err != nil {
return err
}
*t = Time(parse)
return nil
}