-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtime.go
32 lines (27 loc) · 787 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
package ari
import (
"fmt"
"strings"
"time"
)
const timeFormat = "2006-01-02T15:04:05.999-0700"
// Time is a type alias for time.Time with custom marshaling
type Time time.Time
// UnmarshalJSON unmarshals the JSON input
func (j *Time) UnmarshalJSON(input []byte) error {
// ARI stamps in this format: "2014-10-30T06:04:39.113+0000"
strInput := string(input)
strInput = strings.Trim(strInput, `"`)
newTime, err := time.Parse(timeFormat, strInput)
if err != nil {
//fmt.Printf(" - ERROR PARSING ARITIME: %s - ", err)
return fmt.Errorf("Error parsing Time: %s", err)
}
*j = Time(newTime)
return nil
}
// FIXME: This doesn't work to improve "pretty.Formatter"
func (j *Time) MarshalText() ([]byte, error) {
t := time.Time(*j)
return []byte(t.Format(timeFormat)), nil
}