1

I noticed some extremely odd behavior when trying to compare dates in python. I'm trying to use the date to check whether or not to add an item to a MySQL database (using the mysqldb library), and when comparing dates, I'm getting odd behavior:

The code is:

   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"

The output is:

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

You'll notice that dates on Tuesday or Wednesday (Oct 2 and 3) are GREATER THAN the latest date (October 4), but dates in September or October 1 are less than.

Any thoughts on why this might be?

4

1 に答える 1

6

日付は文字列のように見え、アルファベット順に比較されています。Wの後に来るT

于 2012-10-04T18:56:24.857 に答える