-
Notifications
You must be signed in to change notification settings - Fork 3
/
format.go
46 lines (41 loc) · 1.26 KB
/
format.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
package tinytime
import (
"fmt"
"time"
)
// Parse a layout into a TinyTime
func Parse(layout, value string) (TinyTime, error) {
t, err := time.Parse(layout, value)
if err != nil {
return TinyTime{}, fmt.Errorf("tinytime Parse: %v", err)
}
tt, err := FromTime(t)
if err != nil {
return TinyTime{}, fmt.Errorf("tinytime Parse: %v", err)
}
return tt, nil
}
// ParseInLocation parses a layout into a TinyTime including a location.
// The input is converted into UTC
func ParseInLocation(layout, value string, loc *time.Location) (TinyTime, error) {
t, err := time.ParseInLocation(layout, value, loc)
if err != nil {
return TinyTime{}, fmt.Errorf("tinytime ParseInLocation: %v", err)
}
tt, err := FromTime(t)
if err != nil {
return TinyTime{}, fmt.Errorf("tinytime ParseInLocation: %v", err)
}
return tt, nil
}
// AppendFormat is like Format but appends the textual representation to b and returns the extended buffer
func (tt TinyTime) AppendFormat(b []byte, layout string) []byte {
t := tt.ToTime()
return t.AppendFormat(b, layout)
}
// Format returns a formatted time, as specified by the standard time library
// https://golang.org/src/time/format.go?s=16029:16071#L485
func (tt TinyTime) Format(layout string) string {
t := tt.ToTime()
return t.Format(layout)
}