LoadRunner - Date Handling (1 of 3) - lr_save_datetime
If you are using LoadRunner, you've probably encountered date and time values in your scripts. These values should be evaluated and dealt with.
Where Do Date/Time Values Come From?
- Entered during recording.
- Returned by the server.
- Generated by the client.
Let's look at each one and determine how to handle the data.
Entered During Recording
- Why did I pick this date?
- Is the date always the same?
- Is there a calculation involved? For example: today + 7 days, today - 1 hour?
If it's always the same, leave it hard-coded or create a parameter file to store the value. If it's a calculation, use the lr_save_datetime function to generate the required date information. From the ‘HP LoadRunner Online Function Reference’:
lr_save_datetime
Assigns the current date and time to a parameter.
void lr_save_datetime (const char *format, int offset, const char *name);
- format – The format of the retrieved data/time information.
- offset – Offset from the current date and time, using the constants: DATE_NOW, TIME_NOW, ONE_DAY, ONE_HOUR, ONE_MIN. For example, TIME_NOW + ONE_HOUR.
- name – The name of the parameter to store the date/time information.
The supported format codes can be found at: ‘HP LoadRunner Online Function Reference > Utility Functions: C Language (LR) > Datetime Format Codes’
Examples
lr_save_datetime ("%m/%d/%Y", DATE_NOW, "pDate1"); lr_output_message ("Today's Date is %s", lr_eval_string ("{pDate1}")); lr_save_datetime ("%B %d, %Y", DATE_NOW+ONE_DAY, "pDate2"); lr_output_message ("Tomorrow’s Date is %s", lr_eval_string ("{pDate2}")); lr_save_datetime("%a-%b-%d-%Y %H:%M:%S", DATE_NOW-(ONE_DAY*4)-(ONE_MIN*30), "pDate3"); lr_output_message ("Date/Time 4 days and 30 minutes ago is %s", lr_eval_string ("{pDate3}")); lr_save_datetime ("%d", DATE_NOW, "pDate4"); lr_save_datetime ("%m/01/%Y", DATE_NOW-(ONE_DAY*(atoi(lr_eval_string ("{pDate4}"))+1)), "pDate5"); lr_output_message ("First Day of Previous Month is %s", lr_eval_string ("{pDate5}")); // Find the last day of next month // Not efficient, but a different use of lr_save_datetime // Keep adding days until month changes 2 times, then back off 1 day int iDays = 1, iMonthCurrent, iMonthChanged = 0, iMonth, bDone = 0; lr_save_datetime ("%m", DATE_NOW, "pDate6"); iMonthCurrent = atoi (lr_eval_string ("{pDate6}")); while (bDone == 0) { lr_save_datetime ("%m", DATE_NOW+(ONE_DAY*(iDays)), "pDate7"); iMonth = atoi (lr_eval_string ("{pDate7}")); if (iMonth != iMonthCurrent) { bMonthChanged++; iMonthCurrent = iMonth; } if (bMonthChanged == 2) { bDone = 1; } else { iDays++; } } lr_save_datetime ("%m/%d/%Y", DATE_NOW+(ONE_DAY*(iDays-1)), "pDate8"); lr_output_message ("Last Day of Next Month is %s", lr_eval_string ("{pDate8}"));
Returned By Server
- Correlate the value into a parameter and use this new parameter.
Script snippet
web_reg_save_param ("dServerDate", "LB=currentdate=", "RB=;", "ORD=1", LAST); web_url(...); web_submit_data(... ITEMDATA, "Name=_someKey", "Value={dServerDate}", ENDITEM, LAST);
Generated By The Client
- Determine the calculation and use lr_save_datetime.
Common Question And Answer
In most situations, lr_save_datetime will handle the date requirements(if the date in question is relative to the current date/time of your system).
Recently on a LoadRunner forum on Google Groups someone asked how to change the date returned by the server (https://groups.google.com/forum/#!topic/lr-loadrunner/zzG_431yPO4). As suggested, the simplest solution would be to use lr_save_datetime with the given format and DATE_NOW+ONE_MIN. These answers assume the desired date was related to the current date/time and makes lr_save_datetime an easy solution to implement.
lr_save_datetime solution
lr_save_datetime ("%d-%b-%Y %H:%M:%S", DATE_NOW, "pCurrentDate"); lr_save_datetime ("%d-%b-%Y %H:%M:%S", DATE_NOW+ONE_MIN, "pNewDate"); lr_output_message ("Current Date is %s", lr_eval_string ("{pCurrentDate}")); lr_output_message ("Current Date plus one minute is %s", lr_eval_string ("{pNewDate}"));
Tougher Questions And Answers
What if the date could be any date? Given the date format, how would you safely add/subtract date/time elements?
- 21-Aug-2012 07:33:00, add 1 minute
- 31-Dec-2011 23:59:59, add 1 minute
- 24-Feb-2009 12:34:56, add 50 hours
- 01-Sep-2012 09:00:00, subtract 100 days
You can not just parse the data and increment a number. Depending on the date/time given, any additions/subtractions could result in date/time rollovers. What about leap years? Like the example above, an inefficient looping structure could work, but there must be an easier way.
Reader Comments (1)
I'm not a developer, i always use the free online timestamp generator(http://www.online-code.net/unix-timestamp.html) to create the unix timestamp.