-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.go
38 lines (32 loc) · 901 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
38
package timehelper
import (
"time"
)
// Date combines year, month and day.
type Date struct {
Year int
Month time.Month
Day int
}
// DateFromTime extracts Date part from 't'.
func DateFromTime(t time.Time) Date {
return Date{
Year: t.Year(),
Month: t.Month(),
Day: t.Day(),
}
}
// Valid reports whether 'd' represents a valid date.
func (d Date) Valid() error {
return ValidYMD(d.Year, d.Month, d.Day)
}
// Time returns [time.Time] corresponding to 'd'.
// The returned value has zero clock and [time.UTC] time zone.
func (d Date) Time() time.Time {
return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, time.UTC)
}
// TimeClock returns [time.Time] corresponding to 'd' + 'c'.
// The returned value has zero nanoseconds and [time.UTC] time zone.
func (d Date) TimeClock(c Clock) time.Time {
return time.Date(d.Year, d.Month, d.Day, c.Hour, c.Minute, c.Second, 0, time.UTC)
}