社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Josué Cortina

Josué Cortina 最近创建的主题
Josué Cortina 最近回复了
7 年前
回复了 Josué Cortina 创建的主题 » python使用if语句检查时间戳是否工作不正常

我刚刚测试了您的代码,在手动分配给每个变量之后,这似乎是可行的。我什么都没换除了换 strftime("%s") 具有 timestamp() 因为我犯了一个错误: ValueError: Invalid format string .

import datetime
import time

order_id            = 0

# Manually assign this
order_date          = "2018-12-17T16:58:00-06:00"
customer_is_blocked = False

two_month_seconds   = (3600 * 24) * 60
one_month_seconds   = (3600 * 24) * 30
stripped_date       = order_date[:order_date.find("T")]
current_timestamp   = time.time()

# This is the only change I did: strftime("%s") => timestamp()
retrieved_timestamp = int(datetime.datetime.strptime(stripped_date, "%Y-%m-%d").timestamp())

print("one month", one_month_seconds)
print("two months", two_month_seconds)
print("current timestamp", current_timestamp)
print("retrieved timestamp", retrieved_timestamp)
print("current - two months", current_timestamp - two_month_seconds)

if retrieved_timestamp > (current_timestamp - one_month_seconds) and not customer_is_blocked:
    print(1)
elif customer_is_blocked:
    print(2)
elif retrieved_timestamp < (current_timestamp - two_month_seconds):
    print(3)

价值为 order_date 你提供了上面的代码 1 如果 customer_is_blocked == False 和 2 如果 customer_is_blocked == True .

让我知道这对你是否有效!