Working with Unix Time stamp in Loadrunner

610 views Asked by At

I am working on a script which has 2 fields - Doc Validity Start date and End Date. For testing purpose, we have decided to keep the validity as 4 days. While recording the script, the start and the end date is coming in Unix format (with milliseconds). I am aware of the function lr_save_timestamp to capture the current date, which can be used for From Date. However, i am not able to perform the offset of time period by 4 days. I tried converting the captured time stamp to INT and performing mathematical operations, but the time stamp is so huge that even double could not help me on this and as a result i am not able to add the offset value. All the data types - int, long, double were getting exhausted.

Can someone help me on how to make this work?

for example, captured timestamp - 1686700800000 
offset value in milliseconds - 345600000
Final value (which will serve as End Validity date) - 16870473000000

I am trying to embed the following code in the Load Runner script which we are developing using C Language

timestamp()
{
    long long int test;
    double fromDate , toDate ; 
    lr_save_timestamp("time",LAST);
    lr_output_message("Now -> %s", lr_eval_string("{time}"));
    fromDate = atoi(lr_eval_string("{time}"));
    test = atoi(lr_eval_string("{time}"));
    lr_output_message("Now -> %ld", fromDate);
    lr_output_message("Now -> %.0f", fromDate);
    lr_output_message("New -> %d",test);
    return 0;
}

Following is the output

Starting iteration 1.
Starting action timestamp.
timestamp.c(7): Notify: Saving Parameter "time = 1686784563045".
timestamp.c(9): Notify: Parameter Substitution: parameter "time" =  "1686784563045"
timestamp.c(9): Now -> 1686784563045
timestamp.c(11): Notify: Parameter Substitution: parameter "time" =  "1686784563045"
timestamp.c(13): Notify: Parameter Substitution: parameter "time" =  "1686784563045"
timestamp.c(15): Now -> -4194304
timestamp.c(17): Now -> 2147483647
timestamp.c(19): New -> 2147483647
Ending action timestamp.
Ending iteration 1.

Since the value thats coming in variable "time" and value in "fromDate" or "test" is different, i am not able to perform the addition operation.

1

There are 1 answers

8
James Pulley On

The following has been tested in LoadRunner and produced the output you desire. Adjust accordingly for two days or four days. Three days of offset used for the example

Action()
{

    char currenttime[13],futuretime[13],preamble[3];    
    int mytimestampfragment;
    
    lr_save_timestamp("foo","DIGITS=10",LAST);
    lr_message("%s", lr_eval_string( "{foo}" ));
    
    // Your timestamp is now current in ten digits format as a string
    // Let's strip the portion we need to add a couple of days to
    // this would be the last seven digits, as 86400x3=259200

    mytimestampfragment = atoi(&lr_eval_string("{foo}")[3]);
    lr_message("%d", mytimestampfragment);
    
    // Add the three days
    
    mytimestampfragment+=259200;
    lr_message("%d", mytimestampfragment);
    
    // assemble your strings
    sprintf(currenttime,"%s000",lr_eval_string("{foo}") );
    lr_message("The Current timestamp is %s", currenttime );    
    
    strncpy(preamble,lr_eval_string("{foo}"),3);
    sprintf(futuretime,"%s%d000",preamble,mytimestampfragment);
    lr_message("The Future timestamp is %s", futuretime );

    return 0;
}

And, the output

Virtual User Script started at: 6/16/2023 10:37:06 AM
Starting action vuser_init.
Ending action vuser_init.
Running Vuser...
Starting iteration 1.
Starting action Action.
1686926226
6926226
7185426
The Current timestamp is 1686926226000
The Future timestamp is 1687185426000
Ending action Action.
Ending iteration 1.
Ending Vuser...
Starting action vuser_end.
Ending action vuser_end.
Vuser Terminated.