1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/stdlib/datetime/daysinmonth.go

51 lines
1.1 KiB
Go
Raw Normal View History

package datetime
import (
"context"
"time"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
var daysCount = map[time.Month]int{
time.January: 31,
time.February: 28,
time.March: 31,
time.April: 30,
time.May: 31,
time.June: 30,
time.July: 30,
time.August: 31,
time.September: 30,
time.October: 31,
time.November: 30,
time.December: 31,
}
// DateDaysInMonth returns the number of days in the month of date.
// @params date (DateTime) - source DateTime.
// @return (Int) - number of the days.
func DateDaysInMonth(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 1)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], types.DateTime)
if err != nil {
return values.None, err
}
dt := args[0].(values.DateTime)
month := dt.Month()
count := daysCount[month]
if month == time.February && isLeap(dt.Year()) {
count++
}
return values.NewInt(count), nil
}