First of all I know there are a lot threads about this and I could use the solutions there, f.e. write a python function which converts datetime object to the timezone I need and add this as a filter to jinja2.
But I wanted to skip this step.
Usually PostgreSQL saves datetime objects in UTC
.
What I did is: I changed the default timezone which postgreSQL uses to save datetime objects to: Europe/Berlin
ALTER DATABASE postgres SET timezone TO 'Europe/Berlin';
This worked, SHOW timezone;
tells me its Europe/Berlin
.
But what Postgres does, it still saves the datetime as UTC, but with a +1 offset for Europe/Berlin, which is actually not a problem.
I assumed that when reading the datetime object, Postgres would apply the offset and return the Europe/Berlin time, but it doesn't. It still returns the UTC time, so whats the point in changing the timezone?
I still need to write this filter and manually convert it.
I think there should be a simple way to apply the offset on DB level, or am I wrong?
EDIT
Some information, which might help, the model:
class User(UserMixin, Base):
__tablename__ = 'users'
date_added = Column(DateTime(timezone=True), nullable=False)
How it looks in the DB:
And here is an example how I load it in jinja2/html:
{{ tenant.date_added.strftime('%d.%m.%Y um %H:%M Uhr') }}