Created
October 4, 2016 13:50
-
-
Save developer-sdk/cc68228d239ae34f488492485bbb06e3 to your computer and use it in GitHub Desktop.
datetime을 리스트에 넣고 평균을 구하는 벙법 예제
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # -*- coding: utf-8 -*- | |
| strStartTime = "Mon, 03 Oct 2016 23:21:48 GMT" | |
| strEndTime = "Mon, 03 Oct 2016 23:41:28 GMT" | |
| # 문자열을 datetime 객체로 변환 | |
| startTime = datetime.strptime(strStartTime, '%a, %d %b %Y %H:%M:%S %Z') | |
| endTime = datetime.strptime(strEndTime, '%a, %d %b %Y %H:%M:%S %Z') | |
| # 경과 시간 | |
| elapsedTime = endTime - startTime | |
| # 경과시간 리스트 | |
| times = [ elapsedTime ] | |
| # 경과시간을 초(second)로 변환하여 평균을 구하고 | |
| # 이것을 다시 일자로 변환 | |
| sumTime = 0 | |
| for time in times: | |
| sumTime += timedelta_total_seconds(time) | |
| # 경과 시간 출력 | |
| print datetime.fromtimestamp(sumTime / len(times)).strftime('%H:%M:%S') | |
| # timedelta 객체의 총 초(second) 구하기 | |
| # python 2.7 부터는 timedelta.total_seconds() 메소드로 구할 수 있음 | |
| def timedelta_total_seconds(timedelta): | |
| return (timedelta.microseconds + 0.0 + (timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment