Search code examples
rubylogstashlogstash-groklogstash-configuration

How to find time difference in milliseconds from two datetime stamp field using ruby


I need to get time difference from two time stamp fields, i.e., My input : "requestTime" => "2016-12-27 18:35:13:833", "responseTime" => "2016-12-27 18:35:13:834",

I Need to get time diff as 1 milliseconds as a result.

I used this code :

 event['time_difference']= (Time.parse(event['responseTime']).to_i) - (Time.parse(event['requestTime']).to_i)

I got result as 0 seconds for above input.


Solution

  • Use to_f

    millisec = 1000 * (response_time.to_f - request_time.to_f)
    

    The timestamps must have this format though,

    "2016-12-27 18:35:13.833"
    

    Notice the . separator between seconds and milliseconds!