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