Spaces:
Runtime error
Runtime error
File size: 5,029 Bytes
eaaf050 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
#!/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
@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"]
|