Py学习  »  Python

python使用if语句检查时间戳是否工作不正常

Anon_guy • 5 年前 • 1504 次点击  

我有这些 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"
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43991
 
1504 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Josué Cortina
Reply   •   1 楼
Josué Cortina    6 年前

我刚刚测试了您的代码,在手动分配给每个变量之后,这似乎是可行的。我什么都没换除了换 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 .

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

sean
Reply   •   2 楼
sean    6 年前

“break”语句不应在循环之外。但通常情况下,口译员会发现这一点。所以必须有更多的代码,也许问题就在那里。我复制了这里的逻辑并将给定的值赋给变量,如果customer_被阻塞,我得到第一个选项=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)

您是否将'false'或'false'(字符串)分配给customer_是否被阻止而不是false?