# Basic Math in Logstash

**URL:** https://discuss.elastic.co/t/basic-math-in-logstash/206452
**Category:** Logstash
**Created:** [November 4, 2019, 5:26pm UTC](https://discuss.elastic.co/t/basic-math-in-logstash/206452 "2019-11-04T17:26:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jim\_Thunder](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jim_thunder/32/45439_2.png) [@Jim\_Thunder](https://discuss.elastic.co/u/Jim_Thunder)
#### Post date: [November 4, 2019, 5:26pm UTC](https://discuss.elastic.co/t/basic-math-in-logstash/206452/1 "2019-11-04T17:26:55Z")

</div>

All I want to do it subtract 6 from the "hour\_of\_day" field, because it's six hours ahead. Which from my understanding, it shouldn't be ahead b/c I used ruby. But whatever the case, it's wrong.

Here's my code:

```
ruby {
		code => "event.set('[hour_of_day]',(event.get('@timestamp').time.strftime('%H') - 6))"
	}

```

I get error messages on the `-` b/c it (ruby?) doesn't know what `-` is.

How can I fix that?

Much appreciated!

---

<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: [November 4, 2019, 6:11pm UTC](https://discuss.elastic.co/t/basic-math-in-logstash/206452/2 "2019-11-04T18:11:48Z")

</div>

strftime returns a string. You are effectively trying to do

```
"12" - 6

```

It will not coerce "12" into 12, so you need to use .to\_i

```
ruby { code => "event.set('[hour_of_day]', (event.get('@timestamp').time.strftime('%H').to_i - 6))" }

```

How is @timestamp getting set? If it is set by a date filter then you can use the timezone option to adjust the hour.

---

<div class="post-metadata">

### Author: ![Jim\_Thunder](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jim_thunder/32/45439_2.png) [@Jim\_Thunder](https://discuss.elastic.co/u/Jim_Thunder)
#### Post date: [November 4, 2019, 6:14pm UTC](https://discuss.elastic.co/t/basic-math-in-logstash/206452/3 "2019-11-04T18:14:12Z")

</div>

That worked. Got my time set to the correct hour.

But yea I use a date filter to set @timestamp. I didn't know I could change the timezone!  
Point me in the right direction for that?

---

<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: [November 4, 2019, 6:52pm UTC](https://discuss.elastic.co/t/basic-math-in-logstash/206452/4 "2019-11-04T18:52:55Z")

</div>

Use the [timezone](https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html#plugins-filters-date-timezone) option. Something like

```
date {
    match => [...]
    timezone => "Etc/GMT-6"
}
```

---

<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 2, 2019, 6:52pm UTC](https://discuss.elastic.co/t/basic-math-in-logstash/206452/5 "2019-12-02T18:52:55Z")

</div>

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