aboutsummaryrefslogtreecommitdiffstats
path: root/date.go
blob: c82769ff04cf3f9f5907246becb940b402be0f38 (plain) (blame)
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
package lulu

import (
	"fmt"
	"time"
)

type Date time.Time

func ParseDate(s string) (Date, error) {
	t, err := time.Parse(time.DateOnly, s)
	return Date(t), err
}

func MustParseDate(s string) Date {
	d, err := ParseDate(s)
	if err != nil {
		panic(fmt.Sprintf("lulu.ParseDate(%q): %v", s, err))
	}
	return d
}

func (d Date) String() string {
	return time.Time(d).Format(time.DateOnly)
}

func (d *Date) UnmarshalText(text []byte) error {
	nd, err := ParseDate(string(text))
	if err != nil {
		return err
	}
	*d = nd
	return nil
}

func (d Date) MarshalText() ([]byte, error) {
	s := time.Time(d).Format(time.DateOnly)
	return []byte(s), nil
}