fix timestamps once and for all

This commit is contained in:
saji 2023-07-03 13:51:15 -05:00
parent a36e1478bc
commit c68dff9d40
2 changed files with 7 additions and 9 deletions

View file

@ -94,11 +94,9 @@ func run(ctx *cli.Context) (err error) {
segments := strings.Split(dumpLine, " ")
var cd skylab.BusEvent
// this is cursed but easiest way to get a float from a string.
var unixSeconds, unixNanos int64
fmt.Sscanf(segments[0], "(%d.%d)", &unixSeconds, &unixNanos)
slog.Info("hihi hi", "time", unixSeconds)
cd.Timestamp = time.Unix(unixSeconds, unixNanos)
var unixSeconds, unixMicros int64
fmt.Sscanf(segments[0], "(%d.%d)", &unixSeconds, &unixMicros)
cd.Timestamp = time.Unix(unixSeconds, unixMicros*1000) // the canlog does usec precision for the decimal part.
// this is for the latter part, we need to split id/data
hexes := strings.Split(segments[2], "#")

View file

@ -86,7 +86,7 @@ func ToCanFrame(p Packet) (id uint32, data []byte, err error) {
// internal structure for partially decoding json object.
// includes
type RawJsonEvent struct {
Timestamp uint32 `json:"ts" db:"ts"`
Timestamp int64 `json:"ts" db:"ts"`
Id uint32 `json:"id"`
Name string `json:"name"`
Data json.RawMessage `json:"data"`
@ -102,8 +102,8 @@ type BusEvent struct {
func (e *BusEvent) MarshalJSON() (b []byte, err error) {
// create the underlying raw event
j := &RawJsonEvent{
Timestamp: uint32(e.Timestamp.UnixMilli()),
Id: uint32(e.Id),
Timestamp: e.Timestamp.UnixMilli(),
Id: e.Id,
Name: e.Data.String(),
}
// now we use the magic Packet -> map[string]interface{} function
@ -125,7 +125,7 @@ func (e *BusEvent) UnmarshalJSON(b []byte) error {
return err
}
e.Timestamp = time.UnixMilli(int64(j.Timestamp))
e.Timestamp = time.UnixMilli(j.Timestamp)
e.Id = j.Id
e.Data, err = FromJson(j.Id, j.Data)