[python] Arrow 라이브러리에서 지원하는 날짜와 시간 연산

Arrow는 Python에서 사용할 수 있는 강력한 날짜와 시간 처리 라이브러리입니다. 이 라이브러리는 datetime 모듈의 기능을 확장하여 사용하기 쉬운 인터페이스와 자세한 표현 기능을 제공합니다.

Arrow 설치

먼저 Arrow 라이브러리를 설치해야 합니다. pip를 사용하여 설치할 수 있습니다.

pip install arrow

Arrow로 날짜 및 시간 생성하기

Arrow를 사용하여 날짜와 시간을 생성하는 것은 간단합니다. arrow.now()를 사용하면 현재 시간을 가져올 수 있습니다. 또는 arrow.get()을 사용하여 특정한 날짜와 시간을 지정할 수 있습니다.

import arrow

# 현재 시간 가져오기
now = arrow.now()
print(now)

# 특정한 날짜와 시간 지정하기
date = arrow.get(2022, 1, 1)
print(date)

# 현재 시간에서 특정한 시간을 더하거나 빼기
next_week = now.shift(weeks=1)
print(next_week)

Arrow로 날짜 및 시간 연산하기

Arrow는 다양한 날짜와 시간 연산 기능을 제공합니다. 몇 가지 예제를 살펴보겠습니다.

import arrow

# 날짜와 시간 차이 계산하기
start = arrow.get(2022, 1, 1)
end = arrow.get(2022, 1, 5)
duration = end - start
print(duration.days)

# 날짜와 시간 비교하기
now = arrow.now()
tomorrow = now.shift(days=1)
print(now < tomorrow)

# 특정한 시간 포맷으로 변경하기
now = arrow.now()
formatted = now.format("YYYY-MM-DD HH:mm:ss")
print(formatted)

Arrow로 날짜와 시간 처리하기

Arrow는 날짜와 시간을 다루는 다양한 함수 및 속성을 제공합니다. 몇 가지 예를 살펴보겠습니다.

import arrow

# 특정한 날짜의 요일 가져오기
date = arrow.get(2022, 1, 1)
weekday = date.format("dddd")
print(weekday)

# UTC 시간으로 변환하기
now = arrow.now()
utc = now.to("UTC")
print(utc)

# 특정한 타임존으로 변환하기
now = arrow.now()
et = now.to("US/Eastern")
print(et)

Arrow는 날짜와 시간 처리에 매우 유연한 기능을 제공하므로 개발자가 날짜와 시간을 쉽게 다룰 수 있습니다.

더 자세한 내용은 Arrow 공식 문서(https://arrow.readthedocs.io/)를 참조하십시오.