camper/pkg/form/month.go

33 lines
676 B
Go

package form
import (
"fmt"
"net/http"
"strconv"
"time"
)
type Month struct {
Name string
Year int
Month time.Month
Error error
}
func (input *Month) FillValue(r *http.Request) {
if year, err := strconv.Atoi(r.FormValue(input.Name + ".year")); err == nil {
input.Year = year
} else {
fmt.Println(err, r.FormValue(input.Name+".year"))
}
if month, err := strconv.Atoi(r.FormValue(input.Name + ".month")); err == nil && month > 0 && month < 13 {
input.Month = time.Month(month)
} else {
fmt.Println(err, r.FormValue(input.Name+".month"))
}
}
func (input *Month) Date() time.Time {
return time.Date(input.Year, input.Month, 1, 0, 0, 0, 0, time.UTC)
}