snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
build / build (push) Failing after 3s
Some checks failed
build / build (push) Failing after 3s
This commit is contained in:
48
model/time.go
Normal file
48
model/time.go
Normal file
@@ -0,0 +1,48 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user