Convert UTC datetime to specified timezone

Hello,

I have this following date : 2020-02-05 15:31:46 UTC
I would like to convert it with ruby into Europe/Brussels.

I tried :

puts Time.parse(mydate2) // 2020-02-05 15:31:46 UTC
puts Time.parse(mydate2).zone // UTC 
puts Time.parse(mydate2).in_time_zone('Europe/Brussels') // Ruby exception occurred: undefined method `in_time_zone' for 2020-02-05 15:31:46 UTC:Time

How can I convert an event.date UTC to datetime on Europe/Brussels Timezone ?

Thanks

in_time_zone is a rails thing, not present in basic Ruby. But you do not need a ruby filter to do this, you can do it using a date filter. If you want to move from UTC to Europe/Brussels, which is Etc/GMT-1, then tell the date filter it is actually Etc/GMT+1, or Atlantic/Azores.

input { generator { count => 1 lines => [ '' ] } }
filter {
    mutate { add_field => { "timestamp" => "2020-02-05 15:31:46 UTC" } }
    mutate { gsub => [ "timestamp", " UTC", "" ] }
    date { match => [ "timestamp", "YYYY-MM-dd HH:mm:ss" ] timezone => "Atlantic/Azores" target => "timestamp2" }
    mutate { convert => { "timestamp2" => "string" } }
    mutate { gsub => [ "timestamp2", "Z$", "" ] }
}
output { stdout { codec => rubydebug { metadata => false } } }

will produce

 "timestamp" => "2020-02-05 15:31:46",
"timestamp2" => "2020-02-05T16:31:46.000",

Hey Badger,

I will give a try this morning !

Thanks for your time, didn't know about the date filter plugin.

Seems not working.

mutate { add_field => { "timestamp" => "2020-06-08 07:31:46 UTC" } }
mutate { gsub => [ "timestamp", " UTC", "" ] }
date { match => [ "timestamp", "YYYY-MM-dd HH:mm:ss" ] timezone => "Europe/Brussels" target => "timestamp2" }
mutate { convert => { "timestamp2" => "string" } }
mutate { gsub => [ "timestamp2", "Z$", "" ] }

Result :

{
    "timestamp" => "2020-06-08 07:31:46",
    "timestamp2" => "2020-06-08T05:31:46.000"
}

Timestamps2 should have been 09:31:46.
I have the feeling that "timezone" property is the timezone of the given match.

The date plugin filter seems to convert to UTC in anyway.

That's why you have to tell it the timezone of the log file has the opposite sign to the adjustment you want to apply. Atlantic/Azores, not Europe/Brussels.

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