TypeError: can‘t subtract offset-naive and offset-aware datetimes

简介: TypeError: can‘t subtract offset-naive and offset-aware datetimes

两个datetime进行时差计算报错:

TypeError: can't subtract offset-naive and offset-aware datetimes

原来是两个相减的时间时区不一致

# -*- coding: utf-8 -*-
from datetime import datetime
import pytz
now1 = datetime.now(tz=pytz.UTC)
print(now1)
# 零时区的时间 2021-06-11 04:11:39.588770+00:00
now2 = datetime.now()
print(now2)
# 东八区的时间 2021-06-11 12:12:17.431790
# print(now2 - now1)
# TypeError: can't subtract offset-naive and offset-aware datetimes
now3 = now1.replace(tzinfo=None)
print(now3)
# 去掉时区之后 2021-06-11 04:15:06.453159
print(now2 - now3)
# 8:00:00.000061

参考

python datetime offset-aware与offset-navie相互转换

相关文章
|
3月前
GEE错误——Layer error: Image.connectedPixelCount: Segment size calculation on floating point bands is n
GEE错误——Layer error: Image.connectedPixelCount: Segment size calculation on floating point bands is n
32 0
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
2280 0
|
9月前
|
自然语言处理 搜索推荐 数据挖掘
RolePred: Open-Vocabulary Argument Role Prediction for Event Extraction 论文解读
事件抽取中的论元角色是指事件和参与事件的论元之间的关系。尽管事件抽取取得了巨大进展,但现有研究仍然依赖于领域专家预定义的角色。
50 0
|
9月前
|
机器学习/深度学习 自然语言处理 测试技术
Query and Extract Refining Event Extraction as Type-oriented Binary Decoding 论文解读
事件抽取通常被建模为一个多分类问题,其中事件类型和论元角色被视为原子符号。这些方法通常仅限于一组预定义的类型。
39 0
|
人工智能 编解码 自动驾驶
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
YOLOv7在5 FPS到160 FPS的范围内,在速度和精度方面都超过了所有已知的物体检测器,在GPU V100上以30 FPS或更高的速度在所有已知的实时物体检测器中具有最高的精度56.8% AP。
357 0
|
PyTorch 算法框架/工具
pytorch报错 RuntimeError: The size of tensor a (25) must match the size of tensor b (50) at non-singleton dimension 1 怎么解决?
这个错误提示表明,在进行某个操作时,张量a和b在第1个非单例维(即除了1以外的维度)上的大小不一致。例如,如果a是一个形状为(5, 5)的张量,而b是一个形状为(5, 10)的张量,则在第二个维度上的大小不匹配。
3116 0
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2438 0
|
编译器
relocation value does not fit in 26 bits (offset: 0x10, type: 1)
relocation value does not fit in 26 bits (offset: 0x10, type: 1)
146 0
解决办法:RuntimeError: dictionary changed size during iteration
解决办法:RuntimeError: dictionary changed size during iteration
88 0
|
PyTorch 算法框架/工具
【Pytorch问题】RuntimeError: “max_pool2d“ not implemented for ‘Long‘
【Pytorch问题】RuntimeError: “max_pool2d“ not implemented for ‘Long‘
286 0