用法:
datetime.astimezone(tz=None)返回具有新
tzinfo属性tz的datetime对象,调整日期和时间数据,使结果与self的 UTC 时间相同,但在tz的本地时间。如果提供,
tz必须是tzinfo子类的实例,并且其utcoffset()和dst()方法不得返回None。如果self是幼稚的,则假定它表示系统时区中的时间。如果不带参数(或使用
tz=None)调用,则系统本地时区假定为目标时区。转换后的日期时间实例的.tzinfo属性将设置为timezone的实例,其中区域名称和从操作系统获取的偏移量。如果
self.tzinfo是tz,self.astimezone(tz)等于self:不执行日期或时间数据的调整。否则结果是时区tz中的本地时间,表示与self相同的 UTC 时间:在astz = dt.astimezone(tz)之后,astz - astz.utcoffset()将具有与dt - dt.utcoffset()相同的日期和时间数据。如果您只想将时区对象
tz附加到日期时间dt而不调整日期和时间数据,请使用dt.replace(tzinfo=tz)。如果您只想从感知日期时间dt中删除时区对象而不转换日期和时间数据,请使用dt.replace(tzinfo=None)。请注意,可以在
tzinfo子类中覆盖默认的tzinfo.fromutc()方法,以影响astimezone()返回的结果。忽略错误情况,astimezone()的行为如下:def astimezone(self, tz): if self.tzinfo is tz: return self # Convert self to UTC, and attach the new time zone object. utc = (self - self.utcoffset()).replace(tzinfo=tz) # Convert from UTC to tz's local time. return tz.fromutc(utc)在 3.3 版中更改:
tz现在可以省略。在 3.6 版中更改: datetime.datetime.astimezone现在可以在假定代表系统本地时间的幼稚实例上调用方法。
相关用法
- Python datetime.datetime.ctime用法及代码示例
- Python datetime.datetime.fromisoformat用法及代码示例
- Python datetime.datetime.timetuple用法及代码示例
- Python datetime.datetime.isoformat用法及代码示例
- Python datetime.date.isoformat用法及代码示例
- Python datetime.date.replace用法及代码示例
- Python datetime.date.ctime用法及代码示例
- Python datetime.date.fromisoformat用法及代码示例
- Python datetime.date.isocalendar用法及代码示例
- Python datetime.time.fromisoformat用法及代码示例
- Python datetime.utcoffset()用法及代码示例
- Python datetime.tzinfo()用法及代码示例
- Python datetime.timetz()用法及代码示例
- Python datetime.time.isoformat用法及代码示例
- Python datetime.timedelta用法及代码示例
- Python datetime.tzinfo.fromutc用法及代码示例
- Python datetime.timedelta()用法及代码示例
- Python datetime.tzname()用法及代码示例
- Python datetime.tzinfo.dst用法及代码示例
- Python datetime astimezone()用法及代码示例
注:本文由纯净天空筛选整理自python.org大神的英文原创作品 datetime.datetime.astimezone。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
