budgeteer/server/month.go
Jan Bader 8fe91293e1
Some checks failed
continuous-integration/drone/push Build is failing
Add unittests for month
2022-04-06 22:16:50 +00:00

56 lines
897 B
Go

package server
import (
"fmt"
"time"
)
type Month struct {
Year int
Month int
}
func NewFromTime(date time.Time) Month {
return Month{date.Year(), int(date.Month())}
}
func (m Month) String() string {
return fmt.Sprintf("%d-%d", m.Year, m.Month)
}
func (m Month) FirstOfMonth() time.Time {
return time.Date(m.Year, time.Month(m.Month), 1, 0, 0, 0, 0, time.Now().Location())
}
func (m Month) InFuture(date time.Time) bool {
if m.Year < date.Year() {
return true
}
if m.Year > date.Year() {
return false
}
return time.Month(m.Month) < date.Month()
}
func (m Month) InPast(date time.Time) bool {
if m.Year > date.Year() {
return true
}
if m.Year < date.Year() {
return false
}
return time.Month(m.Month) > date.Month()
}
func (m Month) InPresent(date time.Time) bool {
if date.Year() != m.Year {
return false
}
return date.Month() == time.Month(m.Month)
}