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

Last Post!

tj0

tj0

Awesome. I looked through the code and I patched the library in this branch which could serve as a starting point for a PR. It seems to be working (at least for me).

It appears to me only two files needed modification, but I suppose the patch could be bigger to cleanup all instances of :calendar. And I have no idea what Jose is talking about “internal rata die” representation, so I’m just going to use the branch for now and watch and learn.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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 31525 112
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement