获取当前时区时间

获取当前时区时间(带时区)

有时候我们默认获取当前时间会出错,我们通常自己写一个方法,然后调用它,下面分享一个当前时区时间的代码:

1 def get_location_time(self):
2     """
3     获取当前时区时间(带时区)
4     :return:
5     """
6     now_time = datetime.datetime.utcnow()
7     tz = self.env.user.tz or 'Asia/Shanghai'
8     return str(now_time.replace(tzinfo=pytz.timezone(tz)))

然后通过在字段设置默认值。就可以得到正确的时间了。

pay _time = fields.Datetime(string=u’支付时间’,
default=lambda self: self.env[‘yhb.common’].get_location_time())

其中:
yhb.common:表示方法的_name的值。

原文地址:https://www.cnblogs.com/zijue/p/10675040.html