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

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

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

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

Web界面备份数据库文件名时差问题--侧面了解OpenERP时间机制



  • 导读:<br /> [检测到链接无效,已移除] <br /> http://stackoverflow.com/questions/9919706/openerp-strange-date-time-issue <br />[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]<br />从v6.1开始,OpenERP服务器的时间(新手注:这里指的不是操作系统Windows或Linux服务器设定的时间)从设计上统一规定为UTC,即GMT。<br />在web界面,用户只要设置了自己的时区,web端显示的时间会自动转换为本地时间,中国即 UTC + 8小时。<br /><br />而在备份数据库时,自动生成的文件名中的时间,显示的仍然是UTC。因为这个时间戳没有通过web客户端转换,而是直接取OpenERP服务器设定的UTC。代码追溯:openerp\addons\web\controllers\main.py<br />

    &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; }
    

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

    &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;)
    

    <br />重启Openerp Server生效。<br />//完<br /><br /><br /><br />



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



  • 嗯. 感谢分享



  • 直接用datetime.datetime.now()就是取的服务器时间了吧,这样比加8更好些。<br /><br />可以继续研究一下 auto_backup 模块生成的sql文件是按哪个时区的。



  • datetime.datetime.now() 和<br />datetime.datetime.utcnow() 一个效果<br /><br />+8还是必须的吧?



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


  • 管理员

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

    &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;