# Converting timezones in Logstash - HOWTO

**URL:** https://discuss.elastic.co/t/converting-timezones-in-logstash-howto/333821
**Category:** Logstash
**Created:** [May 18, 2023, 10:29pm UTC](https://discuss.elastic.co/t/converting-timezones-in-logstash-howto/333821 "2023-05-18T22:29:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nbertram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nbertram/32/121187_2.png) [@nbertram](https://discuss.elastic.co/u/nbertram)
#### Post date: [May 18, 2023, 10:29pm UTC](https://discuss.elastic.co/t/converting-timezones-in-logstash-howto/333821/1 "2023-05-18T22:29:36Z")

</div>

Hi,

After trawling a lot of the internet asking how to convert a timestamp from UTC to local time in Logstash I came up blank, and against a whole bunch of answers on here saying "don't - leave that to the presentation layer".

If, however, you're using say stdout or file output, then Logstash _is_ your presentation layer so it might be helpful to know how to do it.

In the end, I've come to a solution using a Ruby filter that calls into Java to do this, and I thought I'd share it so anyone else on this quest at least finds _one_ way it can be done.

So in a file `local_date.rb`:

```ruby
def register(params)
    @tz = java.time.ZoneId.of(params["timezone"])
    @date_format = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd")
end

def filter(event)
    zoned_dt = java.time.ZonedDateTime.ofInstant(event.get("@timestamp").to_java.toInstant, @tz).truncatedTo(java.time.temporal.ChronoUnit::SECONDS)
    event.set("@timestamp_local", zoned_dt.format(java.time.format.DateTimeFormatter::ISO_OFFSET_DATE_TIME))
    event.set("@date_local", zoned_dt.format(@date_format))
    return [event]
end

```

then use it in a config like this:

```auto
input {
    generator {
        lines => [
            "hello",
            "world",
            "this is lots of loggy"
        ]
        count => 1
    }
}

filter {
  ruby {
    path => "/path/to/local_date.rb"
    script_params => {
        timezone => "Pacific/Auckland"
    }
  }
}

output {
  stdout {
    codec => line {
      format => "[%{@timestamp_local}] %{message}"
    }
  }
}

```

I hope someone finds this useful! Or if there's a better way, I'm all ears. There's not a lot of API documentation around the internal datetime representation, so I just went off the code. It didn't look like there was a way to do proper timezones inside the Ruby realm with the gems that were available.

---

<div class="post-metadata">

### Author: ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)
#### Post date: [May 19, 2023, 8:25am UTC](https://discuss.elastic.co/t/converting-timezones-in-logstash-howto/333821/2 "2023-05-19T08:25:41Z")

</div>

LS is using @timestamp from source if is provided or by default from the host where LS has been running.  
ES is using UTC,so LS will always send date fields in UTC format.

If source need to change a time zone, there is [the date plugin](https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html) and you can set your timezone of data.

```auto
    date {
       match => ["timestamp", "ISO8601"]
       timezone=> "Pacific/Auckland"
       target=> "yourfield" # default is @timestamp
    }

```

So if you have 10 fields, you will have 10 date conversions with timezone as you wish.

Kibana will present data based your local time settings or you can set the same time as at source.

---

<div class="post-metadata">

### Author: ![nbertram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nbertram/32/121187_2.png) [@nbertram](https://discuss.elastic.co/u/nbertram)
#### Post date: [May 21, 2023, 9:12pm UTC](https://discuss.elastic.co/t/converting-timezones-in-logstash-howto/333821/3 "2023-05-21T21:12:27Z")

</div>

> [@nbertram](#):
>
> `Unit::SECONDS)`

Sure, except in this instance there's no Kibana, no Elasticsearch. Just Logstash.

The date filter interprets which timezone the incoming date is in, not the output. The output as you say is always UTC.

I had UTC coming in, stored in @timestamp, and wanted to write to log files using local timezone, which is what my solution is for.

---

<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: [June 18, 2023, 9:12pm UTC](https://discuss.elastic.co/t/converting-timezones-in-logstash-howto/333821/4 "2023-06-18T21:12:41Z")

</div>

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