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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement