-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ulrich Lissé
committed
Apr 3, 2019
1 parent
0a7b470
commit 1ead21a
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |