Search code examples
datetimeelixirecto

Converting Ecto.DateTime to Erlang datetime tuple using to_erl


I have a Ecto.DateTime I'm trying to extract information from.

This is working fine:

{{y, m, d}, _} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"

I'm trying to get the hours/minute/second values now:

{{y, m, d}, {h,m,s}} = Ecto.DateTime.to_erl(date)
"#{m}/#{d}/#{y}"

But I get this error

no match of right hand side value: {{2017, 5, 5}, {12, 0, 0}}


Solution

  • You're reusing the variable name m in the pattern, which means this'll only work if the month and the minute values are the same. You need to use different names, e.g.

    {{y, m, d}, {h, min, s}} = Ecto.DateTime.to_erl(date)
    

    or

    {{y, mon, d}, {h, m, s}} = Ecto.DateTime.to_erl(date)
    
    iex(1)> {a, a} = {1, 2}
    ** (MatchError) no match of right hand side value: {1, 2}
    
    iex(1)> {a, a} = {1, 1}
    {1, 1}