Python使用if语句无法正常工作检查时间戳

时间:2018-12-19 20:07:22

标签: python if-statement

我有这些if语句应该检查时间戳。最后的if语句似乎无效。这些时间戳记值应显示检索到的时间戳记大于当前时间戳记-两个月,但这不会发生。使用以下值运行时,将触发最后一个if语句。

根据要求:     订单日期2018-12-17T16:58:00-06:00   值:

one month 2592000
two months 5184000
current timestamp 1545247709.1553745
retrieved timestamp 1545026400
current - two months 1540063709.1553745


one month 259200 <class 'int'>
two months 5184000 <class 'int'>
current timestamp 1545252986.085405 <class 'float'>
retrieved timestamp 1545026400 <class 'int'>



   if order_count > 1:
    for result in results['orders']:
        order_status_info= self_api.which_api('order_statuses/%d' % result['order_status_id'])
        for customer_blocked_reason in customer_blocked_reasons:
            if customer_blocked_reason in order_status_info['name']:
                customer_is_blocked = True


   order_id                 = 0
        order_date          = result['ordered_at']
        two_month_seconds   = (3600 * 24) * 60
        one_month_seconds   = (3600 * 24) * 30
        stripped_date       = order_date[:order_date.find("T")]
        current_timestamp   = time.time()
        retrieved_timestamp = int(datetime.datetime.strptime(stripped_date, '%Y-%m-%d').strftime("%s"))
        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:
            status['success'] = 1
            status['message'] = "Customer Verified with orders older than 30 days and no blocking reasons"
            break

        elif customer_is_blocked:
            status_change_result = self_order.update_status(order_statuses['order_hold_manager_review'])
            status['success']    = 1
            status['message']    = "Changed order status to Order Hold - Manager Review"
            break

        elif retrieved_timestamp < (current_timestamp - two_month_seconds):
            status['success'] = 0
            status['message'] = "There is more than 1 order, and none are greater than 60 days, we need to check manually"

2 个答案:

答案 0 :(得分:0)

'break'语句不应在循环之外。但通常情况下,口译员会抓住这一点。因此,必须涉及更多的代码,也许问题就在那里。我在这里复制了逻辑,并为变量分配了给定值,如果customer_is_blocked == False,则得到第一个选项,如果为False,则得到第二个选项

two_month_seconds   = (3600 * 24) * 60
one_month_seconds   = (3600 * 24) * 30
current_timestamp   = 1545247709.1553745
retrieved_timestamp = 1545026400
customer_is_blocked = True

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)

您是否为customer_is_blocked分配了“ False”或“ False”(字符串)而不是False?

答案 1 :(得分:0)

我刚刚测试了您的代码,并且在为每个变量手动分配之后,这似乎可以正常工作。除了用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

让我知道这是否对您有用!