Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python | |
| # coding=utf-8 | |
| # Copyright 2024 The Footscray Coding Collective. All rights reserved. | |
| from datetime import datetime | |
| from typing import Optional | |
| import pytz | |
| from smolagents import tool | |
| def get_temporal_context( | |
| timezone_str: str = "US/Eastern", market: str = "US", date_str: Optional[str] = None | |
| ) -> str: | |
| """ | |
| Provides a concise overview of the current temporal context, including date, time, and market status. | |
| Args: | |
| timezone_str: The timezone to display time in (default: US/Eastern) | |
| market: Market identifier (US, EU, ASIA) (default: US) | |
| date_str: Date in YYYY-MM-DD format (optional, defaults to current date if not provided) | |
| Returns: | |
| A formatted string containing the current date, time, year, trading day status, and market hours status. | |
| """ | |
| try: | |
| # Get current time information using pytz | |
| try: | |
| tz = pytz.timezone(timezone_str) | |
| except pytz.exceptions.UnknownTimeZoneError: | |
| return f"Error: Unknown timezone '{timezone_str}'. Try using standard timezone names like 'US/Eastern'." | |
| now = datetime.now(tz) | |
| current_date = now.strftime("%Y-%m-%d") | |
| current_time = now.strftime("%H:%M:%S") | |
| current_year = now.year | |
| weekday_name = now.strftime("%A") | |
| time_info = f"""Current Time Information: | |
| - Date: {current_date} ({weekday_name}) | |
| - Time: {current_time} ({timezone_str}) | |
| - Year: {current_year} | |
| """ | |
| # Get Market hours Information | |
| if market == "US": | |
| # Convert time to US/Eastern for US market check | |
| eastern_tz = pytz.timezone("US/Eastern") | |
| eastern_now = now.astimezone(eastern_tz) | |
| is_weekday_us = eastern_now.weekday() < 5 | |
| us_minutes = eastern_now.hour * 60 + eastern_now.minute | |
| us_market_open = 9 * 60 + 30 # 9:30 AM ET | |
| us_market_close = 16 * 60 # 4:00 PM ET | |
| if is_weekday_us and us_market_open <= us_minutes < us_market_close: | |
| market_status = "Open" | |
| else: | |
| market_status = "Closed" | |
| market_hours_info = f"US Markets (NYSE, NASDAQ): {market_status}" | |
| elif market == "EU": | |
| # Convert time to London for EU market check | |
| london_tz = pytz.timezone("Europe/London") | |
| london_now = now.astimezone(london_tz) | |
| is_weekday_eu = london_now.weekday() < 5 | |
| eu_minutes = london_now.hour * 60 + london_now.minute | |
| eu_market_open = 8 * 60 # 8:00 AM London | |
| eu_market_close = 16 * 60 + 30 # 4:30 PM London | |
| if is_weekday_eu and eu_market_open <= eu_minutes < eu_market_close: | |
| market_status = "Open" | |
| else: | |
| market_status = "Closed" | |
| market_hours_info = f"European Markets (LSE, Euronext): {market_status}" | |
| elif market == "ASIA": | |
| # Convert time to Tokyo for Asian market check | |
| tokyo_tz = pytz.timezone("Asia/Tokyo") | |
| tokyo_now = now.astimezone(tokyo_tz) | |
| is_weekday_tokyo = tokyo_now.weekday() < 5 | |
| tokyo_minutes = tokyo_now.hour * 60 + tokyo_now.minute | |
| tokyo_morning_open = 9 * 60 # 9:00 AM Tokyo | |
| tokyo_morning_close = 11 * 60 + 30 # 11:30 AM Tokyo | |
| tokyo_afternoon_open = 12 * 60 + 30 # 12:30 PM Tokyo | |
| tokyo_afternoon_close = 15 * 60 # 3:00 PM Tokyo | |
| is_tokyo_session = ( | |
| tokyo_morning_open <= tokyo_minutes < tokyo_morning_close | |
| ) or (tokyo_afternoon_open <= tokyo_minutes < tokyo_afternoon_close) | |
| if is_weekday_tokyo and is_tokyo_session: | |
| market_status = "Open" | |
| else: | |
| market_status = "Closed" | |
| market_hours_info = ( | |
| "Asian Markets (Tokyo Stock Exchange, Shanghai Stock Exchange, " | |
| f"Australian Securities Exchange): {market_status}" | |
| ) | |
| else: | |
| return f"Error: Invalid market '{market}'. Supported markets are 'US', 'EU', and 'ASIA'." | |
| # Get Trading Day Information | |
| if date_str: | |
| try: | |
| date_obj = datetime.strptime(date_str, "%Y-%m-%d") | |
| # Apply timezone to date_obj | |
| date_obj = tz.localize(date_obj) | |
| except ValueError: | |
| return ( | |
| f"Error: Invalid date format '{date_str}'. Use YYYY-MM-DD format." | |
| ) | |
| else: | |
| date_obj = now | |
| date_str = now.strftime("%Y-%m-%d") | |
| is_weekend = date_obj.weekday() > 4 | |
| trading_day = "No" if is_weekend else "Yes" | |
| trading_info = f"Trading Day: {trading_day}" | |
| # Combine all information | |
| final_result = f"""{time_info} | |
| {market_hours_info} | |
| - {trading_info} | |
| """ | |
| return final_result | |
| except Exception as e: | |
| return f"Error retrieving temporal context: {str(e)}" | |
| __all__ = ["get_temporal_context"] | |