C Function – Add five minutes to current time

I needed to write a function which added five minutes to the current time in a LoadRunner script. This allowed a script to simulate appointment bookings for a web application.

The function could be modified to add more or less time, the clever bit was making sure that it handled the last few minutes of an hour properly.

For example.
13:56 + 5 minutes = 14:01

The function handles this by converting the 13 and the 56 into integers, doing the necessary sums and then converting the results back into strings which LoadRunner can use.

When the integers are converted back to strings, single digit number are displayed correctly e.g. 01,02,03 etc. rather than 1,2,3.

The function will not work in the last five minutes of the day because I haven’t added a function to add five minutes to 23:56 to get 00:01. (If you’re running tests this late, don’t use this function).

Action()

{

int iCurrentHour;

int iCurrentMinute;

int iTotalMinutes;

int iNextHour;

int iNextMinute;


char sTimeNow[6];

char sTimeInFiveMins[6];

char sTemporaryTimeValue[3];

char sCharCurrentHour[3];

char sCharCurrentMinute[3];

char sCharNextHour[3];

char sCharNextMinute[3];



lr_save_datetime(“%H”, TIME_NOW , “sCurrentHour”);

lr_save_datetime(“%M”, TIME_NOW , “sCurrentMinute”);

// Uncomment these next two lines if you need to test the function using alternative times.

// lr_save_string(“12”, “sCurrentHour”);

// lr_save_string(“58”, “sCurrentMinute”);


//Convert Hour and Minute into iHour and iMinute (integers)

//Convert Hours into Minutes

iCurrentHour = atoi(lr_eval_string(“{sCurrentHour}”));

iCurrentMinute = atoi(lr_eval_string(“{sCurrentMinute}”));


//Add 5 minutes to the current time

iNextHour=iCurrentHour;

iNextMinute=iCurrentMinute+5;


//If after the 54th minute in the hour, add 1 hour to the “hour” time and take 55 minutes off the “minute” time

if (iCurrentMinute>54)

{

iNextHour=iCurrentHour+1;

iNextMinute=iNextMinute-60;

}


//Convert integers back into strings

//The “if” statements make sure that the times are formatted correctly after the maths has been done.

//e.g. Five past One in the morning is 01:05, instead of 1:5

lr_save_int(iCurrentHour,”sCurrentHour”);

if (strlen(lr_eval_string(“{sCurrentHour}”))<2)

{

strcpy(sTemporaryTimeValue, “0”);

strcat(sTemporaryTimeValue, lr_eval_string(“{sCurrentHour}”));

sprintf(sCharCurrentHour, “%s”, sTemporaryTimeValue);

lr_save_string(sTemporaryTimeValue, “sCurrentHour”);

}


lr_save_int(iCurrentMinute,”sCurrentMinute”);

if (strlen(lr_eval_string(“{sCurrentMinute}”))<2)

{

strcpy(sTemporaryTimeValue, “0”);

strcat(sTemporaryTimeValue, lr_eval_string(“{sCurrentMinute}”));

sprintf(sCharCurrentMinute, “%s”, sTemporaryTimeValue);

lr_save_string(sTemporaryTimeValue, “sCurrentMinute”);

}


lr_save_int(iNextHour,”sNextHour”);

if (strlen(lr_eval_string(“{sNextHour}”))<2)

{

strcpy(sTemporaryTimeValue, “0”);

strcat(sTemporaryTimeValue, lr_eval_string(“{sNextHour}”));

sprintf(sCharNextHour, “%s”, sTemporaryTimeValue);

lr_save_string(sTemporaryTimeValue, “sNextHour”);

}


lr_save_int(iNextMinute,”sNextMinute”);

if (strlen(lr_eval_string(“{sNextMinute}”))<2)

{

strcpy(sTemporaryTimeValue, “0”);

strcat(sTemporaryTimeValue, lr_eval_string(“{sNextMinute}”));

sprintf(sCharNextMinute, “%s”, sTemporaryTimeValue);

lr_save_string(sTemporaryTimeValue, “sNextMinute”);

}



lr_output_message(“The current time is %s:%s”,lr_eval_string(“{sCurrentHour}”),lr_eval_string(“{sCurrentMinute}”));

lr_output_message(“The time in 5 minutes will be %s:%s”,lr_eval_string(“{sNextHour}”),lr_eval_string(“{sNextMinute}”));


return 0;

}

 

Author: Richard Bishop

http://about.me/richard_bishop