Skip to content

toMillis: Width formatting is not respected without a separator #546

@JAMSUPREME

Description

@JAMSUPREME

Bug Summary

Width formatting doesn't appear to be respected without a separator.

Versions

JSONata 1.8.5
Node v14.17.0

Detailed Information

Reference spec for width formatting: https://www.w3.org/TR/xpath-functions-31/#date-time-examples

At the moment, it appears the width formatting is not honored within $toMillis due to the way the regex is being generated. For an example input 201802, I should be able to specify a matcher [Y0001][M01] which will result in a parsed value of 2018/02/01. At the moment, the library incorrectly grabs too many characters due to the + regex so I end up with 20180/02/01

Suggested Fix

I attempted a fix but you can let me know whether or not it's reasonable: #545

Work-around

I made a little helper function to grab the fixed-width portions of year/month/date via substring and then reformat the string prior to sending it to toMillis:

const dateFunc = `
  $dateYMD := function($date) {( 
    $year := $substring($date, 0, 4);
    $month := $substring($date, 4, 2);
    $day := $substring($date, 6, 2);
    $year & '-' & $month & '-' & $day
  )};
`;

The library then functions as expected once the date string has been split via separators.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions