Skip to content

Commit

Permalink
Remove year 9999 restriction, closes #687
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 5, 2024
1 parent 0eca1ac commit ea802cc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
16 changes: 3 additions & 13 deletions lib/postgrex/extensions/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ defmodule Postgrex.Extensions.Date do
use Postgrex.BinaryExtension, send: "date_send"

@gd_epoch Date.to_gregorian_days(~D[2000-01-01])
@max_year 9999

# Latest date supported by Elixir. Postgresql
# supports later dates.
@max_days Date.to_gregorian_days(~D[9999-12-31])

# Elixir supports earlier dates but this is the
# earliest supported in Postgresql.
Expand All @@ -33,23 +28,18 @@ defmodule Postgrex.Extensions.Date do

## Helpers

def encode_elixir(%Date{year: year} = date) when year <= @max_year do
def encode_elixir(date) do
<<4::int32(), Date.to_gregorian_days(date) - @gd_epoch::int32()>>
end

def encode_elixir(%Date{} = date) do
raise ArgumentError, "#{inspect(date)} is beyond the maximum year #{@max_year}"
end

def day_to_elixir(days) do
days = days + @gd_epoch

if days in @min_days..@max_days do
if days > @min_days do
Date.from_gregorian_days(days)
else
raise ArgumentError,
"Postgrex can only decode dates with days between #{@min_days} and #{@max_days}, " <>
"got: #{inspect(days)}"
"Postgrex can only decode dates with days after #{@min_days}, got: #{inspect(days)}"
end
end
end
6 changes: 0 additions & 6 deletions test/calendar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ defmodule CalendarTest do
assert [[~D[-0001-01-01]]] = query("SELECT date 'January 1, 2 BC'", [])
end

test "decode date upper bound error", context do
assert_raise ArgumentError, fn ->
query("SELECT date '10000-01-01'", [])
end
end

test "decode timestamp", context do
assert [[~N[2001-01-01 00:00:00.000000]]] =
query("SELECT timestamp '2001-01-01 00:00:00'", [])
Expand Down

0 comments on commit ea802cc

Please sign in to comment.