반응형
구문
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
설명
(1) timedelta란?
두 날짜나 시간 또는 datetime 인스턴스간의 차이를, 마이크로초 해상도로 출력하는 객체입니다.
timedelta 인자들의 기본값은 0이며, 정수나 부동 소수점, 양수나 음수가 될 수 있습니다.
필요한 모든 인자들을 timedelta 객체에 넣으면 days, seconds, microseconds만 내부적으로 저장됩니다.
다음의 예시 처럼요.
from datetime import timedelta
delta = timedelta(
days=50,
seconds=27,
microseconds=10,
milliseconds=29000,
minutes=5,
hours=8,
weeks=2
)
# Only days, seconds, and microseconds remain
print(delta)
# datetime.timedelta(days=64, seconds=29156, microseconds=10)
(2) timedelta 실행 시 내부에서 일어나는 일
위 예시 결과가 출력되기 전, 객체 내부에서 일어나는 일을 살펴볼까요?
첫번째로 인자들은 timedelta 객체 안에서 다음 단위로 변환됩니다.
- 밀리 초는 1000 마이크로초로 변환
- 분은 60초로 변환
- 시간은 3600초로 변환
- 주는 7일로 변환
그리고 days, seconds, microseconds는 다음과 같이 정규화 됩니다.
- 0 <= microseconds < 1000000, 즉 0과 999999 사이
- 0 <= seconds < 3600 * 24, 즉 0과 86399 사이
- -999999999 <= days <= 999999999, 즉, -999999999와 999999999 사이
float 인자가 있으면 변환과 정규화는 정확하지 않습니다. 값 반올림 방식을 사용하기 때문에 정보가 손실될 수 있습니다. 또한 정규화된 인자값이 표시된 범위를 벗어나면 OverflowError가 발생합니다.
(3) timedelta 클래스 어트리뷰트
timedelta.min
- 가장 음수인 timedelta 객체
- timedelta(-999999999)
timedelta.max
- 가장 양수인 timedelta 객체
- timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999)
timedelta.resolution
- 같지 않은 timedelta 객체 간의 가능한 작은 차이Q
- timedelta(microseconds=1).
https://docs.python.org/ko/3/library/datetime.html
datetime — Basic date and time types
Source code: Lib/datetime.py The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attr...
docs.python.org
반응형
'Programming Language > Python' 카테고리의 다른 글
[Python] Base64로 디코딩하기 / b64decode() (0) | 2023.02.22 |
---|---|
[Python] Base64로 인코딩하기 / b64encode() (0) | 2023.02.22 |
[Python] 파일/폴더 경로 존재 유무 확인하는 함수 (0) | 2023.02.09 |
[Python] 파이썬 현재 경로(디렉토리) getcwd() 쓰세요. (0) | 2023.02.09 |
[Python] 파이썬 경로 결합하기 join() (0) | 2023.02.09 |