# `Bylaw.Credo.Check.Elixir.SafeDateTimeComparison`
[🔗](https://github.com/ryanzidago/bylaw/blob/v0.1.0-alpha.1/lib/bylaw/credo/check/elixir/safe_datetime_comparison.ex#L1)

## Basics

> #### This check is disabled by default. {: .neutral}
>
> [Learn how to enable it](`e:credo:config_file.html#checks`) via `.credo.exs`.

This check has a base priority of `higher` and works with any version of Elixir.

## Explanation

Avoid direct comparison operators on values that look like dates or times.

## Examples

Avoid:

      entry.inserted_at > cutoff_at
      start_date <= end_date

Prefer:

      DateTime.after?(entry.inserted_at, cutoff_at)
      Date.compare(start_date, end_date) in [:lt, :eq]

## Notes

Elixir's term ordering can compare structs even when the comparison is
not the domain comparison you meant. Date and time types have comparison
functions that encode the correct semantics.

Use `compare/2`, `before?/2`, or `after?/2` from the relevant date/time
module. Ecto `where` clauses are ignored because query comparisons are
translated by Ecto instead of using Elixir term ordering.

This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.

## Options

Configure options in `.credo.exs` with the check tuple:

```elixir
%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Elixir.SafeDateTimeComparison,
         [
           datetime_suffixes: ~w(_datetime _at _date _time)
         ]}
      ]
    }
  ]
}
```

- `:datetime_suffixes` - Variable and field suffixes that make a value look like a date or time. Defaults to `_datetime`, `_at`, `_date`, and `_time`.

## Usage

Add this check to Credo's `checks:` list in `.credo.exs`:

```elixir
%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Elixir.SafeDateTimeComparison, []}
      ]
    }
  ]
}
```

## Check-Specific Parameters

Use the following parameters to configure this check:

### `:datetime_suffixes`

  Variable and field suffixes that make a value look like a date or time.
  Defaults to `_datetime`, `_at`, `_date`, and `_time`.
  

*This parameter defaults to* `["_datetime", "_at", "_date", "_time"]`.

## General Parameters

Like with all checks, [general params](`e:credo:check_params.html`) can be applied.

Parameters can be configured via the [`.credo.exs` config file](`e:credo:config_file.html`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
