-
Notifications
You must be signed in to change notification settings - Fork 48
Date Time Support
feel supports date, time, date and time and duration built-in functions for parsing date, time and duration. The built-in functions are used during parsing of the expression instead of building as date, time, date and time and duration are the keywords in FEEL grammar for defining DateTimeLiteral.
feel uses moment and moment-timezone to create a powerful date creation and manipulation engine. It creates a wrapper around the built-in methods while adding a lot of other custom methods for type information, ISO_8601 formatting and IANA timezone parsing. With moment under the hood, feel is able to process some really complex date, time and duration formats while keeping the API footprint convenient and simple. Date arithmetic, equality and comparison operations are made simple by incorporating DateTimeLiteral into existing feel grammar which makes date, time and duration just another literal.
- date("2012-12-25") + date("2012-12-24")
- time("T13:10:06") - time("T13:10:05")
- date and time("2012-12-24T23:59:00") + duration("P1Y")
- date("2012-12-25") > date("2012-12-24")
- date and time("2012-12-24T23:59:00") < date and time("2012-12-25T00:00:00")
- time("13:10:05@Etc/UTC").hour
- time("13:10:05@Etc/UTC").minute
- time("13:01:05+05:30").second
- date and time("2012-12-24T23:59:00").year
- date("2017-06-10").month
- date("2017-06-10").day
- duration("P13M").years
- duration("P1Y11M").months
- duration("P5DT12H10M").days
- duration("P5DT12H10M").hours
- duration("P5DT12H10M").minutes
- duration("P5DT12H10M25S").seconds
- date("2012-12-25") – date("2012-12-24") = duration("P1D")
- date and time("2012-12-24T23:59:00") + duration("PT1M") = date and time("2012-12-25T00:00:00")
- time("23:59:00z") + duration("PT2M") = time("00:01:00@Etc/UTC")
- date and time("2012-12-24T23:59:00") - date and time("2012-12-22T03:45:00") = duration("P2DT20H14M")
- duration("P2Y2M") = duration("P26M")
DMN specification lacks to mention any information on using current date and time. But to enhance usability, feel includes the notion of now. To create date, time and 'date and time' for the current date and time, just call the date, time and 'date and time' in-built functions with empty string (i.e. ""). Please refer to the below example for a sample use-case.
const payload = {dob : '1988-06-10'}
const context = '{now: date(""), y: date(dob), age: (years and months duration(y, now)).years}'
const text = 'if age > 25 then "Legal" else "Illegal"';
const parsedContext = FEEL.parse(context);
const parsedText = FEEL.parse(text);
parsedContext.build(payload).then(ctx => {
const c = Object.assign({}, payload, ctx);
return parsedText.build(c);
}).then((result) => {
console.log(result); // 'Legal'
}).catch(err => {
console.error(err);
});