Skip to content

Commit

Permalink
Add date abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 3, 2019
1 parent 0a7b470 commit 1ead21a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions date/date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package date

import "time"

const layout = "2006-01-02"

type Date struct {
time time.Time
}

func Today() Date {
return From(time.Now())
}

func From(time time.Time) Date {
return Date{time: time}
}

func Parse(date string) (Date, error) {
t, err := time.Parse(layout, date)
if err != nil {
return Date{}, err
}

return From(t), nil
}

func (d Date) Add(years int, months int, days int) Date {
return Date{time: d.time.AddDate(years, months, days)}
}

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

0 comments on commit 1ead21a

Please sign in to comment.