/
timestamp.go
52 lines (46 loc) · 1.25 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
package types
import (
"fmt"
"time"
gogotypes "github.com/gogo/protobuf/types"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)
// GogoToProtobufTimestamp converts a gogo timestamp to a protobuf timestamp.
func GogoToProtobufTimestamp(ts *gogotypes.Timestamp) *timestamppb.Timestamp {
if ts == nil {
return nil
}
return ×tamppb.Timestamp{
Seconds: ts.Seconds,
Nanos: ts.Nanos,
}
}
// ProtobufToGogoTimestamp converts a protobuf timestamp to a gogo timestamp.
func ProtobufToGogoTimestamp(ts *timestamppb.Timestamp) *gogotypes.Timestamp {
if ts == nil {
return nil
}
return &gogotypes.Timestamp{
Seconds: ts.Seconds,
Nanos: ts.Nanos,
}
}
// GogoToProtobufDuration converts a gogo duration to a protobuf duration.
func GogoToProtobufDuration(d *gogotypes.Duration) *durationpb.Duration {
if d == nil {
return nil
}
return &durationpb.Duration{
Seconds: d.Seconds,
Nanos: d.Nanos,
}
}
// ParseDate parses a date using the format yyyy-mm-dd.
func ParseDate(field string, date string) (time.Time, error) {
t, err := time.Parse("2006-01-02", date)
if err != nil {
return t, fmt.Errorf("%s must have format yyyy-mm-dd, but received %v", field, date)
}
return t, nil
}