|
| 1 | +package store |
| 2 | + |
| 3 | +import "time" |
| 4 | + |
| 5 | +// Date represents a single date without any information about the Clock. |
| 6 | +type Date struct { |
| 7 | + Year int |
| 8 | + Month time.Month |
| 9 | + Day int |
| 10 | +} |
| 11 | + |
| 12 | +// Time returns the time.Time that represents this date, with Clock information provided. |
| 13 | +func (dt Date) Time(c Clock) time.Time { |
| 14 | + return time.Date( |
| 15 | + dt.Year, |
| 16 | + dt.Month, |
| 17 | + dt.Day, |
| 18 | + c.Hour(), |
| 19 | + c.Minute(), |
| 20 | + c.Second(), |
| 21 | + c.Nanosecond(), |
| 22 | + c.Location(), |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +// After checks that the current date is after the other date. |
| 27 | +func (dt Date) After(other Date) bool { |
| 28 | + if dt.Year == other.Year { |
| 29 | + if dt.Month == other.Month { |
| 30 | + return dt.Day > other.Day |
| 31 | + } |
| 32 | + return dt.Month > other.Month |
| 33 | + } |
| 34 | + return dt.Year > other.Year |
| 35 | +} |
| 36 | + |
| 37 | +// Before checks that the current date is before the given date. |
| 38 | +func (dt Date) Before(other Date) bool { |
| 39 | + if dt.Year == other.Year { |
| 40 | + if dt.Month == other.Month { |
| 41 | + return dt.Day < other.Day |
| 42 | + } |
| 43 | + return dt.Month < other.Month |
| 44 | + } |
| 45 | + return dt.Year < other.Year |
| 46 | +} |
| 47 | + |
| 48 | +// BeforeOrEqual checks that the current date is before or equal the other date. |
| 49 | +func (dt Date) BeforeOrEqual(other Date) bool { |
| 50 | + return dt.Before(other) || dt.Equal(other) |
| 51 | +} |
| 52 | + |
| 53 | +// AfterOrEqual checks that the current date is after or equal the other date. |
| 54 | +func (dt Date) AfterOrEqual(other Date) bool { |
| 55 | + return dt.After(other) || dt.Equal(other) |
| 56 | +} |
| 57 | + |
| 58 | +// Equal returns true if the dates are the same. |
| 59 | +func (dt Date) Equal(other Date) bool { |
| 60 | + return dt.Year == other.Year && dt.Month == other.Month && dt.Day == other.Day |
| 61 | +} |
| 62 | + |
| 63 | +// Add some time to the current date. |
| 64 | +func (dt Date) Add(y int, m int, d int) Date { |
| 65 | + return DateFromTime(time.Date( |
| 66 | + dt.Year+y, dt.Month+time.Month(m), dt.Day+d, |
| 67 | + 0, 0, 0, 0, time.UTC)) |
| 68 | +} |
| 69 | + |
| 70 | +// DateFromTime returns the Date extracted from the given time.Time |
| 71 | +func DateFromTime(t time.Time) Date { |
| 72 | + y, m, d := t.Date() |
| 73 | + return Date{Year: y, Month: m, Day: d} |
| 74 | +} |
0 commit comments