LoadRunner相关时间函数

| |
[不指定 2006/11/30 16:02 | by ipaddr ]
将以下内容,写成ipaddrtime.h,放在LoadRunner目录,就可以在Scripts里面
#include "../ipaddrtime.h"
再调用这些函数.
[code]
typedef long time_t;
struct tm {
      int tm_sec; /* seconds after the minute - [0,59] */
      int tm_min; /* minutes after the hour - [0,59] */
      int tm_hour; /* hours since midnight - [0,23] */
      int tm_mday; /* day of the month - [1,31] */
      int tm_mon; /* months since January - [0,11] */
      int tm_year; /* years since 1900 */
      int tm_wday; /* days since Sunday - [0,6] */
      int tm_yday; /* days since January 1 - [0,365] */
      int tm_isdst; /* daylight savings time flag */
#ifdef LINUX
      int tm_gmtoff;
      const char *tm_zone;
#endif
};


struct tm *globalGmt;

//取得与当前日期相差tDays天数的日期,将日期以字符串的形式存入tDate
void getDate(char * tDate,int tDays)
{
  time_t tmpTime=0;
  //取得当前的时间截
  time(&tmpTime);
  //加上tDays天,tDays可以是负数
  tDays=tDays*24*60*60;
  tmpTime=tmpTime+tDays;
  //转成tm的格式
  globalGmt=(struct tm *)gmtime(&tmpTime);
  //输出
  sprintf(tDate,"%i-%02i-%02i",globalGmt->tm_year+1900,globalGmt->tm_mon+1,globalGmt->tm_mday);
}

//将时间截转换为字符串
genDate(char * tDate,time_t tTime)
{
  struct tm *tGmt;
  tGmt=(struct tm *)gmtime(&tTime);
  sprintf(tDate,"%i-%02i-%02i",tGmt->tm_year+1900,tGmt->tm_mon+1,tGmt->tm_mday);
}

//将时间inDate加上tDate,并将结果输出到outDate
copyDate(char * inDate,char * outDate,int tDate)
{
  char strTmp[50];
  int year=2006;
  int month=10;
  int day=1;

  struct tm date;
  time_t t;

  char * strSep="-";
  int tokenNum=0;
  char * token;

  //parse date
  strcpy(strTmp,inDate);
  token=(char *)strtok(strTmp,strSep);
  if(!token)
  {
    return 0;
  }
   while(token!= NULL)
  {
    if (tokenNum==0)
    {
      year=atoi(token);
    }
    if (tokenNum==1)
    {
      month=atoi(token);
    }
    if (tokenNum==2)
    {
      day=atoi(token);
    }

       token=(char *)strtok(NULL,strSep);
    tokenNum=tokenNum+1;
   }

  date.tm_year = year-1900;
  date.tm_mon = month-1;
  date.tm_mday = day;
  date.tm_hour = 10;
  date.tm_min = 0;
  date.tm_sec = 0;

  t = mktime(&date);
  t = t + 24*60*60*tDate;

  genDate(outDate,t);
}
//以上函数可改写成从字符串到t_time类型的转换函数.[/code]
Program | 评论(0) | 引用(0) | 阅读(5973)