-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Enhancement: negative integer support in DateTime.from_unix
#5128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: negative integer support in DateTime.from_unix
#5128
Conversation
lib/elixir/lib/calendar.ex
Outdated
| Unix times are always in UTC and therefore the DateTime | ||
| will be returned in UTC. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra new line. ✂️ :D
|
Thanks for your comments! I've added tests for |
lib/elixir/lib/calendar.ex
Outdated
| end | ||
|
|
||
| def to_unix(%DateTime{year: year}, _unit) do | ||
| raise ArgumentError, "Can only convert %DateTime{} to Unix time with a year >= 0, got: #{year}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't capitalize the first letter of raised error messages in Elixir, so this would be "can only convert..." :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to know!
2d1792a to
9c44a65
Compare
9c44a65 to
76fa458
Compare
lib/elixir/lib/calendar.ex
Outdated
| {:ok, %DateTime{year: year, month: month, day: day, | ||
| hour: hour, minute: minute, second: second, microsecond: {microsecond, precision}, | ||
| std_offset: 0, utc_offset: 0, zone_abbr: "UTC", time_zone: "Etc/UTC"}} | ||
| if total < -62167219200000000 do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use an attribute here to avoid a "magic number" please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have @unix_epoch already which is 62167219200 and could be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely agree. I was looking for a away to remove the magic number here, but I hadn't thought that the computed value of @unix_epoch would match it. Thank you, @fishcakez and @lau! :-)
…o microseconds) in documentation.
|
@lau: Could you look at this one more time, to see if it's |
lib/elixir/lib/calendar.ex
Outdated
| end | ||
|
|
||
| def to_unix(%DateTime{year: year}, _unit) do | ||
| raise ArgumentError, "can only convert %DateTime{} to Unix time with a year >= 0, got: #{year}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to not add this clause as this error can be misleading, for example, for a DateTime with non-ISO calendar.
It's acceptable to raise FunctionClauseError, we do that in similar cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raising an ArgumentError seems more logical to me, because it states 'the input is wrong', rather than 'there is a clause missing'.
But that probably is something for a different discussion. For now, as similar cases raise FunctionClauseError, I will remove this extra clause.
| year: 46302, zone_abbr: "UTC"}} | ||
| Negative Unix times are supported, up to -#{@unix_epoch} seconds, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
|
❤️ 💚 💙 💛 💜 |
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
Fixes #5124:
DateTime.from_unix/2.:errorwill be returned instead of{:ok, datetime}. The bang-version of the method will raise an ArgumentError in these cases.DateTime.to_unixhas similarly been altered to support all DateTimes withyear >= 0, which matches the new cutoff-point forDateTime.from_unix.DateTime.from_unix, a descriptive ArgumentError is raised.