github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/datetime/daysinmonth.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  var daysCount = map[time.Month]int{
    13  	time.January:   31,
    14  	time.February:  28,
    15  	time.March:     31,
    16  	time.April:     30,
    17  	time.May:       31,
    18  	time.June:      30,
    19  	time.July:      30,
    20  	time.August:    31,
    21  	time.September: 30,
    22  	time.October:   31,
    23  	time.November:  30,
    24  	time.December:  31,
    25  }
    26  
    27  // DATE_DAYS_IN_MONTH returns the number of days in the month of date.
    28  // @param {DateTime} date - Source DateTime.
    29  // @return {Int} - Number of the days.
    30  func DateDaysInMonth(_ context.Context, args ...core.Value) (core.Value, error) {
    31  	err := core.ValidateArgs(args, 1, 1)
    32  	if err != nil {
    33  		return values.None, err
    34  	}
    35  
    36  	err = core.ValidateType(args[0], types.DateTime)
    37  	if err != nil {
    38  		return values.None, err
    39  	}
    40  
    41  	dt := args[0].(values.DateTime)
    42  	month := dt.Month()
    43  	count := daysCount[month]
    44  
    45  	if month == time.February && isLeap(dt.Year()) {
    46  		count++
    47  	}
    48  
    49  	return values.NewInt(count), nil
    50  }