forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
111 lines (99 loc) · 2.53 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package ravendb
import (
"encoding/json"
"strings"
"time"
)
const (
// time format returned by the server which looks like:
// 2018-05-08T05:20:31.5233900Z or
// 2018-07-29T23:50:57.9998240 or
// 2018-08-16T13:56:59.355664-07:00
timeFormat = "2006-01-02T15:04:05.9999999Z"
timeFormat2 = "2006-01-02T15:04:05.9999999"
timeFormat3 = "2006-01-02T15:04:05.9999999-07:00"
)
// Time is an alias for time.Time that serializes/deserializes in ways
// compatible with Ravendb server
type Time time.Time
// Format formats time in a way that RavenDB server understands.
// RavenDB is strict enough that a single format can't
// produce valid string values.
func (t Time) Format() string {
s := time.Time(t).Format(timeFormat)
// ravendb server only accepts 7 digits for fraction part but Go's
// formatting might remove trailing zeros, producing 6 digits
dotIdx := strings.LastIndexByte(s, '.')
if dotIdx == -1 {
s = s[:len(s)-1] // remove 'Z'
s = s + ".0000000Z"
} else {
nToAdd := 9 - (len(s) - dotIdx) // 9: 7 + 1 for 'Z' and 1 for '.'
if nToAdd > 0 {
s = s[:len(s)-1] // remove 'Z'
for ; nToAdd > 0; nToAdd-- {
s = s + "0"
}
s = s + "Z"
}
}
return s
}
// ParseTime parses string time value returned by RavenDB server
// The value can't be parsed with a single string format
func ParseTime(s string) (time.Time, error) {
tt, err := time.Parse(timeFormat, s)
if err != nil {
tt, err = time.Parse(timeFormat2, s)
if err != nil {
tt, err = time.Parse(timeFormat3, s)
}
}
return tt, err
}
func (t Time) MarshalJSON() ([]byte, error) {
s := t.Format()
return []byte(`"` + s + `"`), nil
}
func (t *Time) UnmarshalJSON(d []byte) error {
s := string(d)
s = strings.TrimLeft(s, `"`)
s = strings.TrimRight(s, `"`)
if s == "null" {
return nil
}
tt, err := ParseTime(s)
*t = Time(tt)
if err != nil {
// TODO: for now make it a fatal error to catch bugs early
must(err)
return err
}
return nil
}
func (t *Time) toTime() time.Time {
// for convenience make it work on nil pointer
if t == nil {
return time.Time{}
}
return time.Time(*t)
}
func (t *Time) toTimePtr() *time.Time {
if t == nil {
return nil
}
res := time.Time(*t)
return &res
}
// RoundToServerTime rounds t to the same precision as round-tripping
// to the server and back. Useful for comparing time.Time values for
// equality with values returned by the server
func RoundToServerTime(t time.Time) time.Time {
st := Time(t)
d, err := json.Marshal(st)
must(err)
var res Time
err = json.Unmarshal(d, &res)
must(err)
return time.Time(res)
}