Skip to content

Commit 10546ab

Browse files
committed
Add utility for strongly typed unix timestamps in JSON
1 parent 4e03c91 commit 10546ab

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

bridge/status/bridgestate.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"maunium.net/go/mautrix/id"
19+
"maunium.net/go/mautrix/util/jsontime"
1920
)
2021

2122
type BridgeStateEvent string
@@ -47,7 +48,7 @@ const (
4748

4849
type BridgeState struct {
4950
StateEvent BridgeStateEvent `json:"state_event"`
50-
Timestamp int64 `json:"timestamp"`
51+
Timestamp jsontime.Unix `json:"timestamp"`
5152
TTL int `json:"ttl"`
5253

5354
Source string `json:"source,omitempty"`
@@ -80,7 +81,7 @@ func (pong BridgeState) Fill(user BridgeStateFiller) BridgeState {
8081
pong.RemoteName = user.GetRemoteName()
8182
}
8283

83-
pong.Timestamp = time.Now().Unix()
84+
pong.Timestamp = jsontime.UnixNow()
8485
pong.Source = "bridge"
8586
if len(pong.Error) > 0 {
8687
pong.TTL = 60
@@ -127,5 +128,5 @@ func (pong *BridgeState) ShouldDeduplicate(newPong *BridgeState) bool {
127128
if pong == nil || pong.StateEvent != newPong.StateEvent || pong.Error != newPong.Error {
128129
return false
129130
}
130-
return pong.Timestamp+int64(pong.TTL/5) > time.Now().Unix()
131+
return pong.Timestamp.Add(time.Duration(pong.TTL/5) * time.Second).After(time.Now())
131132
}

bridge/status/messagecheckpoint.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"maunium.net/go/mautrix"
1919
"maunium.net/go/mautrix/event"
2020
"maunium.net/go/mautrix/id"
21+
"maunium.net/go/mautrix/util/jsontime"
2122
)
2223

2324
type MessageCheckpointStep string
@@ -67,7 +68,7 @@ type MessageCheckpoint struct {
6768
EventID id.EventID `json:"event_id"`
6869
RoomID id.RoomID `json:"room_id"`
6970
Step MessageCheckpointStep `json:"step"`
70-
Timestamp int64 `json:"timestamp"`
71+
Timestamp jsontime.UnixMilli `json:"timestamp"`
7172
Status MessageCheckpointStatus `json:"status"`
7273
EventType event.Type `json:"event_type"`
7374
ReportedBy MessageCheckpointReportedBy `json:"reported_by"`
@@ -102,7 +103,7 @@ func NewMessageCheckpoint(evt *event.Event, step MessageCheckpointStep, status M
102103
EventID: evt.ID,
103104
RoomID: evt.RoomID,
104105
Step: step,
105-
Timestamp: time.Now().UnixNano() / int64(time.Millisecond),
106+
Timestamp: jsontime.UnixMilliNow(),
106107
Status: status,
107108
EventType: evt.Type,
108109
ReportedBy: MsgReportedByBridge,

util/jsontime/jsontime.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) 2022 Tulir Asokan
2+
//
3+
// This Source Code Form is subject to the terms of the Mozilla Public
4+
// License, v. 2.0. If a copy of the MPL was not distributed with this
5+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
7+
package jsontime
8+
9+
import (
10+
"encoding/json"
11+
"time"
12+
)
13+
14+
func UM(time time.Time) UnixMilli {
15+
return UnixMilli{Time: time}
16+
}
17+
18+
func UnixMilliNow() UnixMilli {
19+
return UM(time.Now())
20+
}
21+
22+
type UnixMilli struct {
23+
time.Time
24+
}
25+
26+
func (um UnixMilli) MarshalJSON() ([]byte, error) {
27+
if um.IsZero() {
28+
return []byte{'0'}, nil
29+
}
30+
return json.Marshal(um.UnixMilli())
31+
}
32+
33+
func (um *UnixMilli) UnmarshalJSON(data []byte) error {
34+
var val int64
35+
err := json.Unmarshal(data, &val)
36+
if err != nil {
37+
return err
38+
}
39+
if val == 0 {
40+
um.Time = time.Time{}
41+
} else {
42+
um.Time = time.UnixMilli(val)
43+
}
44+
return nil
45+
}
46+
47+
func U(time time.Time) Unix {
48+
return Unix{Time: time}
49+
}
50+
51+
func UnixNow() Unix {
52+
return U(time.Now())
53+
}
54+
55+
type Unix struct {
56+
time.Time
57+
}
58+
59+
func (u Unix) MarshalJSON() ([]byte, error) {
60+
if u.IsZero() {
61+
return []byte{'0'}, nil
62+
}
63+
return json.Marshal(u.Unix())
64+
}
65+
66+
func (u *Unix) UnmarshalJSON(data []byte) error {
67+
var val int64
68+
err := json.Unmarshal(data, &val)
69+
if err != nil {
70+
return err
71+
}
72+
if val == 0 {
73+
u.Time = time.Time{}
74+
} else {
75+
u.Time = time.Unix(val, 0)
76+
}
77+
return nil
78+
}

0 commit comments

Comments
 (0)