/
time.go
36 lines (31 loc) · 911 Bytes
/
time.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
package bunq
import (
"fmt"
"strings"
"time"
)
// Time represents a parsed timestamp returned by the bunq API.
type Time time.Time
// UnmarshalJSON is being used to parse timestamps from the bunq API.
// Bunq timestamps come into this format: "2015-06-13 23:19:16.215235"
func (ariTime *Time) UnmarshalJSON(input []byte) error {
strInput := string(input)
strInput = strings.Trim(strInput, `"`)
var pattern string
if len(strInput) == 10 {
pattern = "2006-01-02"
} else {
pattern = "2006-01-02 15:04:05.000000"
}
newTime, err := time.Parse(pattern, strInput)
if err != nil {
return fmt.Errorf("Error parsing Time: %s", err)
}
*ariTime = Time(newTime)
return nil
}
// MarshalText implements the encoding.MarshalText interface for custom time type.
func (ariTime *Time) MarshalText() ([]byte, error) {
t := time.Time(*ariTime)
return []byte(t.Format("2006-01-02 15:04:05.000000")), nil
}