forked from mtusi2026/mtusi2026lab3
28 lines
704 B
Go
28 lines
704 B
Go
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
// PostgreSQL stores microseconds, so keeping the same boundary in Go
|
|
// prevents a create response from showing more precision
|
|
// than a later read can reproduce.
|
|
const storageTimePrecision = time.Microsecond
|
|
|
|
type RFCFormTime time.Time
|
|
|
|
func (t RFCFormTime) MarshalJSON() ([]byte, error) {
|
|
// RFC 3339 permits an optional fractional second.
|
|
utc := time.Time(t).UTC().Truncate(storageTimePrecision)
|
|
layout := "2006-01-02T15:04:05Z"
|
|
if utc.Nanosecond() != 0 {
|
|
layout = "2006-01-02T15:04:05.000000Z"
|
|
}
|
|
return json.Marshal(utc.Format(layout))
|
|
}
|
|
|
|
func NowUTC() RFCFormTime {
|
|
return RFCFormTime(time.Now().UTC().Truncate(storageTimePrecision))
|
|
}
|