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

时间:2018-06-14 13:26:48

标签: c difference loadrunner mktime

我试图使用以下代码找出两个日期(即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;
}

实际输出应返回:小时:1,分钟:1,秒:1 但是输出返回:小时:0,分钟:0,秒:0

请帮我解决这个问题。或者其他任何替代方案实现它?

2 个答案:

答案 0 :(得分:0)

小时,分钟和秒不应声明为整数,因为您将值除以3600.如果将它们声明为浮点数,它可能会起作用。除此之外一切看起来都不错

答案 1 :(得分:0)

两次以秒为单位的差异很容易计算:

paper-dropdown-menu

或者这样:

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;

无需费心将时间转换为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); 类型。