시간을 다루는 time
# 소수점 5자리까지 출력
import math import time
start = time.time()
math.factorial(100000)
end = time.time()
print(f"{end - start:.5f} sec")
#datetime으로 예쁘게 출력
import math import time
import datetime
start = time.time()
math.factorial(1234567)
end = time.time()
sec = (end - start)
#시:분:초.소숫점
result = datetime.timedelta(seconds=sec)
print(result)
#시:분:초
result_list = str(datetime.timedelta(seconds=sec)).split(".")
print(result_list[0])
출처: <https://blockdmask.tistory.com/549> [개발자 지망생]
시간 parsing
from datetime import datetime
dt = datetime.strptime(str(202005010000),'%Y%m%d%H%M' )
dt.year
시간 count
from datetime import datetime, timedelta
start_date = 202005010000
end_date = 202005020000
dt = datetime.strptime(str(start_date),'%Y%m%d%H%M' )
year = dt.year
month = dt.month
day = dt.day
sec = 0
while start_date < end_date:
sec += 120
time = timedelta(seconds = sec)
date = datetime(year, month, day)
theday = date + time
start_date = int(theday.strftime('%Y%m%d%H%M'))
print(start_date)