解决Odoo8.0单时区应用中的时区问题
- 
由于数据库中存储的是UTC时区,默认情况下数据导出和group by都存在时区问题。 
 解决办法:[b]将以UTC时区存储的数据改为按目标时区存储,并去掉JS中的时区转换[/b]。
 1、修改Odoo系统环境时区:
 odoo/openerp/init.py 文件
 import os
 os.environ['TZ'] = 'UTC' # 将这里的UTC 改为Asia/Shanghai 或其它目标时区
 2、修改 web页面时区转换的JS文件:
 odoo/addons/web/static/src/js/openerpframework.js 中的两个方法
 openerp.str_to_datetime 字符串转时间
 openerp.datetime_to_str 时间转字符串
 去掉这两个方法中的UTC字符。
 3、修改以UTC时间执行的SQL 查询或插入语句:
 去掉所有带 "at time zone 'UTC'" 或 "at time zone 'utc'"字符串的语句。
 odoo/openerp/models.py 特别是create_date、write_date字段值;
 odoo/openerp/addons/base/ir/ir_cron.py 定时任务中UTC时区改为当前时区;
 odoo/openerp/netsvc.py 备份下来的数据库名称时间标志
 4、修改其他功能性模块中带 "at time zone" 字符串来取UTC时间的语句。
 如 calendar 模块和 hr_timesheet_sheet模块
- 
以上方案只适合一个时区的用户,能解决其时区问题。 
 对于多时区,导出数据的时区问题解决方案是:[b]在导出时进行时区转换[/b]!
 class Datetime(Datetime):
 def convert_to_export(self, value, env):
 """ convertvaluefrom the cache to a valid value for export. The
 parameterenvis given for managing translations.
 """
 #将存储在数据库中的UTC时区的datetime字段转换成用户本机时区
 value_datetime = datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
 current_timezone = env.context.get('tz', False)
 current_tz = pytz.timezone(current_timezone)
 utc_tz = pytz.timezone('UTC')
 utc_tz_datetime = utc_tz.localize(value_datetime, is_dst=None)
 value = utc_tz_datetime.astimezone(current_tz)
 if env.context.get('export_raw_data'):
 return value
 return bool(value) and ustr(value)
 有解决多时区group by问题的大侠请不吝赐教!
- 
我不太喜欢你的彻底解决的方法和标题 

