Web界面备份数据库文件名时差问题--侧面了解OpenERP时间机制
- 
导读: 
 [检测到链接无效,已移除]
 http://stackoverflow.com/questions/9919706/openerp-strange-date-time-issue
 [quote]There is one golden rule for datetime fields in 6.1 addons code: "ALWAYS work in UTC - compute in UTC - save in UTC" The UTC values will be properly converted to local time when the result is diplayed in a client-side form.[/quote]
 从v6.1开始,OpenERP服务器的时间(新手注:这里指的不是操作系统Windows或Linux服务器设定的时间)从设计上统一规定为UTC,即GMT。
 在web界面,用户只要设置了自己的时区,web端显示的时间会自动转换为本地时间,中国即 UTC + 8小时。
 而在备份数据库时,自动生成的文件名中的时间,显示的仍然是UTC。因为这个时间戳没有通过web客户端转换,而是直接取OpenERP服务器设定的UTC。代码追溯:openerp\addons\web\controllers\main.py    def backup(self, req, backup_db, backup_pwd, token):<br />        try:<br />            db_dump = base64.b64decode(<br />                req.session.proxy("db").dump(backup_pwd, backup_db))<br />            filename = "%(db)s_%(timestamp)s.dump" % {<br />                'db': backup_db,<br />                'timestamp': datetime.datetime.utcnow().strftime(<br />                    "%Y-%m-%d_%H-%M-%SZ")<br />            }
 经群里Jeff大神指点,像这种备份操作结果不在web端显示,而是直接取server端的时间生成文件名,就没办法按用户时区生成文件名了!
 不过作为中国用户,可以直接写死+8是不是?                'timestamp': (datetime.datetime.utcnow() + datetime.timedelta(hours = 8)).strftime(<br />                    "%Y-%m-%d_%H-%M-%SZ")
 重启Openerp Server生效。
 //完
- 
可以调用这个 fields.datetime.context_timestamp,在context里面写入tz     def context_timestamp(cr, uid, timestamp, context=None):<br />        """Returns the given timestamp converted to the client's timezone.<br />          This method is *not* meant for use as a _defaults initializer,<br />          because datetime fields are automatically converted upon<br />          display on client side. For _defaults you :meth:`fields.datetime.now`<br />          should be used instead.<br /><br />          :param datetime timestamp: naive datetime value (expressed in UTC)<br />                                      to be converted to the client timezone<br />          :param dict context: the 'tz' key in the context should give the<br />                                name of the User/Client timezone (otherwise<br />                                UTC is used)<br />          :rtype: datetime<br />          :return: timestamp converted to timezone-aware datetime in context<br />                    timezone<br />        """


