-
Notifications
You must be signed in to change notification settings - Fork 6
/
timestamp.go
164 lines (149 loc) · 3.54 KB
/
timestamp.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ast
import (
"fmt"
"math"
"math/big"
"time"
"github.com/dipdup-net/go-lib/tools/base"
"github.com/dipdup-net/go-lib/tools/consts"
"github.com/dipdup-net/go-lib/tools/types"
"github.com/pkg/errors"
)
//
// Timestamp
//
// Timestamp -
type Timestamp struct {
Default
}
// NewTimestamp -
func NewTimestamp(depth int) *Timestamp {
return &Timestamp{
Default: NewDefault(consts.TIMESTAMP, 0, depth),
}
}
// ParseValue -
func (t *Timestamp) ParseValue(node *base.Node) error {
switch {
case node.IntValue != nil:
t.parseTimestamp(node.IntValue.Int64())
case node.StringValue != nil:
utc, err := time.Parse(time.RFC3339, *node.StringValue)
if err != nil {
if *node.StringValue == "" {
t.Value = time.Unix(0, 0).UTC()
} else if bi, ok := big.NewInt(0).SetString(*node.StringValue, 10); ok {
t.parseTimestamp(bi.Int64())
}
} else {
t.Value = utc.UTC()
}
}
return nil
}
func (t *Timestamp) parseTimestamp(ts int64) {
switch {
case ts < 0: // integer overflow
t.Value = time.Unix(math.MaxInt64, 0).UTC()
case 253402300799 > ts: // 31 December 9999 23:59:59 Golang time restriction
t.Value = time.Unix(ts, 0).UTC()
default:
t.Value = time.Unix(ts/1000, 0).UTC() // milliseconds
}
}
// ToBaseNode -
func (t *Timestamp) ToBaseNode(optimized bool) (*base.Node, error) {
switch ts := t.Value.(type) {
case time.Time:
if optimized {
val := types.NewBigInt(ts.UTC().Unix())
return toBaseNodeInt(val), nil
}
val := ts.UTC().Format(time.RFC3339)
return toBaseNodeString(val), nil
case string:
return toBaseNodeString(ts), nil
}
return nil, errors.Errorf("Invalid timestamp type")
}
// FromJSONSchema -
func (t *Timestamp) FromJSONSchema(data map[string]interface{}) error {
for key := range data {
if key == t.GetName() {
t.ValueKind = valueKindInt
switch val := data[key].(type) {
case string:
ts, err := time.Parse(time.RFC3339, val)
if err != nil {
return err
}
t.Value = types.NewBigInt(ts.UTC().Unix())
case float64:
t.Value = types.NewBigInt(int64(val))
}
break
}
}
return nil
}
// ToParameters -
func (t *Timestamp) ToParameters() ([]byte, error) {
switch ts := t.Value.(type) {
case time.Time:
return []byte(fmt.Sprintf(`{"int":"%d"}`, ts.UTC().Unix())), nil
case *types.BigInt:
return []byte(fmt.Sprintf(`{"int":"%d"}`, ts.Int64())), nil
default:
return nil, errors.Wrapf(consts.ErrInvalidType, "Timestamp.ToParameters: %T", t.Value)
}
}
// ToJSONSchema -
func (t *Timestamp) ToJSONSchema() (*JSONSchema, error) {
return wrapObject(&JSONSchema{
Prim: t.Prim,
Title: t.GetName(),
Type: JSONSchemaTypeString,
Format: "date-time",
Default: time.Now().UTC().Format(time.RFC3339),
}), nil
}
// Compare -
func (t *Timestamp) Compare(second Comparable) (int, error) {
secondItem, ok := second.(*Timestamp)
if !ok {
return 0, consts.ErrTypeIsNotComparable
}
ts := t.Value.(time.Time)
ts2 := secondItem.Value.(time.Time)
switch {
case ts.Equal(ts2):
return 0, nil
case ts.Before(ts2):
return -1, nil
default:
return 1, nil
}
}
// Distinguish -
func (t *Timestamp) Distinguish(x Distinguishable) (*MiguelNode, error) {
second, ok := x.(*Timestamp)
if !ok {
return nil, nil
}
return t.Default.Distinguish(&second.Default)
}
// GetJSONModel -
func (t *Timestamp) GetJSONModel(model JSONModel) {
if model == nil {
return
}
ts := t.Value.(time.Time)
model[t.GetName()] = ts.Format(time.RFC3339)
}
// FindByName -
func (t *Timestamp) FindByName(name string, isEntrypoint bool) Node {
if t.GetName() == name {
return t
}
return nil
}