[英]I'm having trouble converting time stamps to readable strings
Is there an easy alternative way to do this?有没有一种简单的替代方法可以做到这一点? I keep ending up with my else executing I don't know why.我一直以我的 else 执行结束,我不知道为什么。 The debug prints return two different data types as well.调试打印也返回两种不同的数据类型。
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
You subtract greater datetime from smaller.您从较小的日期时间中减去较大的日期时间。
difference = last_connect_timestamp - now
now
is greater than last_connect_timestamp
, therefore your difference will always be sub zero. now
大于last_connect_timestamp
,因此您的差异将始终低于零。
Another thing, use if
and elif
.另一件事,使用if
和elif
。 Otherwise you will keep overwriting your variable.否则,您将继续覆盖您的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.