Return iso dates for everything date related from the api #130
|
@ -21,22 +21,29 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
// TimeStamp is a type which holds a unix timestamp, but becomes a RFC3339 date when parsed to json.
|
||||
// This allows us to save the time as a unix timestamp into the database while returning it as an iso
|
||||
// date to the api client.
|
||||
type TimeStamp int64
|
||||
|
||||
// ToTime returns a time.Time from a TimeStamp
|
||||
func (ts *TimeStamp) ToTime() time.Time {
|
||||
return time.Unix(int64(*ts), 0)
|
||||
}
|
||||
|
||||
// FromTime converts a time.Time to a TimeStamp
|
||||
func FromTime(t time.Time) TimeStamp {
|
||||
return TimeStamp(t.Unix())
|
||||
}
|
||||
|
||||
// MarshalJSON converts a TimeStamp to a json string
|
||||
func (ts *TimeStamp) MarshalJSON() ([]byte, error) {
|
||||
var s string
|
||||
s = `"` + ts.ToTime().String() + `"`
|
||||
return []byte(s), nil
|
||||
}
|
||||
|
||||
// UnmarshalJson converts an iso date string from json to a TimeStamp
|
||||
func (ts *TimeStamp) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(data, &s)
|
||||
|
|
Loading…
Reference in New Issue