简体   繁体   中英

How to convert a Git log date format to an integer?

I want store a date from git log to compare them then, but when I'm storing them into an array, It says they are string type and I don't know how to convert this such format (eq commited date: Mon Aug 22 15:43:38 2016 +0200 ).

date = commits[i]['Date']  
print("commited date:", date) 
moduleDate.append(date)
upToDateModule = max(moduleDate) #trigger here

Traceback (most recent call last):   File
"/home/savoiui/PycharmProjects/VersionChecker/versionCheckerV4.py",
line 120, in <module>
    main()   File "/home/savoiui/PycharmProjects/VersionChecker/versionCheckerV4.py",
line 110, in main
    upToDateModule = max(moduleDate)
 TypeError: an integer is required (got type str)

To get a datetime from a string, you could use the datetime library, with the strptime() method.

NB: Did you use // to comment? In python, you comment with the # character

You can try converting your date string to datetime format before appending them to the moduleDate list like

from datetime import datetime

date = commits[i]['Date']
print("commited date:", date) 
# commited date: Mon Aug 22 15:43:38 2016 +0200

datetime_object = datetime.strptime(' '.join(date.split(' ')[:-1]), '%a %b %d %H:%M:%S %Y') 

moduleDate.append(datetime_object) 
upToDateModule = max(moduleDate)

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM