Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/time.go
2497 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package db
6
7
import (
8
"database/sql/driver"
9
"fmt"
10
"time"
11
12
"github.com/relvacode/iso8601"
13
"google.golang.org/protobuf/types/known/timestamppb"
14
)
15
16
func NewVarCharTime(t time.Time) VarcharTime {
17
return VarcharTime{
18
t: t.UTC(),
19
valid: true,
20
}
21
}
22
23
func NewVarCharTimeFromStr(s string) (VarcharTime, error) {
24
var vt VarcharTime
25
err := vt.Scan(s)
26
return vt, err
27
}
28
29
// VarcharTime exists for cases where records are inserted into the DB as VARCHAR but actually contain a timestamp which is time.RFC3339
30
type VarcharTime struct {
31
t time.Time
32
valid bool
33
}
34
35
// Scan implements the Scanner interface.
36
func (n *VarcharTime) Scan(value interface{}) error {
37
if value == nil {
38
n.valid = false
39
return nil
40
}
41
42
switch s := value.(type) {
43
case []uint8:
44
return n.parseString(string(s))
45
case string:
46
return n.parseString(s)
47
}
48
return fmt.Errorf("unknown scan value for VarcharTime with value: %v", value)
49
}
50
51
func (n *VarcharTime) parseString(s string) error {
52
// Null value - empty string mean value is not set
53
if len(s) == 0 {
54
n.valid = false
55
return nil
56
}
57
58
parsed, err := iso8601.ParseString(s)
59
if err != nil {
60
return fmt.Errorf("failed to parse %v into ISO8601: %w", s, err)
61
}
62
63
if parsed.UTC().IsZero() {
64
n.t = time.Time{}.UTC()
65
n.valid = false
66
return nil
67
}
68
69
n.valid = true
70
n.t = parsed.UTC()
71
return nil
72
}
73
74
func (n VarcharTime) Time() time.Time {
75
return n.t
76
}
77
78
func (n VarcharTime) IsSet() bool {
79
return n.valid
80
}
81
82
// Value implements the driver Valuer interface.
83
func (n VarcharTime) Value() (driver.Value, error) {
84
if n.IsSet() {
85
return TimeToISO8601(n.t), nil
86
}
87
return "", nil
88
}
89
90
func (n VarcharTime) String() string {
91
if n.IsSet() {
92
return TimeToISO8601(n.t)
93
}
94
return ""
95
}
96
97
var null = "null"
98
99
func (n VarcharTime) MarshalJSON() ([]byte, error) {
100
if !n.IsSet() {
101
return []byte(null), nil
102
}
103
104
return n.Time().UTC().MarshalJSON()
105
}
106
107
func (n *VarcharTime) UnmarshalJSON(data []byte) error {
108
if string(data) == null {
109
n.valid = false
110
return nil
111
}
112
113
t := time.Time{}
114
if err := t.UnmarshalJSON(data); err != nil {
115
return fmt.Errorf("failed to unmarshal VarcharTime %s: %w", string(data), err)
116
}
117
118
if t.IsZero() {
119
return nil
120
}
121
122
n.valid = true
123
n.t = t.UTC()
124
125
return nil
126
}
127
128
const ISO8601Format = "2006-01-02T15:04:05.000Z"
129
130
func TimeToISO8601(t time.Time) string {
131
return t.UTC().Format(ISO8601Format)
132
}
133
134
func VarcharTimeToTimestamppb(t VarcharTime) *timestamppb.Timestamp {
135
if !t.IsSet() {
136
return nil
137
}
138
139
return timestamppb.New(t.Time())
140
}
141
142