2022-11-13 12:03:58 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
// Convenience types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-11-13 12:41:13 -05:00
|
|
|
// PyTimeStamp is a timestamp that marshals to python-style timestamp strings (long nano).
|
2022-11-13 12:03:58 -05:00
|
|
|
PyTimestamp time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
const PytTimeFormat = "2006-01-02T15:04:05.999999-07:00"
|
|
|
|
|
|
|
|
func (t *PyTimestamp) MarshalJSON() ([]byte, error) {
|
|
|
|
rv := fmt.Sprintf("%q", time.Time(*t).Format(PytTimeFormat))
|
|
|
|
return []byte(rv), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *PyTimestamp) UnmarshalJSON(b []byte) error {
|
|
|
|
s := strings.Trim(string(b), `"`)
|
|
|
|
tm, err := time.Parse(PytTimeFormat, s)
|
|
|
|
*t = PyTimestamp(tm)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|