github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/datetime/format.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_FORMAT format date according to the given format string.
    12  // @param {DateTime} date - Source DateTime object.
    13  // @param {String} format - String format.
    14  // @return {String} - Formatted date.
    15  func DateFormat(_ context.Context, args ...core.Value) (core.Value, error) {
    16  	err := core.ValidateArgs(args, 2, 2)
    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  	err = core.ValidateType(args[1], types.String)
    27  	if err != nil {
    28  		return values.None, err
    29  	}
    30  
    31  	date := args[0].(values.DateTime)
    32  	format := args[1].(values.String).String()
    33  
    34  	return values.NewString(date.Format(format)), nil
    35  }