Closed dfrese closed 10 months ago
Ecto type is :utc_datetime, but Oracle type is TIMESTAMP(0) WITH TIME ZONE So, stored value must be AT TIME ZONE 'Etc/UTC'
insert into xxx (uuu) values (SYSTIMESTAMP AT TIME ZONE 'Etc/UTC')
not
insert into xxx (uuu) values (SYSTIMESTAMP)
If not, then convertion required
Jamdb.Xxx |> select([r], fragment("? AT TIME ZONE 'Etc/UTC'", r.uuu)) |> Jamdb.Repo.all
The idea was that in application you have date in localtime or utctime or any time format then you convert it to utc and store to database
I see. But I did not insert the timestamp explicitly; this is done automatically - I think by Ecto - when using https://hexdocs.pm/ecto/Ecto.Schema.html#timestamps/1
In my case like so:
schema "mytable" do
timestamps(
inserted_at: :timestamp,
type: :utc_datetime,
inserted_at_source: :timestamp,
updated_at: false
)
Would be really nice if this would work out-of-the-box.
I'm not sure if Ecto uses a Database-Feature, or if it takes the application's current time.
In the to_date
function above, tz
is '+02:00'
in my case.
Maybe that needs to be parsed into utc_offset
?
defp encode(%DateTime{microsecond: {0, 0}, zone_abbr: "UTC"} = datetime) do
{date, {hour, min, sec}} = NaiveDateTime.to_erl(DateTime.to_naive(datetime))
{date, {hour, min, sec, 0}, 28}
end
defp encode(%DateTime{microsecond: {ms, _}, zone_abbr: "UTC"} = datetime) do
{date, {hour, min, sec}} = NaiveDateTime.to_erl(DateTime.to_naive(datetime))
{date, {hour, min, sec, ms}, 28}
end
Now stored value tz is UTC +00:00
got #DateTime<2023-10-11 16:01:08.000000+00:00 UTC +00:00>
Thanks!
Hi there,
I used 'timestamps' in an Ecto Schema with type
:utc_datetime
, and the corresponding table created via a migration also has:utc_datetime
as the type for the field.But reading it fails with
Changing
time_zone
fromto_binary(tz)
to"Etc/UTC"
injamdb_oracle.ex
fixed it, although that's probably not the "proper" fix: