# Time difference using ruby

**URL:** https://discuss.elastic.co/t/time-difference-using-ruby/173549
**Category:** Logstash
**Created:** [March 22, 2019, 6:32pm UTC](https://discuss.elastic.co/t/time-difference-using-ruby/173549 "2019-03-22T18:32:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)
#### Post date: [March 22, 2019, 6:32pm UTC](https://discuss.elastic.co/t/time-difference-using-ruby/173549/1 "2019-03-22T18:32:47Z")

</div>

I am trying to get a time difference between two time field using ruby filter but looks like one of the timefiled is empty or nill and that is giving me error  
ERROR] ruby - Ruby exception occurred: undefined method `-' for nil:NilClass

I am already converting execution\_end to date format above this code.  
I don't think database has this field empty.

if [next\_exec\_start] == "nil" or [next\_exec\_start] == "" or [execution\_end] == "" or [execution\_end] == "nil" {  
mutate { update =\> { "next\_exec\_start" =\> 0 } }  
mutate { add\_field =\> { "next\_exec\_min" =\> 0 } }  
}  
else {  
date { match =\> ["next\_exec\_start", "dd-MMM-yy HH:mm"]  
target =\> "next\_exec\_start" }  
ruby {  
init =\> "require 'time'"  
code =\> "event.set('next\_exec\_min',(event.get('next\_exec\_start')-event.get('execution\_end'))/60.round(2))"  
}  
}

What am I doing wrong. I want to skip ruby operation if next\_exec\_start is not present. but I need that record.

---

<div class="post-metadata">

### Author: ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)
#### Post date: [March 22, 2019, 6:38pm UTC](https://discuss.elastic.co/t/time-difference-using-ruby/173549/2 "2019-03-22T18:38:48Z")

</div>

more update, here is the record where I get \_rubyexception

```
{
     tags" => [
        [0] "_rubyexception"
    ],
       "execution_end" => 2019-03-21T16:31:00.000Z,
   "execution_start" => 2019-03-21T14:33:00.000Z,
    "next_exec_start" => nil,
}

```

Looks like it is failing when next\_exec\_start is nil but I am trying to replace that with 0 in if statement. looks like I am not comparing something nil might be something else in logstash

---

<div class="post-metadata">

### Author: ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)
#### Post date: [March 22, 2019, 7:10pm UTC](https://discuss.elastic.co/t/time-difference-using-ruby/173549/3 "2019-03-22T19:10:53Z")

</div>

ok fixed it myself  
nil value is consider nothing in logstash I think  
used following and it worked  
if ![next\_exec\_start] { assign 0 }  
else { do something }

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [April 19, 2019, 7:11pm UTC](https://discuss.elastic.co/t/time-difference-using-ruby/173549/4 "2019-04-19T19:11:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
