Elixir: DateTime negative microseconds and zero_pad failure

Created on 21 Sep 2017  路  7Comments  路  Source: elixir-lang/elixir

Environment

  • Elixir 1.5.1 OTP 20.0
  • Operating system: Mac

Current behavior

DateTime.from_unix!(-100000, :microsecond)

https://github.com/elixir-lang/elixir/blob/f0ba0b4aa339cdea1e489938e3a0cdd0eee2e524/lib/elixir/lib/calendar/iso.ex#L335

The zero_pad function should ignore the negative sign when adding zeros.

Elixir Bug Starter

All 7 comments

I'm happy to take this if no one is already?

Please do!

Great! I'll get started.

If this is still open, I'd like to pick this up.

@vishaldeepak I'm working on this today. If I make no progress will let you know. :+1:

If I'm understanding correctly, the actual issue is that calling DateTime.from_unix/2 with a negative value (which is not a multiple of 1_000_000), and :microsecond unit returns a DateTime struct with a negative microsecond value. e.g.

iex(1)> DateTime.from_unix!(-1, :microsecond)
#DateTime<1970-01-01 00:00:00.0000-1Z>

when we would expect the following:

iex(1)> DateTime.from_unix!(-1, :microsecond)
#DateTime<1969-12-31 23:59:59.999999Z>

I have a fix for this which I'll submit tomorrow. If I haven't understood this correctly, or there's a good reason to keep the negative microsecond output please let me know!

No, there is no reason to keep it negative.

Was this page helpful?
0 / 5 - 0 ratings