I found many discussions in the forums on handling timezones. Most paths
lead to Niphlod's timezone plugin (thank you!). But most of the comments
and documentation surrounding that plugin didn't address standardizing on
UTC throughout the app, enabling timezone handling for auth.signatures, and
the function of the timezone parameter in the IS_DATETIME validator.
Here is what I have so far to bring these all together - all feedback
appreciated ...
Start by adding the timezone plugin to your app:
https://github.com/niphlod/w2p_timezone_plugin
Add "request.now = request.utcnow" at the top of db.py. This sets all
internal datetimes to UTC. I found one comment about potential session
handling prior to the first model executing but I haven't run into any
problems. This assures that all dates stored in the database and elsewhere
are UTC.
Then import pytz. This seems like the best python timezone library. There
are discussions in the developers group about not making this a dependency
(and Massimo cited some viable alternatives). Niphlod's plugin requires it.
If you want all your auth.signatures to follow the same rules as other
datetime fields (and the datetime fields in auth_event and auth_cas) then
add these five lines before any calls to define_table that include
auth.signature:
user_timezone = session.plugin_timezone_tz or 'UTC'
db.auth_event.time_stamp.requires = IS_DATETIME(timezone=pytz.timezone(
user_timezone))
db.auth_cas.created_on.requires = IS_DATETIME(timezone=pytz.timezone(
user_timezone))
auth.signature.created_on.requires = IS_DATETIME(timezone=pytz.timezone(
user_timezone))
auth.signature.modified_on.requires = IS_DATETIME(timezone=pytz.timezone(
user_timezone))
session.plugin_timezone_tz is handled by the timezone plugin.
I couldn't find any documentation on the timezone parameter for the
IS_DATETIME validator. This parameter translates datetime's to and from
UTC, i.e., the validator will validate and then translate a date entered on
a form from the user_timezone to UTC and performs the reverse operation
when retrieving the datetime from the database for display on a form. All
dates are stored in UTC.
To display the timezone in grids and views, modify the languages file and
add "%Z" to the end of the datetime string translation,
i.e.,for %Y-%m-%d %H:%M:%S enter %Y-%m-%d %H:%M:%S %Z (I'm having a problem
with this in edit forms which I think is a bug in the widget).
Then I follow the instructions for the timezone plugin to detect the users
timezone, set the session variable.
I think there is some overlap in some of the string formatting for the
timezone in the plugin with the languages translation but I haven't found a
problem yet (I'm not sure the pluging uses the translations).
Does this look right?
--
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 the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.