代码之家  ›  专栏  ›  技术社区  ›  jeesan485

计算soapui中两个日期时间之间的差值

  •  1
  • jeesan485  · 技术社区  · 8 年前

    我正在使用SoapUI免费版本。我有一个REST响应,它像这样返回日期时间

    <startTime>2018-02-22T17:10:00-05:00</startTime>
    <endTime>2018-02-22T18:05:00-05:00</endTime>
    

    如何使用groovy测试步骤计算它们之间的差异(以分钟为单位)?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Chris Adams    8 年前
    //Assuming string teased out of response, if not you need to extract the value to a string....
    def startString = '<startTime>2018-02-22T17:10:00-05:00</startTime>';
    def endString     = '<endTime>2018-02-22T18:05:00-05:00</endTime>';
    
    // If you have the tags, ditch them.
    startString = startString.replace("<startTime>", "");
    startString = startString.replace("</startTime>", "");
    
    endString = endString.replace('<endTime>', '');
    endString = endString.replace('</endTime>', '');
    
    log.info("Now just strings... ${startString} - ${endString}");
    
    
    // Convert strings to dates...
    def convertedStartDate = Date.parse("yyyy-MM-dd'T'HH:mm:ssX",startString);
    def convertedEndDate = Date.parse("yyyy-MM-dd'T'HH:mm:ssX",endString);
    
    log.info("Now dates...  ${convertedStartDate} - ${convertedEndDate}");
    
    
    //Use time category to tease out the values of interest...
    use(groovy.time.TimeCategory) {
        def duration = convertedEndDate - convertedStartDate
        log.info( "Days: ${duration.days}, Hours: ${duration.hours}, Minutes: ${duration.minutes}, Seconds: ${duration.seconds}, etc.")
    }