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
相关文章
|
4月前
|
自然语言处理 Go 索引
startOffset must be non-negative, and endOffset must be >= startOffset, and offsets must not go backwards startOffset=615,endOffset=617,lastStartOffset=616 for field 'convContent.content'
【7月更文挑战第4天】startOffset must be non-negative, and endOffset must be >= startOffset, and offsets must not go backwards startOffset=615,endOffset=617,lastStartOffset=616 for field 'convContent.content'
|
6月前
|
iOS开发
Xcode报错“compact unwind compressed function offset doesn‘t fit in 24 bits
Xcode报错“compact unwind compressed function offset doesn‘t fit in 24 bits
63 1
|
6月前
|
机器学习/深度学习 监控 数据可视化
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
|
算法框架/工具
Keras报错:TypeError: (‘Keyword argument not understood:‘, ‘offset‘)
Keras报错:TypeError: (‘Keyword argument not understood:‘, ‘offset‘)
183 0
|
存储
【CSAPP】HW1 | 位向量的应用 Application of bit vectors | Adressing and Byte Ordering
【CSAPP】HW1 | 位向量的应用 Application of bit vectors | Adressing and Byte Ordering
27 0
【CSAPP】HW1 | 位向量的应用 Application of bit vectors | Adressing and Byte Ordering
Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?)
Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?)
394 0
Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?)
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2693 0
|
TensorFlow 算法框架/工具
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
277 0
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
|
编译器
relocation value does not fit in 26 bits (offset: 0x10, type: 1)
relocation value does not fit in 26 bits (offset: 0x10, type: 1)
199 0
ValueError: Sample larger than population or is negative
ValueError: Sample larger than population or is negative
197 0