1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/stdlib/datetime/quarter.go

39 lines
889 B
Go

package datetime
import (
"context"
"time"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
// DateQuarter returns which quarter date belongs to.
// @params date (DateTime) - source DateTime.
// @return (Int) - a quarter number.
func DateQuarter(_ 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], core.DateTimeType)
if err != nil {
return values.None, err
}
month := args[0].(values.DateTime).Month()
quarter := values.NewInt(1)
switch month {
case time.April, time.May, time.June:
quarter = values.NewInt(2)
case time.July, time.August, time.September:
quarter = values.NewInt(3)
case time.October, time.November, time.December:
quarter = values.NewInt(4)
}
return quarter, nil
}