Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Get Current time in Milliseconds

VirtEyes
VirtEyes Posts: 62

I am trying to generate a timestamp of the current time but in milliseconds. Is there a scripting option to do this, perhaps Extension tool, or is it possible to generate it via the Data Generator Tool?

Comments

  • [Deleted User]
    [Deleted User] Posts: 0 admin

    This is possible in the Data Generator Tool, you will want to use a syntax like the following:

    HH:mm:ss.SSS or hh:mm:ss.SSS a
    This will give you a time stamp like
    13:30:00.010 or 01:30:00.010 PM, respectively.

    Hope this helps!

  • VirtEyes
    VirtEyes Posts: 62
    edited November 2019

    Hey Thomas, will this generate the timestamp as just the number of milliseconds?
    Ex: 12 pm -> 12333434 milliseconds

  • VirtEyes
    VirtEyes Posts: 62

    We have a script that does this but I haven't adjusted yet to Virtualize, additionally, I want to see if I could use the Data Generator Tool instead:

    import java.util.GregorianCalendar;
    GregorianCalendar cal = new GregorianCalendar();
    responsedate = cal.getTimeInMillis();

    testExec.setStateObject("responsedate", responsedate);

  • [Deleted User]
    [Deleted User] Posts: 0 admin

    I think in that case I missed your question :) It looks like this should be handled in an extension tool.

    If you want to do this in Javascript, then I would use the following:
    var d = new Date("<Today's Date>");
    var n = d.getMilliseconds();

    More info on this here:
    https://www.w3schools.com/jsref/jsref_getmilliseconds.asp

  • VirtEyes
    VirtEyes Posts: 62

    Hey Thomas, that resource works however it returns a three digit number

    I dug around and produced this Groovy script which works:

    import com.parasoft.api.*

    public int getMili(){
    Calendar responsedate = Calendar.getInstance();

    return responsedate.getTimeInMillis() / 1000;
    

    }

  • benken_parasoft
    benken_parasoft Posts: 1,302 ✭✭✭

    Folks, just use java.lang.System.currentTimeMillis()