टाइमज़ोन कन्वर्ट करने के लिए फंक्शन डिफाइन करना
आप एक शेड्यूलिंग असिस्टेंट बना रहे हैं जो अलग-अलग टाइमज़ोन में मीटिंग्स को कोऑर्डिनेट करने में मदद करता है। OpenTimezone API टाइमज़ोन कन्वर्ज़न सर्विस देता है—आपको बस एक datetime, सोर्स टाइमज़ोन, और टार्गेट टाइमज़ोन भेजना है, और यह कन्वर्ट किया हुआ समय लौटाता है। आपका काम ऐसा फंक्शन बनाना है जो यह API कॉल करे और एक फॉर्मेट किया हुआ परिणाम लौटाए।
requests और json मॉड्यूल आपके लिए पहले से इम्पोर्ट किए गए हैं।
यह अभ्यास पाठ्यक्रम का हिस्सा है
OpenAI Responses API के साथ काम करना
अभ्यास निर्देश
urlवैरिएबल को"https://api.opentimezone.com/convert"पर सेट करें।payloadनाम का एक डिक्शनरी बनाएँ, जिसमें कीज़"dateTime","fromTimezone", और"toTimezone"हों, जिन्हें फंक्शन के आर्गुमेंट्स से मैप करें।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)