Return iso dates for everything date related from the api #130

Merged
konrad merged 15 commits from feature/datetime into master 2020-02-08 12:48:51 +00:00
1 changed files with 7 additions and 0 deletions
Showing only changes of commit 1cd6fa8eaa - Show all commits

View File

@ -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)