tj0

tj0

Ecto / Postgres timestamp before ‘year 0’

I’m currently trying to store dates in Postgresql that are before ISO8601 ‘year 0’ and am running into issues. Postgresql stores dates internally as JD and can correctly insert/remove dates. I’m storing the date generally as NaiveDateTime and it works fine for anything > year 0.

For instance, the following query works fine in SQL.

sql> insert into table1 set t = '1000-01-01BC 00:00:00'; 

However, trying to load this in Ecto doesn’t seem to work as we’re referring to a calendar function that doesn’t have any representation of times before 0.

    (stdlib 3.13) calendar.erl:257: :calendar.last_day_of_the_month/2
    (stdlib 3.13) calendar.erl:133: :calendar.date_to_gregorian_days/3
    (stdlib 3.13) calendar.erl:155: :calendar.datetime_to_gregorian_seconds/1
    (postgrex 0.15.8) lib/postgrex/extensions/timestamp.ex:48: Postgrex.Extensions.Timestamp.encode_elixir/1
    (postgrex 0.15.8) lib/postgrex/type_module.ex:897: Postgrex.DefaultTypes.encode_params/3
    (postgrex 0.15.8) lib/postgrex/query.ex:75: DBConnection.Query.Postgrex.Query.encode/3

-spec datetime_to_gregorian_seconds(DateTime) -> Seconds when
      DateTime :: datetime(),
      Seconds :: non_neg_integer().
datetime_to_gregorian_seconds({Date, Time}) ->
    ?SECONDS_PER_DAY*date_to_gregorian_days(Date) +
        time_to_seconds(Time).

What’s the appropriate way for dealing with this? As far as I know, erlang/elixir doesn’t have any methodology for monkey-patching, the only thing I could think of is to store these dates in an alternative format as a workaround. Any thoughts?

Most Liked

kip

kip

ex_cldr Core Team

I think thats a bug in Postgrex (I will open an issue and a PR) since t:NaiveDateTime supports negative years I think its reasonable that Postgrex should.

As an alternative I would suggest implementing your own Ecto custom type (if you’re using Ecto) so you can do your own encoding.

EDIT: This issue is already on the Postgrex issue tracker

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement