# Convert time duration to float

**URL:** https://discuss.elastic.co/t/convert-time-duration-to-float/109449
**Category:** Logstash
**Created:** [November 28, 2017, 5:39pm UTC](https://discuss.elastic.co/t/convert-time-duration-to-float/109449 "2017-11-28T17:39:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yahoo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yahoo](https://discuss.elastic.co/u/yahoo)
#### Post date: [November 28, 2017, 5:39pm UTC](https://discuss.elastic.co/t/convert-time-duration-to-float/109449/1 "2017-11-28T17:39:45Z")

</div>

I am trying to convert a time duration into float.  
The error message:

```
 Ruby exception occurred: undefined method `/' for 2017-01-01T05:00:00.000Z:LogStash::Timestamp
{
       "voice_duration" => "00:00:00",
       "voice_durationd" => 2017-01-01T05:00:00.000Z,
}

```

The Logstash config:

```
 date {
        match => ["[voice_duration]", "HH:mm:ss"]
        target => "[voice_durationd]"
       timezone => "%{mytimezone}"
   }
 ruby { code => "event.set('duration', (event.get('voice_durationd') / 60 ))" }
```

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [November 28, 2017, 7:16pm UTC](https://discuss.elastic.co/t/convert-time-duration-to-float/109449/2 "2017-11-28T19:16:55Z")

</div>

Durations can be converted to different units by division or multiplication but a timestamp can't. `voice_durationd` is clearly a timestamp.

What are you trying to do here?

---

<div class="post-metadata">

### Author: ![yahoo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yahoo](https://discuss.elastic.co/u/yahoo)
#### Post date: [November 28, 2017, 9:01pm UTC](https://discuss.elastic.co/t/convert-time-duration-to-float/109449/3 "2017-11-28T21:01:28Z")

</div>

I am trying to convert a time duration into float.  
The input data is in the form HH:mm:ss.

---

<div class="post-metadata">

### Author: ![yahoo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yahoo](https://discuss.elastic.co/u/yahoo)
#### Post date: [November 29, 2017, 3:35am UTC](https://discuss.elastic.co/t/convert-time-duration-to-float/109449/4 "2017-11-29T03:35:05Z")

</div>

My solution:

```
# convert duration from hour:min:second 00:00:00 into a float of minutes
csv {
     # hour:min:second
     columns => ["hours", "minutes", "seconds"]
     source => "voice_duration"
     separator => ":"
}
 
 ruby {
        code => "event.set('duration', ( (event.get('hours').to_f * 60) + event.get('minutes').to_f + (event.get('seconds').to_f / 60 )))"
 }
mutate { 
    remove_field => ["voice_duration", "hours", "minutes", "seconds"]
}
```

---

<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: [December 27, 2017, 3:35am UTC](https://discuss.elastic.co/t/convert-time-duration-to-float/109449/5 "2017-12-27T03:35:28Z")

</div>

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