DateTime.add is not working. Can any one help with this

I have UTC iso datetime and I want to add 900 Second into this datetime.

I read the doc to add time into DateTime. Even given example also not working I am getting Error "UndefinedFunctionError ". Help me guys.
I don’t want to use any third-party dependency.

Elixir DateTime core code
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/calendar/datetime.ex

Here is my code.

iso_datetime = DateTime.utc_now()|>DateTime.to_iso8601

IO.inspect(iso_datetime)

{:ok, datetime, 0}= DateTime.from_iso8601(iso_datetime)
IO.inspect(datetime)

# new_datetime = DateTime.add(datetime, 900, :second, Calendar.get_time_zone_database())

new_datetime = DateTime.add(datetime, 900, :second, "Etc/UTC")

IO.inspect(new_datetime)

Here is result

"2019-09-25T12:32:19.092915Z"
#DateTime<2019-09-25 12:32:19.092915Z>

** (UndefinedFunctionError) function DateTime.add/4 is undefined or private
    (elixir) DateTime.add(#DateTime<2019-09-25 12:32:19.092915Z>, 900, :second, "Etc/UTC")
    

Whats your version of elixir, DateTime.add/4 is only available since 1.8.0 according to the documentation.

Mix 1.6.6 (compiled with OTP 19).

But how can we do this in Elixir Version “1.6.6”

Thats your mix version, but lucky you, as its bundled with and the version is locked to be the same as elixir, I can tell you that you need to upgrade your elixir to at least 1.8.0 to be able to use the function mentioned above.

Perhaps it is possible through timex, still I’d upgrade elixir.

2 Likes

Thanks @NobbZ
I will update my Elixir version and migrate my code to updated version.

Unless you have “imperative assignments”, there shouldn’t be any problem upgrading.

I’d suggest though, to upgrade to 1.7 first and tackle all warnings before upgrading further.

3 Likes