# Calculate the difference between two fields with time

**URL:** https://discuss.elastic.co/t/calculate-the-difference-between-two-fields-with-time/338859
**Category:** Logstash
**Created:** [July 20, 2023, 8:43am UTC](https://discuss.elastic.co/t/calculate-the-difference-between-two-fields-with-time/338859 "2023-07-20T08:43:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![akeelow](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@akeelow](https://discuss.elastic.co/u/akeelow)
#### Post date: [July 20, 2023, 8:43am UTC](https://discuss.elastic.co/t/calculate-the-difference-between-two-fields-with-time/338859/1 "2023-07-20T08:43:38Z")

</div>

Hello! One event has two fields ConnectTime and DisconnectTime. We need to calculate how long the event lasted. To do this, we need to subtract ConnectTime from DisconnectTime.

It seems to be very close to the solution, but I can not find a description of methods for events, which will help to convert text to time.

**Filtr:**

```auto
grok {
            match => { "message" => "<%{NUMBER:log_sequence}>%{NUMBER}: .%{SYSLOGTIMESTAMP:syslog_timestamp}: %%{WORD:facility}-%{INT:severity_level}-%{WORD:facility_mnemonic}: %{GREEDYDATA:message}" }
            match => { "message" => ", CallLegType %{INT:CallLegType}" }
            match => { "message" => ", ConnectionId %{WORD:ConnectionId}" }
            match => { "message" => ", SetupTime .%{TIME:SetupTime}" }
            match => { "message" => ", PeerAddress %{WORD:PeerAddress}" }
            match => { "message" => ", PeerSubAddress %{WORD:PeerSubAddress}" }
            match => { "message" => ", DisconnectCause %{INT:DisconnectCause}" }
            match => { "message" => ", DisconnectText %{GREEDYDATA:DisconnectText}" }
            match => { "message" => ", ConnectTime .%{TIME:ConnectTime}" }
            match => { "message" => ", DisconnectTime .%{TIME:DisconnectTime}" }
            match => { "message" => ", CallOrigin %{INT:CallOrigin}" }
            match => { "message" => ", ChargedUnits %{INT:ChargedUnits}" }
            match => { "message" => ", InfoType %{INT:InfoType}" }
            match => { "message" => ", TransmitPackets %{INT:TransmitPackets}" }
            match => { "message" => ", TransmitBytes %{INT:TransmitBytes}" }
            match => { "message" => ", ReceivePackets %{INT:ReceivePackets}" }
            match => { "message" => ", ReceiveBytes %{INT:ReceiveBytes}" }

            break_on_match => false
        }
        ruby {
            code => "event.set('DurationTime', (event.get('DisconnectTime').nil? || event.get('ConnectTime').nil?) ? nil : event.get('DisconnectTime') - event.get('ConnectTime'))"
        }

```

**Error:**

```auto
[ERROR][logstash.filters.ruby][main][2a1af8dde985efdc0de41f59cdb87ca292a39edd280f7831799e47e51fabef35] Ruby exception occurred: undefined method `-' for "14:17:36.195":String {:class=>"NoMethodError", :backtrace=>["(ruby filter code):2:in `block in filter_method'"

```

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [July 20, 2023, 4:59pm UTC](https://discuss.elastic.co/t/calculate-the-difference-between-two-fields-with-time/338859/2 "2023-07-20T16:59:11Z")

</div>

You could start with

```
    ruby {
        code => '
            c = event.get("ConnectTime")
            d = event.get("DisconnectTime")
            if c and d
                c = Time.parse(c)
                d = Time.parse(d)
                event.set("DurationTime", d - c)
            end
        '
    }

```

but you may want to add heuristics to cope with transitions across midnight.

```
            "DisconnectTime" => "00:00:00.001"
            "ConnectTime" => "23:59:59.999"

```

will result in

```
  "DurationTime" => -86399.998,

```

---

<div class="post-metadata">

### Author: ![akeelow](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@akeelow](https://discuss.elastic.co/u/akeelow)
#### Post date: [July 21, 2023, 5:06am UTC](https://discuss.elastic.co/t/calculate-the-difference-between-two-fields-with-time/338859/3 "2023-07-21T05:06:26Z")

</div>

I think I've solved the midnight crossing problem

```auto
        ruby {
            code => '
                c = event.get("ConnectTime")
                d = event.get("DisconnectTime")
                if c and d
                    c = Time.parse(c)
                    d = Time.parse(d)
                    a = c > d ? 86400 : 0
                    event.set("DurationTime", d - c + a)
                end
            '
        }

```

---

<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: [August 18, 2023, 5:06am UTC](https://discuss.elastic.co/t/calculate-the-difference-between-two-fields-with-time/338859/4 "2023-08-18T05:06:44Z")

</div>

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