-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
94 lines (83 loc) · 2.91 KB
/
date.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
package dynamocity
import (
"fmt"
"reflect"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// Date represents a sortable Date with fixed date precision.
//
// Date implements attributevalue.Marshaler specifically for "YYYY-MM-DD"
// format which does not permit any timestamp; however, it can unmarshall from any
// Timestamp with nanosecond precision.
type Date time.Time
// MarshalDynamoDBAttributeValue implements the attributevalue.Marshaler interface to marshal
// a dynamocity.Date into a DynamoDB AttributeValue string value with specific second precision
func (t Date) MarshalDynamoDBAttributeValue() (types.AttributeValue, error) {
rfcTime := time.Time(t).Format(StrictDateFmt)
return &types.AttributeValueMemberS{
Value: rfcTime,
}, nil
}
// UnmarshalDynamoDBAttributeValue implements the attributevalue.Unmarshaler interface to unmarshal
// a attributevalue.AttributeValue into a dynamocity.Date. This unmarshal is flexible and supports any timestamp
// with nanosecond precision
func (t *Date) UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error {
tv, ok := av.(*types.AttributeValueMemberS)
if !ok {
return &attributevalue.UnmarshalTypeError{
Value: fmt.Sprintf("%T", av),
Type: reflect.TypeOf((*MillisTime)(nil)),
}
}
date, err := parse(tv.Value)
if err != nil {
return err
}
*t = Date(date)
return nil
}
// parse is a helper function to assist with parsing a string to a time.Time.
//
// This function will attempt to parse using dynamocity.FlexibleNanoFmt, and if that fails
// dynamocity.StrictDateFmt
func parse(str string) (time.Time, error) {
parsedTime, err := time.Parse(FlexibleNanoFmt, str)
if err == nil {
return parsedTime, nil
}
return time.Parse(StrictDateFmt, str)
}
// Time is a handler func to return an instance of dynamocity.Date as time.Time
func (t Date) Time() time.Time {
return time.Time(t)
}
// String implements the fmt.Stringer interface to supply a native String representation for a value in time.RFC3339
// Format with second precision
func (t Date) String() string {
return t.Time().Format(StrictDateFmt)
}
// UnmarshalJSON implements the json.Unmarshaler interface to unmarshal a date or RFC3339 timestamp
func (t *Date) UnmarshalJSON(b []byte) error {
str, err := strconv.Unquote(string(b))
if err != nil {
return err
}
parsedTime, err := parse(str)
if err != nil {
return fmt.Errorf("Timestamp '%s' cannot be unmarshalled", str)
}
*t = Date(parsedTime)
return nil
}
// MarshalJSON implements the json.Marshaler interface to marshal RFC3339 timestamps with second precision
func (t Date) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(t.String())), nil
}
// ParseDate will attempt to parse any RFC3339 Timestamp or date with format YYYY-MM-DD to a dynamocity.Date
func ParseDate(str string) (Date, error) {
time, err := parse(str)
return Date(time), err
}