[原创]Loadrunner时间函数、用时间…

来源:互联网 发布:ucloud pecona数据库 编辑:程序博客网 时间:2024/06/08 02:54

Loadrunner中取时间函数、用时间函数生成订单编号例子:

<如要转载,请注明网络来源及作者:Cheers_Lee>

问题的提出:

(1)有时候在Loadrunner中用C语言设计脚本时却要取系统时间;

(2)需要用时间生成唯一订单号;

(3)时间函数加随机数产生不重复的订单号;

一、取系统时间:

Action()
{
lr_save_datetime("现在是:%Y年%m月%d日%H时%M分%S秒", DATE_NOW,"pNextWeek_date");
lr_output_message("%s",lr_eval_string("{pNextWeek_date}"));

 

lr_save_datetime("下周这个时间是:%Y/%m/%d/%H:%M:%S", DATE_NOW +(ONE_DAY * 7), "pNextWeek_date");
lr_output_message("%s",lr_eval_string("{pNextWeek_date}"));

 

lr_save_datetime("%Y%m%d%H%M%S", DATE_NOW,"pNextWeek_date");
lr_output_message("用时间函数生成订单号:%s",lr_eval_string("{pNextWeek_date}"));

 return 0;
}

二、收到的启发时间函数随机数产生不重复的订单号;)非常有用

int i;
char OrderId[20];
Action()
{

srand(time(NULL)); //特别注意:加上这句,每次取不同的随机值

i=rand()%10;
lr_save_datetime("%Y%m%d%H%M%S", DATE_NOW,"now");

sprintf(OrderId,"%s%d",lr_eval_string("{now}"),i);


lr_output_message("订单号为:%s",OrderId);
 return 0;
}

更多时间格式函数参考lr_save_datetime()函数及DatetimeFormat Codes (如下表)。

Datetime Format Codes

The following format codes are supported forlr_save_datetime.

Code
Description
%a
day of week, using locale's abbreviatedweekday names
%A
day of week, using locale's full weekdaynames
%b
month, using locale's abbreviated monthnames
%B
month, using locale's full month names
%c
date and time as %x %X
%d
day of month (01-31)
%H
hour (00-23)
%I
hour (00-12)
%j
number of day in year (001-366)
%m
month number (01-12)
%M
minute (00-59)
%p
locale's equivalent of AM or PM, whicheveris appropriate
%S
seconds (00-59)
%U
week number of year (01-52), Sunday is thefirst day of the week. Week number 01 is the first week with fouror more January days in it.
%w
day of week; Sunday is day 0
%W
week number of year (01-52), Monday is thefirst day of the week. Week number 01 is the first week with fouror more January days in it.
%x
date, using locale's date format
%X
time, using locale's time format
%y
year within century (00-99)
%Y
year, including century (for example,1988)
%Z
time zone abbreviation
%%
to include the "%" character in your outputstring
原创粉丝点击