how to compare strftime values

前端 未结 3 599
醉话见心
醉话见心 2021-01-14 07:54

I have two values created from strftime as below

TIMEFORMAT=\"%Y-%m-%d %H:%M:%S\"

time1 = time.strftime(TIMEFORMAT)
time2 = time.strftime(TIMEFORMAT)


        
相关标签:
3条回答
  • 2021-01-14 08:25

    I use GNU-Awk (gawk) in UNXUTILS on a Win-7 PC. This answer to the strftime(.) question raises a small puzzle of its own. I have a similar problem. In financial market data I have a date-time string ($25) given as "03-APR-2006 09:55:25" (so time = substr($25, 13, 8)) and my objective is to count records (especially order cancellation records) that come in after 14:00:00 (2 pm).

    In my code I have a line which reads

             { if ($3==3) { 
                           { ++CK_NR} 
                           { ++CO_NR[$4, substr($25, 1, 11)] }  
                           { if (strftime(substr($25, 13, 8)) > ("14:00:00"))\
                             {
                              { ++CK_LATE_NR }
                              { ++CO_LATE_NR[$4, substr($25, 1, 11)] }
                             }
                           }
                          }
             }
    

    Just realized that the inequality I used -- if (strftime(substr($25, 13, 8)) > ("14:00:00")) -- has only a string in the RHS, and I didn't make this string the argument of another strftime(.). What's puzzling is that it did NOT give me an error.

    Am concerned that while it has not generated any errors, and the code has run, perhaps it is giving me something other than what I intended with the code. In a Command Prompt Window, doing

          \>gawk "{ print (strftime(\"09:55:25\") > (\"14:00:00\")) }"
    

    does yield "0" and

          \>gawk "{ print (strftime(\"09:55:25\") < (\"14:00:00\")) }"
    

    does yield "1". The GNU Awk Manual (http://www.gnu.org/software/gawk/manual/gawk.html#Time-Functions) yields little information on what is required for a meaningful comparison. Just now tried the above deleting the "strftime" even from the LHS, as under

          \>gawk "{ print ((\"09:55:25\") > (\"14:00:00\")) }"
    

    and

          \>gawk "{ print ((\"09:55:25\") > (\"14:00:00\")) }"
    

    and got the same results. So while I offer this as a candidate "answer," I want to be sure that I am getting the correct True/False result because GAWK is comparing time, and not for some other internal rule it uses in making string comparisons (causing the limited test to be only a coincidence). Can someone resolve this puzzle? Thanks. Best,

    Murgie

    0 讨论(0)
  • 2021-01-14 08:28

    Hope this helps you:

    In [1]: from datetime import datetime, timedelta
    
    In [2]: nd = datetime.now()
    
    In [3]: pd = nd - timedelta(hours=1)
    
    In [4]: snd = nd.strftime("%Y-%m-%d %H:%M:%S")
    
    In [5]: spd = pd.strftime("%Y-%m-%d %H:%M:%S")
    
    In [6]: print(snd, spd)
    ('2013-11-22 15:21:22', '2013-11-22 14:21:22')
    
    In [7]: datetime.strptime(snd, "%Y-%m-%d %H:%M:%S") > datetime.strptime(spd, "%Y-%m-%d %H:%M:%S")
    Out[7]: True
    
    0 讨论(0)
  • 2021-01-14 08:33

    Yes, you can compare them. And, yes, t1 > t2.

    In [1]: t1 = "2013-11-22 04:03:56"
    
    In [2]: t2 = "2013-11-22 01:03:56"
    
    In [3]: t1 > t2
    Out[3]: True
    
    In [4]: t1 < t2
    Out[4]: False
    
    In [5]: t1 == t2
    Out[5]: False
    
    0 讨论(0)
提交回复
热议问题