I am evaluating Prisma and I am a complete noob...
I am using Postgresql
I have the following model definition
model Sth {
id Int #default(autoincrement()) #id
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
expiresAt DateTime?
}
The createdAt column translates to
createdAt | timestamp(3) without time zone | | not null | CURRENT_TIMESTAMP
Since I am planing to really work with the timestamps - I need them to be timestamp with time zone.
How can I achieve this with Prisma?
Edit NOW() > '2021-02-16': Prisma now, has the "native types"
https://github.com/prisma/prisma/releases/tag/2.17.0
https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#postgresql-6
For those of us living in the future, it is now possible to save records with timestamp with timezone for postgresql through Prisma's 'Native database type attribute'.
https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#postgresql-6
Please note that for my database I had to set the timezone to 'UTC' so that the correct timezone is set to what I wanted by #default.
The above example with the Native database type attributes.
model Sth {
id Int #default(autoincrement()) #id
createdAt DateTime #default(now()) #db.Timestamptz(3)
updatedAt DateTime #updatedAt #db.Timestamptz(3)
expiresAt DateTime? #db.Timestamptz(3)
}
Currently the timestamptz field is not supported as Prisma automatically converts the Timestamp you sent to UTC. The support will be available in a further version of Prisma via this request.
As a workaround, you would need to convert the timestamp to a specific required timezone as Prisma would save it in UTC in the DB.
Related
select created_at, CONVERT_TZ(created_at, 'UTC', 'IST') as local_created_at
from posts
WHERE created_at between CONVERT_TZ('01-07-2022 00:00:00','IST','UTC') AND CONVERT_TZ('30-07-2022 23:59:59','IST','UTC').
I have stored the data values in UTC in database. Now How will be above the query in mongoose ? Here all the IST are the user's timezone (it will be dynamic, based on user's timezone) and the date value (in above example '01-07-2022 00:00:00' and '30-07-2022 23:59:59') is also dynamic. This date value will be provided in the user's current time zone. As example here both 01-07-2022 00:00:00' and '30-07-2022 23:59:59' are provided in IST timezone.
I am trying to save datetime with timezone in Postgres but can not find anything usefull.
Input would be like,
2021-03-21T12:24:30Z
2021-03-21T12:24:30PST'
and i am expecting output in the datetime column like,
'2001-02-16 18:38:40+05:30'
'2001-02-16 18:38:40+00'
what would be the datatype in column and what's the way i can save the date time including timezone.
You should use the TIMESTAMPTZ data type for the column - https://www.postgresql.org/docs/9.5/datatype-datetime.html
Table column is creating with:
#Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
private OffsetDateTime timeStamp;
When Instant epoch value (ex:2020-05-25T16:16:16Z) is saved it is appending the local time hours in DB (ex: 2020-05-26 04:16:16+12).
Please suggest how can we enforce the same UTC value to be stored in DB.
I have a datetime object from a sitemap in this format -
2014-12-29 05:00:38-05:00
How can I store this datetime in POSTGRES?
Use timestamp with time zone:
CREATE TABLE tab(col timestamp with time zone);
INSERT INTO tab
VALUES ('2014-12-29 05:00:38-05:00');
SELECT *
FROM tab;
SqlFiddleDemo
This seems like it would be a common problem so perhaps I am missing something obvious. Using Joda Time I want to persist dates with a timezone. If I wasn't using JPA I would want to do something like this to save and retrieve the timestamps:
create table test_dates
(
zone varchar(50),
ts_with_time_zone timestamp with time zone
);
insert into test_dates values ('UTC', '2012-08-12 12:34:56 UTC');
insert into test_dates values ('MST', '2012-08-12 05:34:56 MST');
select 'UTC in MST', ts_with_time_zone at time zone 'MST'
from test_dates where zone = 'UTC';
How then can I save and retrieve these timestamps with the specified timezone using Joda Time, JPA, EclipseLink and Postgres?
I have seen answers using converters but I am not sure how you would specify or use time zones. Sure I could get the UTC time and convert it myself but that defeats the purpose of having the "with time zone" column. Is this possible?
The ideal solution would be if EclipseLink handled JodaTime similar to hibernate.
You may be able to customize your PostgreSQLPlatform to add support for storing the time-zone through JDBC. Your JDBC driver will have to support the time-zone (most likely through a custom type, or a Calendar API).
EclipseLink does have support for time-zones on Oracle, so it should be possible to extend the PostgreSQLPlatform for time-zones as well if the database and JDBC drivers support it.
You don't save the time zone. You just save the timestamp with the time zone specified in the input string. The time zone will not be saved.
select '2012-08-12 12:34:56 UTC'::timestamp with time zone;
timestamptz
------------------------
2012-08-12 09:34:56-03
select '2012-08-12 05:34:56 MST'::timestamp with time zone;
timestamptz
------------------------
2012-08-12 09:34:56-03
The timezone will be the database timezone.
select now()::timestamp with time zone;
now
-------------------------------
2012-12-10 12:26:16.318484-02