반응형
지정한 시간으로부터 몇초 지났는지 출력하는 파이썬 예제입니다.
2023. 10. 20 최초작성
시작 시간(start)과 현재 시간(now)로 부터 몇초 지났는지 계산하여 출력해줍니다.
현재 시간부터 10초 동안의 초단위 카운트를 출력하고 있습니다.
start : 20231020_204717 now : 20231020_204718 diff = 1
start : 20231020_204717 now : 20231020_204719 diff = 2
start : 20231020_204717 now : 20231020_204720 diff = 3
start : 20231020_204717 now : 20231020_204721 diff = 4
start : 20231020_204717 now : 20231020_204722 diff = 5
start : 20231020_204717 now : 20231020_204723 diff = 6
start : 20231020_204717 now : 20231020_204724 diff = 7
start : 20231020_204717 now : 20231020_204725 diff = 8
start : 20231020_204717 now : 20231020_204726 diff = 9
start : 20231020_204717 now : 20231020_204727 diff = 10
전체 코드입니다.
import datetime import time # 시작 시간을 현재 시간으로 지정합니다. start = datetime.datetime.now() datetime_string_start = start.strftime('%Y%m%d_%H%M%S') for i in range(10): time.sleep(1) # 현재 시간을 가져옵니다. now = datetime.datetime.now() datetime_string_now = now.strftime('%Y%m%d_%H%M%S') # 지정한 시간과 현재 시간의 차이를 구합니다. count = now - start # 시간 차이를 초 단위로 변경합니다. diff = int(count.total_seconds()) print(f'start : {datetime_string_start} now : {datetime_string_now} diff = {diff}') |
반응형
'Python > Python 예제 코드' 카테고리의 다른 글
파이썬 함수의 인자로 리스트 넘겨주는 예제 (0) | 2023.11.05 |
---|---|
파이썬 리스트에서 특정값에 가까운 수 찾기 (0) | 2023.11.04 |
날짜 시간 기준 파일 삭제하는 파이썬 예제 (0) | 2023.10.21 |
UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 5302: illegal multibyte sequence (0) | 2023.10.21 |
리스트의 값 범위를 변환하는 파이썬 예제 (0) | 2023.10.21 |