loadrunner随机获取两个日期
时间: 2023-10-29 12:06:02 浏览: 26
可以使用以下代码来随机获取两个日期:
```lrscript
#include "stdlib.h"
#include "time.h"
void get_random_date(char* date1, char* date2) {
time_t t1, t2;
struct tm* tm1, *tm2;
char buf1[80], buf2[80];
// 获取当前时间戳
t1 = time(NULL);
t2 = t1 + (86400 * (rand() % 30)); // 随机增加 0-30 天的时间
// 将时间戳转换成 struct tm 格式
tm1 = localtime(&t1);
tm2 = localtime(&t2);
// 将 struct tm 格式转换成字符串格式
strftime(buf1, 80, "%Y-%m-%d", tm1);
strftime(buf2, 80, "%Y-%m-%d", tm2);
// 将字符串格式赋值给输出参数
strcpy(date1, buf1);
strcpy(date2, buf2);
}
int main() {
char date1[80], date2[80];
srand(time(NULL)); // 初始化随机种子
get_random_date(date1, date2);
lr_output_message("随机日期1:%s", date1);
lr_output_message("随机日期2:%s", date2);
return 0;
}
```
此代码中,使用了 `rand()` 函数和 `time()` 函数来生成随机日期。其中,`rand()` 函数用于生成随机天数,`time()` 函数用于获取当前时间戳,然后根据随机天数计算出另一个时间戳。接着,使用 `localtime()` 函数将时间戳转换成 struct tm 格式,再使用 `strftime()` 函数将 struct tm 格式转换成字符串格式,最终将字符串格式赋值给输出参数。