布尔比较日期时非常奇怪的行为

时间:2012-10-04 18:52:40

标签: python

在尝试比较python中的日期时,我注意到了一些非常奇怪的行为。我正在尝试使用日期来检查是否要将项目添加到MySQL数据库(使用mysqldb库),并且在比较日期时,我的行为很奇怪:

代码是:

   latestDate = pullDateFromTable() #pull the latest item from the table
   for transaction in transactions:
        print "Transaction Time:",transaction.date
        print "Latest Entry:",latestDate
        if transaction.date > latestDate:
            print "Transaction is new, added product"
            #insert item to MySQL database
        else:
            print "Old transaction, did not add product"

输出结果为:

Transaction Time: Thu Oct  4 11:42:25 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Wed Oct  3 22:11:48 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 19:32:56 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 17:46:35 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 17:46:35 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 17:46:35 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 16:25:06 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 14:00:48 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 11:46:20 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 11:46:20 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 11:22:37 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 09:05:13 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 09:05:13 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Wed Oct  3 09:05:13 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Tue Oct  2 07:38:25 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Transaction is new, added product

Transaction Time: Mon Oct  1 17:39:52 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Mon Oct  1 15:05:29 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Sun Sep 30 20:56:38 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

Transaction Time: Sun Sep 30 09:52:09 2012
Latest Entry: Thu Oct  4 11:42:25 2012
Old transaction, did not add product

您会注意到周二或周三(10月2日和3日)的日期大于最后日期(10月4日),但9月或10月1日的日期小于。

有关为何会这样做的任何想法?

1 个答案:

答案 0 :(得分:6)

您的日期似乎是字符串,并且按字母顺序进行比较。 W之后发现T