forked from mtusi2026/mtusi2026lab2
refactor(app) AI refactoring before moving to Lab3 done
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// storageTimePrecision is the maximum precision shared by PostgreSQL
|
||||
// timestamptz columns and the JSON representation. 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. Use all six digits that
|
||||
// PostgreSQL can store, and omit the fraction only for an exact second.
|
||||
// A fixed six-digit fraction is also accepted by strict consumers that only
|
||||
// understand 3- or 6-digit fractions (for example Python 3.9's parser).
|
||||
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 {
|
||||
// AGENT: The API used to truncate timestamps to whole seconds to keep
|
||||
// keyset ordering consistent with JSON. RFC 3339 supports fractional seconds
|
||||
// and PostgreSQL has microsecond precision, so use that precision end to end:
|
||||
// timestamps remain ordered after serialization without discarding sub-second
|
||||
// information.
|
||||
return RFCFormTime(time.Now().UTC().Truncate(storageTimePrecision))
|
||||
}
|
||||
Reference in New Issue
Block a user