Search code examples
pythongitgitpython

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 (e.q 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)

Solution

  • 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!