Changing my datetime from UTC to local time

27 views
Skip to first unread message

mostwanted

unread,
Jun 3, 2020, 2:22:00 PM6/3/20
to web2py-users

Hi guys has anyone ever tried this solution by Massimo & Nyphold (http://luca.dealfaro.org/code/web2py-recipes/utc-to-localtime-and-back-in-web2py)?
I tried it in my application but I must be missing a step or something because my time is NOT changing or maybe i'm just using it wrong!
Because of different time zones I wanna convert my users time stamps from the app hosters (pythonanywhere.com) time (UTC) to my time (local time):

CODE:

Model (db.py):
# --------------------------------------------------------------------
#Detecting the usertime zone
#---------------------------------------------------------------------
is_timezone_unknown
= (session.user_timezone is None)
user_timezone
= session.user_timezone or 'UTC'

Model (Application DB)
datetime_validator = IS_DATETIME(timezone=pytz.timezone(user_timezone), format=DATE_FORMAT)


Controller:
def set_timezone():
   
"""Ajax call to set the timezone information for the session."""
    tz_name
= request.vars.name
   
from pytz import all_timezones_set
   
if tz_name in all_timezones_set:
        session
.user_timezone = tz_name

View (Somewhere in the Layout):

{{if is_timezone_unknown:}}
   
<script src="{{=URL('static', 'js/jstz.min.js')}}"></script>
   
<script>
      $
(function() {
       
var tz_name = jstz.determine();
        $
.get("{{=URL('default', 'set_timezone')}}", tz_name);
     
});
   
</script>
 
{{pass}}

I wanna believe this is how this solution is used but I am not getting results!

Please assist;

Regards;

Mostwanted

mostwanted

unread,
Jun 4, 2020, 6:14:36 AM6/4/20
to web2py-users
Instead of using this method i decided to just add the time difference to the UTC time to get my local time.

import datetime
db
.define_table('my_token',
   
Field('purchased_on', 'datetime', default=(request.now+datetime.timedelta(hours=2)), readable=False, writable=False),
   
.............
   
.............
    .............

Regards;

Mostwanted
Reply all
Reply to author
Forward
0 new messages