Skip to content

Conversation

@Qqwy
Copy link
Contributor

@Qqwy Qqwy commented Aug 11, 2016

Fixes #5124:

  • Unix times back to -62167219200000000 microseconds (0 Gregorian seconds) are now supported by DateTime.from_unix/2.
  • For lower integers, :error will be returned instead of {:ok, datetime}. The bang-version of the method will raise an ArgumentError in these cases.
  • Usage of negative Unix times has been documented.
  • A couple of tests were added to ensure that this is indeed the behaviour that is happening.
  • DateTime.to_unix has similarly been altered to support all DateTimes with year >= 0, which matches the new cutoff-point for DateTime.from_unix.
  • When passing a DateTime with a negative year to DateTime.from_unix, a descriptive ArgumentError is raised.

Unix times are always in UTC and therefore the DateTime
will be returned in UTC.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra new line. ✂️ :D

@josevalim
Copy link
Member

This looks great @Qqwy! I have added two comments and I would love @lau's review on this too.

@Qqwy
Copy link
Contributor Author

Qqwy commented Aug 11, 2016

Thanks for your comments!

I've added tests for DateTime.to_unix, and made it raise a descriptive ArgumentError if a DateTime before 0 Gregorian Seconds is passed in.

end

def to_unix(%DateTime{year: year}, _unit) do
raise ArgumentError, "Can only convert %DateTime{} to Unix time with a year >= 0, got: #{year}"
Copy link
Member

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..." :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know!

@Qqwy Qqwy force-pushed the enhancement/negative_datetime_from_unix branch from 2d1792a to 9c44a65 Compare August 11, 2016 08:30
@Qqwy Qqwy force-pushed the enhancement/negative_datetime_from_unix branch from 9c44a65 to 76fa458 Compare August 11, 2016 09:42
{: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
Copy link
Member

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

Copy link
Contributor

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.

Copy link
Contributor Author

@Qqwy Qqwy Aug 11, 2016

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! :-)

@Qqwy
Copy link
Contributor Author

Qqwy commented Aug 12, 2016

@lau: Could you look at this one more time, to see if it's :shipit:-ready?

end

def to_unix(%DateTime{year: year}, _unit) do
raise ArgumentError, "can only convert %DateTime{} to Unix time with a year >= 0, got: #{year}"
Copy link
Member

@lexmag lexmag Aug 12, 2016

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.

Copy link
Contributor Author

@Qqwy Qqwy Aug 12, 2016

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@josevalim josevalim merged commit 5c08f86 into elixir-lang:master Aug 12, 2016
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

josevalim pushed a commit that referenced this pull request Aug 12, 2016
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

6 participants