사건 발생까지 걸린 시간, 또는 사건 사이의 시간 등을 구할 수 있어야 한다. 날짜는 매우 많은
애플리케이션에서 중요하기도 하고,
60
분,
24
시간, ‘평균
30
일’, ‘거의 정확히
365
.
25
일’ 등을
섞어 쓰는 아주 이상한 종류의 데이터이므로, 파이썬에서는 날짜를 다루는 특별한 방법을 제공
한다.
파이썬은 날짜와 시간 처리용으로
datetime
모듈을 제공한다. 스크립트에서
datetime
이 제공
하는 기능을 모두 이용하기 위해
from
datetime
import
date
,
time
,
datetime
,
timedelta
를 기존 스크립트 상단 기존
import
문 아래에 추가한다. 이제
first_script.py
는 다음과 같
을 것이다.
#!/usr/bin/env python3
from math import exp, log, sqrt
import re
from datetime import date, time, datetime, timedelta
datetime
모듈을 임포트하면 용도에 따라 다양한 종류의 날짜와 시간 객체와 함수를 사용
할 수 있다. 몇몇 유용한 객체와 함수로는
today
,
year
,
month
,
day
,
timedelta ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.