Python时间整数问题
Python time integer issue
我正在尝试计算 'then' 和 'now' 之间的时差。我更改了格式以便更好地比较它(我不需要秒或纳秒等)
'then' 时间来自加密,正在对其进行解析以进行比较,这就是我担心的错误原因。
def decrypt_and_compare_date():
from Crypto.Cipher import XOR
from datetime import timedelta, datetime, date
import base64
import config
cipher = XOR.new(cryptopassword)
encrypted = cipher.decrypt(base64.b64decode(config.event_date))
then = date(encrypted)
now = date(2015,10,5)
days = (now - then).days
print days + " days ago."
给我以下错误:
TypeError: an integer is required
如果我在这一行使用 *:
then = date(encrypted)
然后它解析了这个错误。
TypeError: function takes at most 3 arguments (8 given)
date(encrypted) 应该是 2015,7,1
有谁知道这个魔术吗?
尝试使用 datetime.strptime()
将字符串解析为日期。
例子-
>>> s = "2015,7,1"
>>> from datetime import datetime
>>> d = datetime.strptime(s,'%Y,%m,%d').date()
>>> d
datetime.date(2015, 7, 1)
这是基于假设字符串中的第二个数字是月份,第三个数字是日期,如果相反,则在 '%Y,%m,%d'
中交换 %m
和 %d
。
我正在尝试计算 'then' 和 'now' 之间的时差。我更改了格式以便更好地比较它(我不需要秒或纳秒等)
'then' 时间来自加密,正在对其进行解析以进行比较,这就是我担心的错误原因。
def decrypt_and_compare_date():
from Crypto.Cipher import XOR
from datetime import timedelta, datetime, date
import base64
import config
cipher = XOR.new(cryptopassword)
encrypted = cipher.decrypt(base64.b64decode(config.event_date))
then = date(encrypted)
now = date(2015,10,5)
days = (now - then).days
print days + " days ago."
给我以下错误:
TypeError: an integer is required
如果我在这一行使用 *:
then = date(encrypted)
然后它解析了这个错误。
TypeError: function takes at most 3 arguments (8 given)
date(encrypted) 应该是 2015,7,1
有谁知道这个魔术吗?
尝试使用 datetime.strptime()
将字符串解析为日期。
例子-
>>> s = "2015,7,1"
>>> from datetime import datetime
>>> d = datetime.strptime(s,'%Y,%m,%d').date()
>>> d
datetime.date(2015, 7, 1)
这是基于假设字符串中的第二个数字是月份,第三个数字是日期,如果相反,则在 '%Y,%m,%d'
中交换 %m
和 %d
。