Skip to content

mention alternatives to compare/2 for comparison in other dates and times #14151

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

Conversation

roelandvanbatenburg
Copy link
Contributor

Similar to #14147

During our upgrade to Elixir 1.18 we got some compile warnings about the usage of == on DateTime structs. https://hexdocs.pm/elixir/1.18.1/DateTime.html suggested using compare/2 which led to replacing

  if user.password_reset_token_sent_at > oldest_allowed_token_datetime do

with

  if DateTime.compare(user.password_reset_token_sent_at, oldest_allowed_token_datetime) == :gt do

However, a far better solution is:

elixir
if DateTime.after?(user.password_reset_token_sent_at, oldest_allowed_token_datetime) do


To make it easier for developers to think of these functions (`after?/2` and `before?/2`), I suggest mentioning them alongside `compare/2`.

@josevalim josevalim merged commit 9256143 into elixir-lang:main Jan 7, 2025
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@roelandvanbatenburg roelandvanbatenburg deleted the mention_alternatives_to_compare branch January 7, 2025 07:18
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.

3 participants