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

     1  package datetime
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
     9  )
    10  
    11  // DATE_LEAPYEAR returns true if date is in a leap year else false.
    12  // @param {DateTime} date - Source DateTime.
    13  // @return {Boolean} - Date is in a leap year.
    14  func DateLeapYear(_ context.Context, args ...core.Value) (core.Value, error) {
    15  	err := core.ValidateArgs(args, 1, 1)
    16  	if err != nil {
    17  		return values.None, err
    18  	}
    19  
    20  	err = core.ValidateType(args[0], types.DateTime)
    21  	if err != nil {
    22  		return values.None, err
    23  	}
    24  
    25  	year := args[0].(values.DateTime).Year()
    26  
    27  	return values.NewBoolean(isLeap(year)), nil
    28  }
    29  
    30  func isLeap(year int) bool {
    31  	return year%4 == 0 && (year%100 != 0 || year%400 == 0)
    32  }