jsonstreamsets

Modify time:now() to be less one hour


First of all, I don't quite know what the core language is of this. I have followed a tutorial.

My code looks like this:

{
    "type": "dateRange",
    "dateRange": "${time:extractStringFromDateTZ(time:now(), 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ss.SSS')}/${time:extractStringFromDateTZ(time:now(), 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ss.SSS')}"
}

This gives me the following:

{
    "type": "dateRange",
    "dateRange": "2019-04-01T00:00:00.000/2019-04-01T00:00:00.000"
}

What I am trying to do is take 1h off the first date. This is what I have tried:

Before:

${time:extractStringFromDateTZ(time:now(), 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ss.SSS')}

After:

${time:millisecondsToDateTime(time:dateTimeToMilliseconds(time:now()) - (3600), 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ss.SSS')}

Although this seems to be valid code, it says its invalid. Please can someone help me understand how to modify the "before" code to be 1 hour from the time now.


Solution

  • First, as mentioned in the comments to your post, 1h is 3600000 milliseconds

    Then, I never worked with StreamSets but looking at the doc:

    the signature of the method you try to use is time:dateTimeToMilliseconds(<Date object>) and it returns a Long Its description is:

    Converts a Date object to an epoch or UNIX time in milliseconds.

    So you can't call it with these parameters , 'UTC', 'yyyy-MM-dd\'T\'HH:mm:ss.SSS'

    This might be the valid syntax it is expecting:

    ${time:millisecondsToDateTime(time:dateTimeToMilliseconds(time:now()) - (3600000))}
    

    You should be using other functions if you want to use a time zone:

    time:millisecondsToDateTime(<long>) returns a Date object and time:extractStringFromDateTZ(<Date object>, <time zone>, <format string>) returns a String

    Can't you chain them like this?

    ${time:extractStringFromDateTZ(
        time:millisecondsToDateTime(time:dateTimeToMilliseconds(time:now()) - 3600000), 
        'UTC', 
        'yyyy-MM-dd\'T\'HH:mm:ss.SSS')}