Skip to content

Commit 1ead21a

Browse files
author
Ulrich Lissé
committed
Add date abstraction
1 parent 0a7b470 commit 1ead21a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

date/date.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package date
2+
3+
import "time"
4+
5+
const layout = "2006-01-02"
6+
7+
type Date struct {
8+
time time.Time
9+
}
10+
11+
func Today() Date {
12+
return From(time.Now())
13+
}
14+
15+
func From(time time.Time) Date {
16+
return Date{time: time}
17+
}
18+
19+
func Parse(date string) (Date, error) {
20+
t, err := time.Parse(layout, date)
21+
if err != nil {
22+
return Date{}, err
23+
}
24+
25+
return From(t), nil
26+
}
27+
28+
func (d Date) Add(years int, months int, days int) Date {
29+
return Date{time: d.time.AddDate(years, months, days)}
30+
}
31+
32+
func (d Date) String() string {
33+
return d.time.Format(layout)
34+
}

0 commit comments

Comments
 (0)