Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

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

    &nbsp; &nbsp; def backup(self, req, backup_db, backup_pwd, token):<br />&nbsp; &nbsp; &nbsp; &nbsp; try:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db_dump = base64.b64decode(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; req.session.proxy(&quot;db&quot;).dump(backup_pwd, backup_db))<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename = &quot;%(db)s_%(timestamp)s.dump&quot; % {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;db&#039;: backup_db,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;timestamp&#039;: datetime.datetime.utcnow().strftime(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;%Y-%m-%d_%H-%M-%SZ&quot;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
    



    经群里Jeff大神指点,像这种备份操作结果不在web端显示,而是直接取server端的时间生成文件名,就没办法按用户时区生成文件名了!
    不过作为中国用户,可以直接写死+8是不是?

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;timestamp&#039;: (datetime.datetime.utcnow() + datetime.timedelta(hours = 8)).strftime(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;%Y-%m-%d_%H-%M-%SZ&quot;)
    


    重启Openerp Server生效。
    //完





  • 嗯. 感谢分享.....
    因为 备份时, 并不提供用户信息, 所以, 电脑并不知道 是来自哪个时区的用户在备份.
    我个人认为 就使用GMT时间就好了. 备份的人自己知道就OK了....



  • 嗯. 感谢分享



  • 直接用datetime.datetime.now()就是取的服务器时间了吧,这样比加8更好些。

    可以继续研究一下 auto_backup 模块生成的sql文件是按哪个时区的。



  • datetime.datetime.now() 和
    datetime.datetime.utcnow() 一个效果

    +8还是必须的吧?



  • autobackup 设置界面再弄个 参数 ,时区差异  ?


  • 管理员

    可以调用这个 fields.datetime.context_timestamp,在context里面写入tz

    &nbsp; &nbsp; def context_timestamp(cr, uid, timestamp, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;Returns the given timestamp converted to the client&#039;s timezone.<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  This method is *not* meant for use as a _defaults initializer,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  because datetime fields are automatically converted upon<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  display on client side. For _defaults you :meth:`fields.datetime.now`<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  should be used instead.<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  :param datetime timestamp: naive datetime value (expressed in UTC)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to be converted to the client timezone<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  :param dict context: the &#039;tz&#039; key in the context should give the<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name of the User/Client timezone (otherwise<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UTC is used)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  :rtype: datetime<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  :return: timestamp converted to timezone-aware datetime in context<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timezone<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot;
    

Log in to reply