본문 바로가기
 

하나성경

읽기 쉬운 성경, 하나성경을 소개합니다.

www.hbible.co.kr

투자를 하며

채권 구매일자와 상환일자 사이에 이자를 받는 횟수를 계산해 보자.

by 컴 여행자 2025. 2. 5.
728x90
반응형

채권 이자

 

 

채권을 구매한 후, 구매한 날짜로부터 채권 상환일자까지 몇 번의 이자를 받을 수 있을까요?

 

채권 구매일자에서 상환일자까지 이자를 받을 수 있는 횟수를 계산하는 코드를 생성해 보았습니다.

 

코드를 생성하며 고려한 사항입니다.

  • 기준일자는 채권 구매일자와 상환일자입니다.
  • 이자를 받는 일자는 상환일자의 일을 기준으로 합니다. (예: 2025년 5월 31일 - 31일이 이자 받는 날)
  • 이자 지급은 1개월마다 이자를 지급하는 채권을 기준으로 했습니다.
  • 존재하지 않는 날짜 여부를 확인하고 올바른 날짜를 찾습니다. (예: 2월 30일, 4월 31일 등)
  • 구매일자의 일과 상환일자의 일이 같은 경우에 구매일자에는 이자가 지급되지 않습니다.
  • 상환일자는 마지막으로 이자가 지급되는 날입니다.

 

코드는 Python 2.7.5, 자바스크립트, 두 가지입니다.

 

먼저 파이썬 코드입니다.

from datetime import datetime
from dateutil.relativedelta import relativedelta

def get_valid_date(year, month, day):
    """ 해당 월에 존재하는 가장 가까운 날짜를 찾는 함수 """
    while True:
        try:
            return datetime(year, month, day)
        except ValueError:
            day -= 1  # 존재하지 않는 날짜라면 하루씩 줄이면서 확인

def count_interest_payments(start_date, end_date):
    start = datetime.strptime(start_date, "%Y-%m-%d") # 구매일자
    end = datetime.strptime(end_date, "%Y-%m-%d") # 상환일자
    interest_day = end.day  # 상환일자의 '일' 값
    count = 0

    # 첫 번째 이자 지급일 설정 (존재하지 않는 날짜 예외 처리 포함)
    current = get_valid_date(start.year, start.month, interest_day)

    # 구매일(start_date)이 이자 지급일 이후라면 다음 달부터 시작
    if current <= start:
        next_month = start + relativedelta(months=1)
        current = get_valid_date(next_month.year, next_month.month, interest_day)

    while current <= end:
        count += 1
        next_month = current + relativedelta(months=1)
        current = get_valid_date(next_month.year, next_month.month, interest_day)

    return count

# 테스트 케이스
print(count_interest_payments("2025-01-30", "2025-04-30"))  # 결과: 3
print(count_interest_payments("2025-01-30", "2025-07-31"))  # 결과: 6
print(count_interest_payments("2025-01-30", "2025-06-29"))  # 결과: 5
print(count_interest_payments("2025-01-30", "2025-06-28"))  # 결과: 5
print(count_interest_payments("2025-01-30", "2025-02-28"))  # 결과: 1 (2025년 2월은 윤년이 아님)
print(count_interest_payments("2024-01-30", "2024-02-29"))  # 결과: 1 (2024년은 윤년이므로 2월 29일이 있음)

 

 

 

다음은 자바스크립트 코드입니다.

function getValidDate(year, month, day) {
    // 존재하지 않는 날짜를 방지하는 함수
    while (true) {
        let date = new Date(year, month, day);
        if (date.getMonth() === month) return date; // 유효한 날짜면 반환
        day -= 1; // 존재하지 않는 날짜면 하루 줄이기
    }
}

function countInterestPayments(startDate, endDate) {
    const start = new Date(startDate); // 구매일자
    const end = new Date(endDate); // 상환일자
    const interestDay = end.getDate(); // 상환일자의 '일' 값
    let count = 0;

    // 첫 번째 이자 지급일 설정 (존재하지 않는 날짜 예외 처리 포함)
    let current = getValidDate(start.getFullYear(), start.getMonth(), interestDay);

    // 구매일(startDate)이 이자 지급일 이후라면 다음 달부터 시작
    if (current <= start) {
        let nextMonth = new Date(start.getFullYear(), start.getMonth() + 1, 1);
        current = getValidDate(nextMonth.getFullYear(), nextMonth.getMonth(), interestDay);
    }

    while (current <= end) {
        count++;
        let nextMonth = new Date(current.getFullYear(), current.getMonth() + 1, 1);
        current = getValidDate(nextMonth.getFullYear(), nextMonth.getMonth(), interestDay);
    }

    return count;
}

// 테스트 케이스
console.log(countInterestPayments("2025-01-30", "2025-04-30")); // 결과: 3
console.log(countInterestPayments("2025-01-30", "2025-07-31")); // 결과: 6
console.log(countInterestPayments("2025-01-30", "2025-06-29")); // 결과: 5
console.log(countInterestPayments("2025-01-30", "2025-06-28")); // 결과: 5
console.log(countInterestPayments("2025-01-30", "2025-02-28")); // 결과: 1 (2025년 2월은 윤년이 아님)
console.log(countInterestPayments("2024-01-30", "2024-02-29")); // 결과: 1 (2024년은 윤년이므로 2월 29일이 있음)

 

 

이상입니다.

 

즐거운 코딩 하세요. ^^

 

 

추신 : 채권 공부에 도움이 되는 사이트도 링크해 봅니다.

 

https://www.bondweb.co.kr/_help/05_guide_01.asp

 

https://www.bondweb.co.kr/_help/05_guide_01.asp

 

www.bondweb.co.kr

 

 

 

 

 

728x90
반응형

댓글