If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
  • At the beggining of a model you put the following code:
request.localnow = request.now # creation of variable localnow
if not session.timeshift: # cretion of variable timeshift and save it to session
    session.timeshift = request.localnow - request.utcnow 

This model must be executed before any call of the variables session.timeshift or request.localnow. I personally create models/0_.py for this. 0_.py always executes before other models.

 

  • Create file modules/helpers.py and put there the following code
    from gluon import current
    
    # utc : datetime
    def utc2local(utc):
        return utc+current.session.timeshift
    
    # utc : datetime
    def local2utc(local):
        return local-current.session.timeshift
  • Everytime that you save a datetime to database save it as UTC datetime. Use local2utc() function for the conversion.
  • In every view use utc2local() function to convert UTC datetime from the database to the local timezone of each user.

 

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere