시작하기무료로 시작하기

시간대 변환 함수 정의하기

여러 시간대에 걸친 회의 일정을 조율해 주는 스케줄링 도우미를 만들고 있어요. OpenTimezone API는 시간대 변환 기능을 제공하며, datetime과 기준 시간대, 대상 시간대를 보내면 변환된 시간을 반환해요. 여러분의 과제는 이 API를 호출하고 서식을 갖춘 결과를 반환하는 함수를 만드는 것이에요.

requestsjson 모듈은 이미 임포트되어 있어요.

이 연습은 강의의 일부입니다

OpenAI Responses API 활용하기

강의 보기

연습 안내

  • url 변수를 "https://api.opentimezone.com/convert"로 설정하세요.
  • 함수의 인자를 각각 "dateTime", "fromTimezone", "toTimezone" 키에 매핑한 payload 딕셔너리를 생성하세요.
  • payload를 JSON으로 지정하여 url에 POST 요청을 보내세요.
  • 제공된 값으로 함수를 테스트해 작동을 확인하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

def convert_timezone(date_time: str, from_timezone: str, to_timezone: str) -> str:
    """
    Convert a datetime from one timezone to another.
    
    Args:
        date_time: The datetime string in ISO format
        from_timezone: Source timezone
        to_timezone: Target timezone
    
    Returns:
        A string with the converted datetime and timezone information
    """
    # Set the API endpoint
    url = "____"
    
    # Prepare the request payload
    payload = {"dateTime": ____, "fromTimezone": ____, "toTimezone": ____}
    
    try:
        # Make the API request and extract converted time
        response = requests.post(url, json=____)
        response.raise_for_status()
        
        data = response.json()
        converted_time = data.get('dateTime', 'N/A')
        
        return f"Time in {to_timezone}: {converted_time}"
    
    except requests.exceptions.RequestException as e:
        return f"Error converting timezone: {str(e)}"

# Test the function
result = convert_timezone('2025-01-20T14:30:00', 'America/New_York', 'Europe/London')
print(result)
코드 편집 및 실행