Elixir: Support greater precision than 6 in DateTime

Created on 4 Jul 2017  路  8Comments  路  Source: elixir-lang/elixir

Environment

  • Elixir & Erlang versions (elixir --version): 1.4.4
  • Operating system: Mac OS X Sierra

Current behavior

Currently DateTime's fractional part's precision is limited to 6 digits (https://github.com/elixir-lang/elixir/blob/5fc7ee129ddfe534cdc6d9b1e2b5eb90b698c811/lib/elixir/lib/calendar/iso.ex#L277)

That prevents from using Date/Time with greater precision which obviously rarely is the case but once you're operating in domain where systems have to be precise that becames a major bottleneck.

Moreover, in widespread standards such as ISO8601 there's no mention of hardcoded precision which makes Elixir's DateTime inferior towards them.

Moreover see https://github.com/bitwalker/timex/issues/318.

Expected behavior

DateTime's fractional part has unlimited precision.

Most helpful comment

If you don't reject the idea by itself, isn't it better to leave this this for 2.0? (I assume there will be 2.0 one day).

All 8 comments

We can't do this change without breaking backwards compatibility so if you need to work with higher precisions, your best bet is to introduce a new type which is focused on high precision values.

If you don't reject the idea by itself, isn't it better to leave this this for 2.0? (I assume there will be 2.0 one day).

Even on Elixir 2.0, we want to minimize breaking changes as much as possible, otherwise we will cause a lot of churn in the ecosystem or people will simply refuse to migrate. We currently have a field named microsecond and renaming the microsecond field would simply break a bunch of libraries and applications, which is not worth doing even on a major release.

One alternative if you don't want to reinvent a DateTime API is to carry the precision on the side and keep the microseconds on the datetime itself as 0.

Well I know what alternatives are :) I just thought that it would be cool to support this in the core, although I see your point.

@mspanc if you settle for one approach, please document it here if possible, as it may help others who may need the same feature. Thank you!

Could add a 'submicrosecond' field or so perhaps (or in 2.0)?

This additional field would again need to have a hardcoded precision or it does need to be some kind of fixed point value, which aren't currently supported in elixir core for reasons.

If though those were added, we could introduce another field which takes arbitrary precision of fractional seconds while still keeping microseconds in the mix and keep them insync via official API. Problem is that people still could update one without the other by directly accessing the struct.

Was this page helpful?
0 / 5 - 0 ratings