github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/datetime/quarter.go (about)

     1  package datetime
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // DATE_QUARTER returns which quarter date belongs to.
    13  // @param {DateTime} date - Source DateTime.
    14  // @return {Int} - A quarter number.
    15  func DateQuarter(_ context.Context, args ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(args, 1, 1)
    17  	if err != nil {
    18  		return values.None, err
    19  	}
    20  
    21  	err = core.ValidateType(args[0], types.DateTime)
    22  	if err != nil {
    23  		return values.None, err
    24  	}
    25  
    26  	month := args[0].(values.DateTime).Month()
    27  	quarter := values.NewInt(1)
    28  
    29  	switch month {
    30  	case time.April, time.May, time.June:
    31  		quarter = values.NewInt(2)
    32  	case time.July, time.August, time.September:
    33  		quarter = values.NewInt(3)
    34  	case time.October, time.November, time.December:
    35  		quarter = values.NewInt(4)
    36  	}
    37  
    38  	return quarter, nil
    39  }