time zone in web2py revisited

128 views
Skip to first unread message

icodk

unread,
Sep 24, 2015, 11:34:07 AM9/24/15
to web2py-users
I have the following 2.5 issues with  user time zone
1. I installed the timezone plugin and followed the instructions up to that line: 
....As soon as you call this page, session.plugin_timezone_tz is filled automatically with the detected timezone

which comes just after the: 

from plugin_timezone import fast_tz_detector
def detect_timezone():
    tz = fast_tz_detector()
    return dict(tz=tz)

The problem is that I do not call this page. I don't even know which page is talking about.
My application is a simple table shown in an SQLFORM.smartgrid() that invoked from a controller function like this(simplified): 

def show_list():
grid=SQLFORM.smartgrid(db.visit_list)
return locals()

Needless to say that the user_timezone is set to UTC and this bring me to the next issue:

2. If I manually set the  user_timezone='Europe/Berlin' then I do get the right time zone in an Edit/new forms that are started from the grid
clicking on the visit_list link or Add new line button. but a datetime field still shows UTC datetime in the grid itself, probably due to the following line:

db.visit_list.started.represent=lambda t,r: t.strftime("%m-%d %H:%M") if t else ''

   2.1 as a sub issue this represent line only works in the grid. In the Edit/new forms the datetime widget takes over and add a full datetime format.Could this be 
 controlled? Fx. I would like to show only "%m-%d %H:%M"

Thanks for help
     


Niphlod

unread,
Sep 24, 2015, 2:56:17 PM9/24/15
to web2py-users
first time I hear someone having 2.5 issues, nice!

let's see if we can clear them up....

1.
The plugin basically works invoking a nice js library that does its best to determine client-side what timezone the user is in.
This info is POSTed to web2py, and stored in the user session.
Since the "auto-determination" can only be done on client-side, you NEED to use that piece of code  (fast_tz_detector) in AT LEAST one page. If you don't know where the user lands initially, well, it's an architectural problem of the app. call it with ajax in every page, redirect the user there and then on the home page if session.plugin_timezone_tz is not there already, you name it.
The important bit to grasp is that if you don't use fast_tz_detector, you don't have the session.plugin_timezone_tz filled. 
fast_tz_detector just appends the javascript needed plus a snippet that fills the session value.
Without it, you NEED to guess yourself or make the user choose its preferred timezone explicitely (using, e.g., tz_nice_detector_widget).

2.
it's definitely dependant on your custom represent. It's the IS_DATE(TIME) validator that, set up with the timezone info, does "the math" and when called for representation, displays the right value. If you want to make your own representation, you need to do the 
same math. 
"The math" is pretty straightforward, given that "d" is the utc datetime value, is "d.replace(tzinfo=pytz.UTC).astimezone(pytz.timezone(user_timezone))"

2.1
web2py has a pretty extensive internationalization support, and it includes date formatting. IS_DATETIME and IS_DATE both take a "format" parameter that correctly represents the date as specified in the format. the default value for IS_DATETIME is "T('%Y-%m-%d %H:%M:%S')" . the T is the internationalization support, so if you set in the relevant language file to be %m-%d %H:%M, it'll be shown throughout all the app. If you don't want to leverage internationalization and just set a fixed representation, use format='%m-%d %H:%M', without any T involved . 

BTW: Fixing 2.1 will effectively eliminate the 2. issue: you won't need any custom represent.

Itamar Cohen

unread,
Sep 24, 2015, 3:24:20 PM9/24/15
to web...@googlegroups.com
Thank you Niphold
Thanks for the explanation: My specific individual problem was that it took me a while to find out where to inject the tz script that I generate (in my case in the  index() function). Since the index function is  the first page in my  application I just had to add {{=tz}} in the header of the index.html. Hopefully this will help others for faster understanding.
Thanks also for the  solution for the last  1.5 :-)

Best Regards
Itamar Cohen
Picatek
Ahornvej 27
DK-7700 Thisted
Denmark
www.picatek.dk
i...@picatek.dk
T: +45 97 91 07 82
M:+45 29 89 07 82

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/jIEUIb8wxtg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages