0

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.

Nike
  • 23
  • 1
  • 10
  • If programming in C, an integer type that is at least 64 bits wide (or at least 42 bits wide but I doubt you have a 42-bit integer type!) will be able to handle those values. For example, the `long long int`, `int64_t`, `int_fast64_t` or `int_least64_t` types will be suitable. (You need `#include ` or `#include ` for all those types except `long long int`.) – Ian Abbott Jun 14 '23 at 15:54
  • You mentioned a "script" but tagged [tag:c], so I do not know if you are programming in C or not. – Ian Abbott Jun 14 '23 at 15:56
  • @Nike, " tried converting the captured time stamp to INT and performing mathematical operations" --> Post the script and/or the C code. – chux - Reinstate Monica Jun 14 '23 at 16:09
  • @IanAbbott - i have included the code piece and also the output in the question. How can i maintain the value "1686784563045" which i am converting from String to Int type. I am converting this because the final value i need will be offset by 4 days – Nike Jun 14 '23 at 23:22
  • Use `strtoll` instead of `atoi`. – Ian Abbott Jun 15 '23 at 09:10
  • @IanAbbott, the default language for LoadRunner is C. The parlance referring to the virtual user is a script, but under the covers the LCC C compiler is used to compile and execute the virtual user code, – James Pulley Jun 16 '23 at 13:35
  • @JamesPulley Doesn't it support `strtoll`? – Ian Abbott Jun 16 '23 at 15:58
  • @JamesPulley OK I see no mention of `strtoll` in the C function reference [here](https://admhelp.microfocus.com/vugen/en/2023/help/function_reference/Content/FuncRef/c_language/etc/lrFuncRef_CLang_Alphabetical_List.htm), but it does have `sscanf`. Does that support reading a `long long` value with the `%lld` format specifier? For example, `sscanf(lr_eval_string("{time}", "%lld", &test);`? – Ian Abbott Jun 17 '23 at 16:27

1 Answers1

0

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.
James Pulley
  • 5,606
  • 1
  • 14
  • 14
  • So what would happen when the addition carries over into first three digits? – Ian Abbott Jun 16 '23 at 16:01
  • It will break on July 19, 2023. – Ian Abbott Jun 16 '23 at 16:11
  • In `char currenttime[13],futuretime[13],preamble[3];`, those arrays are too short by one character. They have no room for the null terminators. – Ian Abbott Jun 16 '23 at 16:30
  • This is an example, modify as needed for your offsets and dates if it carries to the eighth digit – James Pulley Jun 16 '23 at 16:56
  • were this my code, I would be passing the C variables back into LR variables using lr_save_string() to take advantage of the LoadRunner parameterization engine. When doing so that will add a null terminator to the end of the char* string. The variables will be reinitialized on the second iteration. I only needed thirteen, and so that is what I grabbed. – James Pulley Jun 16 '23 at 17:03
  • 1
    But your `sprintf` calls are writing 14 characters (including the null terminator) into a 13 character array. – Ian Abbott Jun 16 '23 at 17:04
  • Point well taken (sprintf). I had not considered that in my example. LoadRunner's compiler is generous enough not to throw an exception or warning in that case. Alter to [14] as needed. – James Pulley Jun 16 '23 at 17:07
  • The point I was trying to make with the example, is that the whole 13 digits are not needed for the math, just a subset. As the timestamp comes back as a string, pull the number of significant digits you need to perform the math, then glue the string back together so it becomes useful as part of the request. I omitted the last three digits beyond one second, replacing them with 000 and then used a pointer offset to grab the significant digits I needed. You could just as easily pull a substring out and perform the math needed for the day_seconds offsets – James Pulley Jun 16 '23 at 17:10