summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gopackage/ddp/time.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2021-10-17 00:47:22 +0200
committerGitHub <noreply@github.com>2021-10-17 00:47:22 +0200
commit4dd8bae5c91fa4aef09d865d8fef1acd84f90925 (patch)
treeffad9b242daccaf8c86d1c1fbd59032302bd3be9 /vendor/github.com/gopackage/ddp/time.go
parent7ae45c42e712bd0e66c101f3f714c05aa1dc2104 (diff)
downloadmatterbridge-msglm-4dd8bae5c91fa4aef09d865d8fef1acd84f90925.tar.gz
matterbridge-msglm-4dd8bae5c91fa4aef09d865d8fef1acd84f90925.tar.bz2
matterbridge-msglm-4dd8bae5c91fa4aef09d865d8fef1acd84f90925.zip
Update dependencies (#1610)
* Update dependencies * Update module to go 1.17
Diffstat (limited to 'vendor/github.com/gopackage/ddp/time.go')
-rw-r--r--vendor/github.com/gopackage/ddp/time.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/vendor/github.com/gopackage/ddp/time.go b/vendor/github.com/gopackage/ddp/time.go
new file mode 100644
index 00000000..584f9ce0
--- /dev/null
+++ b/vendor/github.com/gopackage/ddp/time.go
@@ -0,0 +1,49 @@
+package ddp
+
+import (
+ "encoding/json"
+ "io"
+ "strconv"
+ "time"
+)
+
+// utcOffset in milliseconds for the current local time (east of UTC).
+var utcOffset int64
+
+func init() {
+ _, offsetSeconds := time.Now().Zone()
+ utcOffset = int64(offsetSeconds * 1000)
+}
+
+// Time is an alias for time.Time with custom json marshalling implementations to support ejson.
+type Time struct {
+ time.Time
+}
+
+// UnixMilli creates a new Time from the given unix millis but in UTC (as opposed to time.UnixMilli which returns
+// time in the local time zone). This supports the proper loading of times from EJSON $date objects.
+func UnixMilli(i int64) Time {
+ return Time{Time: time.UnixMilli(i - utcOffset)}
+}
+
+func (t *Time) UnmarshalJSON(b []byte) error {
+ var data map[string]float64
+ err := json.Unmarshal(b, &data)
+ if err != nil {
+ return err
+ }
+ val, ok := data["$date"]
+ if !ok {
+ return io.ErrUnexpectedEOF
+ }
+ // The time MUST be UTC but time.UnixMilli uses local time.
+ // We see what time it is in local time and calculate the offset to UTC
+ *t = UnixMilli(int64(val))
+
+ return nil
+}
+
+func (t Time) MarshalJSON() ([]byte, error) {
+ return []byte("{\"$date\":" + strconv.FormatInt(t.UnixMilli(), 10) + "}"), nil
+}
+