CRS Script Workaround for Time.ms issue

Author
William Bell
Vice President, Solutions and Products

I came across an issue working on a script enhancement for a customer where  I needed to  compare two time/date variables and evaluate the seconds that have expired between them.  The CRS development guide says that using the “.ms” method on a time variable would return epoch time but that is not so, or at least not the case with version 4.0.

{readmore}

CRS has Date variables and Time variables.  Date variables support multiple formats but will capture time values as well as MM/DD/YY values.  Time variables will get HH:MM:SS.ms AM|PM values.  According to the CRS developer guide, if you have a date variable and use the “.ms” method you will get the number of milliseconds since January 1, 1970, 00:00:00 GMT (aka Unix epoch or epoch time).

Unfortunately, the developer guide is either incorrect or I am completely misunderstanding the document.  Whichever is the case, the syntax (and I tried multiple methods) isn’t getting me what it is supposed to.  Instead, I get the actual milliseconds for a specific instance in time. 
To work around this issue you have to execute a couple of conversions and assign the value to a variable of type long. This is done as follows:

Set lngEnterQ = Date.parse(D[now].toGMTString())

Basically, this will return the milliseconds that have expired since Jan 1, 1970 00:00:00 GMT.  It took some reading of the Java API to put that together.  The CRS developer guide does say you can do the .parse() and .toGMTString() functions but doesn’t explain them at all.  The .toGMTString() is pretty intuitive but the .parse() was not what I thought it was.

Here’s to Google and Sun’s Java documentation library.

2 responses to “CRS Script Workaround for Time.ms issue

  1. Time.ms should return the millisecond part of the Date object

    Also, a simpler way to get the epoch is to call ?.getTime() on your object.

    e.g.,
    start_time = t[now].getTime()
    delay 5 seconds
    end_time = t[now].getTime()
    time_diff = end_time – start_time

    Of course this will not work in standard because you cannot use methods on objects in standard.

  2. Hi Bill,

    Thanks for blogging about this – I spent too long poring over the scanty documentation before finding. Kudos to Anthony to for posting his contribution.

    🙂

Leave a Reply