-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtimestamp.go
More file actions
84 lines (69 loc) · 2.03 KB
/
timestamp.go
File metadata and controls
84 lines (69 loc) · 2.03 KB
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
package data
import (
"time"
)
// Timestamp is a date time information having microsecond-precision.
// It can be assigned to Value. It may have nanosecond values but they're
// usually ignored.
type Timestamp time.Time
// Type returns TypeID of Timestamp. It's always TypeTimestamp.
func (t Timestamp) Type() TypeID {
return TypeTimestamp
}
func (t Timestamp) asBool() (bool, error) {
return false, castError(t.Type(), TypeBool)
}
func (t Timestamp) asInt() (int64, error) {
return 0, castError(t.Type(), TypeInt)
}
func (t Timestamp) asFloat() (float64, error) {
return 0, castError(t.Type(), TypeFloat)
}
func (t Timestamp) asString() (string, error) {
return "", castError(t.Type(), TypeString)
}
func (t Timestamp) asBlob() ([]byte, error) {
return nil, castError(t.Type(), TypeBlob)
}
func (t Timestamp) asTimestamp() (time.Time, error) {
return time.Time(t), nil
}
func (t Timestamp) asArray() (Array, error) {
return nil, castError(t.Type(), TypeArray)
}
func (t Timestamp) asMap() (Map, error) {
return nil, castError(t.Type(), TypeMap)
}
func (t Timestamp) clone() Value {
return Timestamp(t)
}
// MarshalJSON marshals a Timestamp to JSON. A Timestamp is encoded as a string
// in RFC3339Nano format.
func (t Timestamp) MarshalJSON() ([]byte, error) {
// the JSON serialization is defined via the String()
// return value as defined below
return []byte(t.String()), nil
}
// UnmarshalJSON reconstructs a Timestamp from JSON. It first tries to parse
// a string in RFC3339Nano format. When it fails, then it tries again with
// RFC3339 format.
func (t *Timestamp) UnmarshalJSON(data []byte) error {
str := string(data)
ts, err := time.Parse(`"`+time.RFC3339Nano+`"`, str)
if err == nil {
*t = Timestamp(ts)
return nil
}
ts, err = time.Parse(`"`+time.RFC3339+`"`, str)
if err == nil {
*t = Timestamp(ts)
return nil
}
return err
}
// String returns JSON representation of a Timestamp. A Timestamp is encoded as
// a string in RFC3339Nano format.
func (t Timestamp) String() string {
s, _ := ToString(t)
return `"` + s + `"`
}