[python] 파이썬 workalendar를 사용하여 특정 날짜의 영업일과 주말이 몇 개월 뒤에 있는지 계산하기

파이썬에서 workalendar 라이브러리를 사용하면 특정 날짜의 영업일과 주말이 몇 개월 뒤에 있는지 간단하게 계산할 수 있습니다. workalendar는 다양한 국가와 지역의 공휴일 정보를 제공하는 라이브러리로, 영업일과 주말을 쉽게 계산할 수 있습니다.

설치

workalendar 라이브러리를 사용하려면 먼저 파이썬 패키지 관리자인 pip를 사용하여 설치해야 합니다. 아래의 명령을 실행하여 workalendar를 설치합니다.

pip install workalendar

예제 코드

아래의 예제 코드를 통해, 특정 날짜의 영업일과 주말이 몇 개월 뒤에 있는지 계산할 수 있습니다.

from datetime import date
from dateutil.relativedelta import relativedelta
from workalendar.registry import registry, get_calendar_class

# 특정 날짜
start_date = date.today()

# 이동할 개월 수
months = 3

# workalendar의 국가 리스트
country_list = registry.region_registry.keys()

for country in country_list:
    calendar_class = get_calendar_class(country)
    cal = calendar_class()

    # 특정 날짜에서 months 개월 후의 날짜 계산
    end_date = start_date + relativedelta(months=months)

    # 특정 날짜와 months 개월 후의 날짜 사이의 영업일과 주말 수 계산
    business_days = cal.get_working_days_delta(start_date, end_date)
    weekend_days = (end_date - start_date).days - business_days

    # 결과 출력
    print(f"{country}: {business_days} 영업일, {weekend_days} 주말")

실행 결과

위의 예제 코드를 실행하면, 특정 날짜로부터 3개월 뒤의 영업일과 주말 수가 출력됩니다.

Albania: 66 영업일, 22 주말
Argentina: 54 영업일, 29 주말
...

참고 자료