如何将 Git 日志日期格式转换为整数?
How to convert a Git log date format to an integer?
我想存储一个来自 git 日志的日期然后比较它们,但是当我将它们存储到一个数组中时,它说它们是字符串类型,我不知道如何转换成这样格式 (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)
要从字符串中获取日期时间,您可以使用 datetime 库和 strptime()
方法。
NB: Did you use //
to comment? In python, you comment with the #
character
您可以尝试将日期字符串转换为日期时间格式,然后再将它们附加到 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)
希望对您有所帮助!
我想存储一个来自 git 日志的日期然后比较它们,但是当我将它们存储到一个数组中时,它说它们是字符串类型,我不知道如何转换成这样格式 (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)
要从字符串中获取日期时间,您可以使用 datetime 库和 strptime()
方法。
NB: Did you use
//
to comment? In python, you comment with the#
character
您可以尝试将日期字符串转换为日期时间格式,然后再将它们附加到 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)
希望对您有所帮助!