Set a default timezone

62 views
Skip to first unread message

Mamisoa Andriantafika

unread,
Mar 28, 2021, 1:37:18 PM3/28/21
to py4web
Hi, 

I noticed that every datetime is set with UTC.

How can I setup py4web to use a local TZ time?

I try to add in common.py:

import os,time
# set local tz
os.environ['TZ'] = 'Europe/Brussels'
time.tzset()

but it does not work.

Mamisoa

Mamisoa Andriantafika

unread,
Mar 30, 2021, 4:33:11 AM3/30/21
to py4web
@massimo:

with the auth.signature: are the datetime fields created by py4web, pydal or taken from the DB ?

Mamisoa Andriantafika

unread,
Apr 1, 2021, 7:34:00 PM4/1/21
to py4web
Ok I see the code in auth.py:


def signature(self):

"""Returns a list of fields for a table signature"""

now = lambda: datetime.datetime.utcnow()

I think it would be good to make a transition as we deal now with Python 3 to switch to timezone aware datetime.


Mamisoa

Massimo

unread,
Apr 11, 2021, 9:22:50 PM4/11/21
to py4web
Even if python support timezone aware datetimes, the database backends do not. I think the best approach is to keep all dates server side as UTC and do any conversion to/from localtime in the browser (using <input type="datetime-local" value="[[=dt]" readonly>) The browser knows how to do this conversion. The server does not need to know the user timezone which may change (unless you really need to). py4web's Form should already handle this correctly. If not please provide an example of when it is broken.

Mamisoa Andriantafika

unread,
Apr 15, 2021, 8:04:40 AM4/15/21
to py4web
Thanks for your answer. 

For doing calculations server side eg for a calendar app, it is sometime useful to have datetime that are tz aware. It avoids to make some transformations on both sides.

Mamisoa

Reply all
Reply to author
Forward
0 new messages