Here is how we can detect the user timezone in web2py, and convert from localtime to UTC and back, so that dates are always stored in UTC in web2py, and yet displayed in the user's local timezone. Many thanks to Massimo and Niphold for this!
First, there is the task of detecting the user timezone. Put the following code in one of the models (e.g., db.py):
This code works in tandem with the following code, that you have to put in layout.html:
In default.py, you need to put the code to receive the above Ajax call:
In order for this to work, you have to do two additional things:
Once you know the time in the user timezone, you need to convert the times to and from UTC as you store data in the database, or read from it. These two lines create a validator for a datetime localized in the user's local time (you can change the date format to be whatever you like):
You can then declare fields to use this validator as follows: