forked from SoCloz/goprismic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
37 lines (30 loc) · 719 Bytes
/
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
package fragment
import (
"fmt"
"reflect"
"time"
"github.com/anilamilineni/goprismic/fragment/link"
)
// A date fragment (YYYY-MM-DD)
type Date time.Time
func (d *Date) Decode(_ string, enc interface{}) error {
dec, ok := enc.(string)
if !ok {
return fmt.Errorf("unable to decode date fragment : %+v is a %s, not a string", enc, reflect.TypeOf(enc))
}
date, err := time.Parse("2006-01-02", dec)
if err != nil {
return err
}
*d = Date(date)
return nil
}
func (d *Date) AsText() string {
t := time.Time(*d)
return t.Format("2006-01-02")
}
func (d *Date) AsHtml() string {
t := time.Time(*d)
return "<time>" + t.Format("2006-01-02") + "</time>"
}
func (d *Date) ResolveLinks(_ link.Resolver) {}