tvl-depot/fun/clbot/gerrit/gerritevents/time.go
Luke Granger-Brown c05803ff14 feat(clbot): Create Gerrit watcher and basic clbot binary.
gerrit.Watcher is a class which watches the Gerrit stream-events SSH
connection and produces events.

There's a basic CLBot binary as well, to demonstrate driving it to
produce messages on the logging output. It doesn't really do anything
else.

Change-Id: I274fe0a77c8329f79456425405e2fbdc3ca2edf0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/245
Reviewed-by: tazjin <mail@tazj.in>
2020-06-14 17:16:32 +00:00

38 lines
740 B
Go

package gerritevents
import (
"fmt"
"strconv"
"time"
)
// Time is a time.Time that is formatted as a Unix timestamp in JSON.
type Time struct {
time.Time
}
// UnmarshalJSON unmarshals a Unix timestamp into a Time.
func (t *Time) UnmarshalJSON(bs []byte) error {
if string(bs) == "null" {
return nil
}
u, err := strconv.ParseInt(string(bs), 10, 64)
if err != nil {
return err
}
t.Time = time.Unix(u, 0)
return nil
}
// MarshalJSON marshals a Time into a Unix timestamp.
func (t *Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("%d", t.Unix())), nil
}
// IsSet returns true if the time.Time is non-zero.
func (t *Time) IsSet() bool {
return !t.IsZero()
}