1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-23 01:24:30 +02:00
ferret/pkg/stdlib/datetime/daysinmonth.go
Tim Voronov 1af8b37a0f
New type system (#232)
* New type system

* Fixed dot notation for HTML elements
2019-02-13 12:31:18 -05:00

51 lines
1.1 KiB
Go

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
}