2018-11-07 20:52:25 +02:00
|
|
|
package datetime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sliceDateTime = []core.Type{core.DateTimeType}
|
|
|
|
sliceIntType = []core.Type{core.IntType}
|
|
|
|
sliceStringType = []core.Type{core.StringType}
|
|
|
|
|
|
|
|
emptyDateTime values.DateTime
|
|
|
|
emptyInt values.Int
|
|
|
|
emptyString values.String
|
|
|
|
)
|
|
|
|
|
|
|
|
// DateAdd add amount given in unit to date.
|
|
|
|
// @params date (DateTime) - source date.
|
|
|
|
// @params amount (Int) - amount of units
|
|
|
|
// @params unit (String) - unit.
|
|
|
|
// @return (DateTime) - calculated date.
|
|
|
|
// The following units are available:
|
|
|
|
// * y, year, year
|
|
|
|
// * m, month, months
|
|
|
|
// * w, week, weeks
|
|
|
|
// * d, day, days
|
|
|
|
// * h, hour, hours
|
|
|
|
// * i, minute, minutes
|
|
|
|
// * s, second, seconds
|
|
|
|
// * f, millisecond, milliseconds
|
|
|
|
func DateAdd(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
date, amount, unit, err := getArgs(args)
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:49:34 +02:00
|
|
|
u, err := UnitFromString(unit.String())
|
2018-11-07 20:52:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:49:34 +02:00
|
|
|
tm := AddUnit(date.Time, int(amount), u)
|
|
|
|
|
|
|
|
return values.NewDateTime(tm), nil
|
2018-11-07 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DateSubtract subtract amount given in unit to date.
|
|
|
|
// @params date (DateTime) - source date.
|
|
|
|
// @params amount (Int) - amount of units
|
|
|
|
// @params unit (String) - unit.
|
|
|
|
// @return (DateTime) - calculated date.
|
|
|
|
// The following units are available:
|
|
|
|
// * y, year, year
|
|
|
|
// * m, month, months
|
|
|
|
// * w, week, weeks
|
|
|
|
// * d, day, days
|
|
|
|
// * h, hour, hours
|
|
|
|
// * i, minute, minutes
|
|
|
|
// * s, second, seconds
|
|
|
|
// * f, millisecond, milliseconds
|
|
|
|
func DateSubtract(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
date, amount, unit, err := getArgs(args)
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:49:34 +02:00
|
|
|
u, err := UnitFromString(unit.String())
|
2018-11-07 20:52:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:49:34 +02:00
|
|
|
tm := AddUnit(date.Time, -1*int(amount), u)
|
|
|
|
|
|
|
|
return values.NewDateTime(tm), nil
|
2018-11-07 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getArgs(args []core.Value) (values.DateTime, values.Int, values.String, error) {
|
|
|
|
err := core.ValidateArgs(args, 3, 3)
|
|
|
|
if err != nil {
|
|
|
|
return emptyDateTime, emptyInt, emptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = core.ValidateValueTypePairs(
|
|
|
|
core.PairValueType{Value: args[0], Types: sliceDateTime},
|
|
|
|
core.PairValueType{Value: args[1], Types: sliceIntType},
|
|
|
|
core.PairValueType{Value: args[2], Types: sliceStringType},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return emptyDateTime, emptyInt, emptyString, err
|
|
|
|
}
|
|
|
|
|
|
|
|
date := args[0].(values.DateTime)
|
|
|
|
amount := args[1].(values.Int)
|
|
|
|
unit := args[2].(values.String)
|
|
|
|
|
|
|
|
return date, amount, unit, nil
|
|
|
|
}
|