[英]How to convert a Git log date format to an integer?
我希望存儲一個來自git log的日期來比較它們,但是當我將它們存儲到數組中時,它說它們是字符串類型,我不知道如何轉換這種格式(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)
要從字符串中獲取日期時間,可以使用帶有strptime()
方法的日期時間庫。
注意:您是否使用
//
發表評論? 在python中,您使用#
字符進行注釋
您可以嘗試將日期字符串轉換為日期時間格式,然后再將它們附加到moduleDate
列表中
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)
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.