# Help with the logstash ruby filter.Ruby exception occurred: undefined method \`time' for 1546441403:Fixnum

**URL:** https://discuss.elastic.co/t/help-with-the-logstash-ruby-filter-ruby-exception-occurred-undefined-method-time-for-1546441403-fixnum/162676
**Category:** Logstash
**Created:** [January 2, 2019, 3:13pm UTC](https://discuss.elastic.co/t/help-with-the-logstash-ruby-filter-ruby-exception-occurred-undefined-method-time-for-1546441403-fixnum/162676 "2019-01-02T15:13:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![simmonHuong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simmonhuong/32/32586_2.png) [@simmonHuong](https://discuss.elastic.co/u/simmonHuong)
#### Post date: [January 2, 2019, 3:13pm UTC](https://discuss.elastic.co/t/help-with-the-logstash-ruby-filter-ruby-exception-occurred-undefined-method-time-for-1546441403-fixnum/162676/1 "2019-01-02T15:13:55Z")

</div>

```auto

filter {
    if [is_try] == 1 {
         drop { }
    } 

    ruby { 
         code => "

            fmt = '%Y-%m-%dT%H:%M:%S.%L%z'
            tz = '+08:00'

            create_time = event.get('[create_time]').time
            update_time = event.get('[update_time]').time

            event.set('create_time', create_time.localtime(tz).strftime(fmt))
            event.set('update_time', update_time.localtime(tz).strftime(fmt))

            event.set('yearandmonth', event.get('[create_time]').to_time.localtime(tz).strftime('%Y%m'))

            all_money = event.get('cash') + event.get('points') 
            event.set('all_money', all_money)

                 "
    } 
}

```

The above code will end up with

```auto
[2019-01-02T23:06:00,402][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `time' for 1546441403:Fixnum

```

Struggled for hours,please help.

1. why this exception occurs?
2. the variable `yearandmonth` will not work ,instead it results a index name like this `_dev_%{yearandmonth}`

---

<div class="post-metadata">

### Author: ![simmonHuong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simmonhuong/32/32586_2.png) [@simmonHuong](https://discuss.elastic.co/u/simmonHuong)
#### Post date: [January 2, 2019, 5:18pm UTC](https://discuss.elastic.co/t/help-with-the-logstash-ruby-filter-ruby-exception-occurred-undefined-method-time-for-1546441403-fixnum/162676/2 "2019-01-02T17:18:20Z")

</div>

Somehow I figured out how to make it right.  
It really made me hair lose,lucky for you.

```auto
filter {
    if [is_try] == 1 {
         drop { }
    } 

    ruby { 
         code => "
                  require 'time'
                  require 'date'

                  fmt = '%Y-%m-%dT%H:%M:%S.%L%z'
                  tz = '+08:00'

                  create_time = event.get('[create_time]')
                  update_time = event.get('[update_time]')

                  event.set('create_time', Time.at(create_time).localtime(tz).strftime(fmt))
                  event.set('update_time', Time.at(update_time).localtime(tz).strftime(fmt))

                  event.set('yearandmonth', Time.at(create_time).localtime(tz).strftime('%Y%m'))

                  all_money = event.get('cash') + event.get('points')
                  event.set('all_money', all_money)
                 "
    } 
}

```

---

<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: [January 30, 2019, 5:18pm UTC](https://discuss.elastic.co/t/help-with-the-logstash-ruby-filter-ruby-exception-occurred-undefined-method-time-for-1546441403-fixnum/162676/3 "2019-01-30T17:18:24Z")

</div>

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