그래서 이것이 내가 생각 해낸 것입니다 - 시간을 해석하는 사용자 정의 파서 : 지금은 아마 그래서 슈퍼 비 시원하고
def durationToSeconds(duration):
"""
duration - ISO 8601 time format
examples :
'P1W2DT6H21M32S' - 1 week, 2 days, 6 hours, 21 mins, 32 secs,
'PT7M15S' - 7 mins, 15 secs
"""
split = duration.split('T')
period = split[0]
time = split[1]
timeD = {}
# days & weeks
if len(period) > 1:
timeD['days'] = int(period[-2:-1])
if len(period) > 3:
timeD['weeks'] = int(period[:-3].replace('P', ''))
# hours, minutes & seconds
if len(time.split('H')) > 1:
timeD['hours'] = int(time.split('H')[0])
time = time.split('H')[1]
if len(time.split('M')) > 1:
timeD['minutes'] = int(time.split('M')[0])
time = time.split('M')[1]
if len(time.split('S')) > 1:
timeD['seconds'] = int(time.split('S')[0])
# convert to seconds
timeS = timeD.get('weeks', 0) * (7*24*60*60) + \
timeD.get('days', 0) * (24*60*60) + \
timeD.get('hours', 0) * (60*60) + \
timeD.get('minutes', 0) * (60) + \
timeD.get('seconds', 0)
return timeS
을하지만, 작동, 그래서 때문에 공유 해요 나는 너를 걱정한다.
와우, 내 파서를 작성했다고 생각하면이 모든 것이 너무 쉽게 보이게됩니다 :) 감사합니다! –
@MorganWilde Python의 가장 큰 장점 중 하나는 표준 라이브러리가 아니라면 pypi에 기존 솔루션을 찾을 수 있다는 것입니다. 솔루션이 이미 존재하는 경우 (파이썬뿐만 아니라 모든 언어로) 구현을 피하기 위해 노력하는 것이 좋습니다. – zstewart