我在将时间戳转换为可读字符串时遇到问题



有简单的替代方法吗?我一直在执行我的其他任务,我不知道为什么。调试打印还返回两种不同的数据类型。

def last_played(player_id):
response = requests.get(f'{base}player?id={player_id}')
if response.status_code == 200:
parsed = json.loads(response.text)
last_connect_timestamp = parsed["last_connect_timestamp"]
now = time.time()
print(last_connect_timestamp)
print(now)
difference = last_connect_timestamp - now
last_connect_timestamp /=  1000
if difference > 86400:
last_seen = "more than a day ago on " + str(datetime.utcfromtimestamp(last_connect_timestamp).strftime('%Y-%m-%d'))
if difference > 18000:
last_seen = "few hours ago " + str(datetime.utcfromtimestamp(last_connect_timestamp).strftime('at %H:%M'))
if difference > 3600:
last_seen = "more than a hour ago " + str(datetime.utcfromtimestamp(last_connect_timestamp).strftime('at %H:%M'))
if difference > 600:
last_seen = "few minutes ago " + str(datetime.utcfromtimestamp(last_connect_timestamp).strftime('at %H:%M'))
if difference > 60:
last_seen = "a minute ago"
if difference < 59:
last_seen = "few seconds ago"
else:
last_seen = datetime.utcfromtimestamp(last_connect_timestamp).strftime('on %Y-%m-%d at %H:%M')
else:
print(f"Web-driver  : Failed to retrive data from API! Error : {response.status_code}")
last_seen = "Failed to retrive data!"
return last_seen

您可以从较小的日期时间中减去较大的日期时间。

difference = last_connect_timestamp - now

now大于last_connect_timestamp,因此您的差值将始终为零以下。

另外,使用ifelif。否则,您将继续覆盖您的变量。

相关内容

最新更新