Convert datetime to another timezone in logstash

You can do it in ruby.

    mutate { add_field => { "rt" => "Jun 16 2023 11:24:40 GMT" } }
    date { match => [ "rt", "MMM dd YYYY HH:mm:ss ZZZ" ] }

    ruby {
        code => '
            t = event.get("[@timestamp]").to_f
            t = Time.at(t, in: "+08:00")
            event.set("localtime", t.strftime("%b %d %Y %H:%M:%S MYT"))
        '
    }

will produce

 "localtime" => "Jun 16 2023 19:24:40 MYT",
"@timestamp" => 2023-06-16T11:24:40.000Z,

I don't think core Ruby has enough timezone support to avoid specifying both the offset and the name of MYT.

1 Like