简体   繁体   English

如何用C语言-Loadrunner Web区分两个日期时间?

[英]How to differentiate between two Date Time in C language -Loadrunner Web?

I am trying to find differentiation between two date(ie 14:49:41 and 15:50:42) using below code: 我试图使用下面的代码找到两个日期(即14:49:41和15:50:42)之间的区别:

    Action()
{
    struct tm { 
    int tm_sec; 
    int tm_min; 
    int tm_hour; 
  }; 

  int rc; // return code
  struct tm date1;
  struct tm date2;
  long time_difference; // the number of time ticks (seconds) that separate date1 and date2.
  int hours, minutes, seconds;

  // Save example dates to a parameter. 
  // capture these values using web_reg_save_param or similar.
  // date format: hh:mm:ss
  lr_save_string("14:49:41", "Param_Date1");
  lr_save_string("15:50:42", "Param_Date2");

  // Read the values from the string into the date variables
  rc = sscanf(lr_eval_string("{Param_Date1}"), "%d:%d:%d",&date1.tm_hour, &date1.tm_min, &date1.tm_sec);


  // Repeat the above steps for Date2
  rc = sscanf(lr_eval_string("{Param_Date2}"), "%d:%d:%d", &date2.tm_hour, &date2.tm_min, &date2.tm_sec);




  time_difference = mktime(&date2) - mktime(&date1);
  lr_output_message("Total number of seconds difference: %d", time_difference);

  // Calculate time difference in  hours, minutes and seconds.

  hours = time_difference/3600;
  time_difference = time_difference - (hours * 3600);
  minutes = time_difference/60;
  time_difference = time_difference - (minutes * 60);
  seconds = time_difference;
  lr_output_message("Hours: %d, Minutes: %d, Seconds: %d", hours, minutes, seconds);

    return 0;
}

Actual output should return : Hours: 1, Minutes: 1, Seconds: 1 实际输出应返回:小时:1,分钟:1,秒:1
But output returns : Hours: 0, Minutes: 0, Seconds: 0 但是输出返回:小时:0,分钟:0,秒:0

Please help me fix this problem. 请帮助我解决此问题。 Or Else any other alternative achieve it? 还是其他任何替代方法都能实现?

hours, minutes and seconds should not be declared as Integers as you are dividing the value by 3600. If you declare them as floating point numbers it may work. 将值除以3600时,不应将小时,分钟和秒声明为整数。如果将它们声明为浮点数,则可能会起作用。 Other than that everything looks good 除此之外,一切看起来都不错

The difference between two times in seconds is easy to compute: 几秒钟之间的时间差很容易计算:

int secs1 = ((time1.hour * 60) + time1.min) * 60 + time1.sec;
int secs1 = ((time2.hour * 60) + time2.min) * 60 + time2.sec;
int sec_dif = secs1 - secs2;

Or this way: 或者这样:

int sec_dif =
    ((time1.hour - time2.hour) * 60 + (time1.min - time2.min)) * 60 + (time1.min - time2.min);

int min_dif = sec_dif / 60;
int hour_dif = sec_dif / (60 * 60);

No need to bother with converting the times into time_t types. 无需费心将时间转换为time_t类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM