Parse local datetime with timex

I am struggling to parse a local datetime with timex.

The datetime string is like “26/10/2018 10:00” and I know it is CET or CEST. I managed to parse it with:

Timex.parse!(time_str, "{0D}/{0M}/{YYYY} {h24}:{m}")

I then receive the time in utc which is wrong. I could convert it to cet with

Timex.Timezone.convert("Europe/Berlin")

But then I get 12:00 CEST and not 10:00 CEST.

I tried adding the timezone to the datetime string, but I need the offset to parse with %Z, I dont know the offset.

Any ideas?

Use Timex.to_datetime/2:

iex> Timex.parse!("26/10/2018 10:00", "{0D}/{0M}/{YYYY} {h24}:{m}") |> Timex.to_datetime("Europe/Berlin")
#DateTime<2018-10-26 10:00:00+02:00 CEST Europe/Berlin>
2 Likes

Thx, that was quick… and it worked.

1 Like