File: time.go

package info (click to toggle)
golang-sourcehut-emersion-gqlclient 0.0~git20230820.8873fe0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136 kB
  • sloc: makefile: 2
file content (36 lines) | stat: -rw-r--r-- 527 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package gqlclient

import (
	"encoding/json"
	"time"
)

const timeLayout = time.RFC3339Nano

type Time struct {
	time.Time
}

func (t Time) MarshalJSON() ([]byte, error) {
	var v interface{}
	if !t.IsZero() {
		v = t.Format(timeLayout)
	}
	return json.Marshal(v)
}

func (t *Time) UnmarshalJSON(b []byte) error {
	if string(b) == "null" {
		t.Time = time.Time{}
		return nil
	}

	var s string
	if err := json.Unmarshal(b, &s); err != nil {
		return err
	}

	var err error
	t.Time, err = time.Parse(timeLayout, s)
	return err
}