Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to set time zone in py4web? #703

Open
sunjian-github opened this issue Mar 20, 2022 · 1 comment
Open

how to set time zone in py4web? #703

sunjian-github opened this issue Mar 20, 2022 · 1 comment

Comments

@sunjian-github
Copy link

Hi
I have a time zone problem when i insert data into a table
as we all know,by default ,py4web will generate timestamp on table with Built-in Field——“created_on” when inserting data.

In my project, the Built-in Field——“created_on” should be
2022-03-20 19:06:19
but display
2022-03-20 11:06:19

I have checked the settings.py and common.py , but unsolved

@davidmanns
Copy link

I have a concept of local time for my app. It's defined in my settings_private.py (app is based on _scaffold):

#localization settings
import locale
from dateutil import tz
locale.setlocale(locale.LC_ALL, '')
"""
as above, takes the default settings for the server. You can check what this is using a
command line terminal on the server using the "locale" command.
Use "locale -a" to see list of supported locale's.
You can specify by replacing the empty '' above with the preferred locale
"""
DATE_FORMAT = locale.nl_langinfo(locale.D_FMT)
CURRENCY_SYMBOL = locale.nl_langinfo(locale.CRNCYSTR)[1:]
#don't currently deal with currency symbols that follow the amount
TIME_ZONE = tz.gettz('America/New_York')
#see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

In my database table, I typically use:

Field('Created', 'datetime', default=lambda: datetime.datetime.now(TIME_ZONE).replace(tzinfo=None), writable=False),
Field('Modified', 'datetime', default=lambda: datetime.datetime.now(TIME_ZONE).replace(tzinfo=None),
   			update=lambda: datetime.datetime.now(TIME_ZONE).replace(tzinfo=None), writable=False),

the lambda functions get the current local time, then convert to a 'naive' datetime, i.e. just a number.

So everything is stored in local time rather than UST (Universal Standard Time).

You've probably long since solved your problem, but just in case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants