get_datetime / tool.py
Sajjad313's picture
Upload tool
1c75184 verified
from smolagents import Tool
from typing import Any, Optional
class SimpleTool(Tool):
name = "get_datetime"
description = "Get the current date and time in Gregorian (Miladi), Persian (Jalali), and Hijri (Islamic/Arabic) calendars\nfor a specified timezone region."
inputs = {'region': {'type': 'string', 'description': "The timezone region string (e.g., 'Asia/Tehran', 'Europe/London', 'America/New_York')."}}
output_type = "object"
def forward(self, region: str) -> dict:
"""
Get the current date and time in Gregorian (Miladi), Persian (Jalali), and Hijri (Islamic/Arabic) calendars
for a specified timezone region.
Args:
region (str): The timezone region string (e.g., 'Asia/Tehran', 'Europe/London', 'America/New_York').
Returns:
dict: A dictionary with the following structure:
{
'current_date': {
'persian': 'دوشنبه, 11 تیر 1403',
'miladi': 'Monday, July 01, 2024',
'hijri': 'الاثنين, 24 ذو الحجة 1445'
},
'current_time': '09:15 PM'
}
Example:
>>> forward('Asia/Tehran')
Note:
you should provide the date to the user only the calender they want.
for example if the user want the date in miladi calender you should provide the date only in miladi calender
or if the user want the date in jalali calender(شمسی یا ایرانی) you should provide the date only in jalali calender
and if they don't specify the calender, you find this out based on the user's language,
for example if the user's language is persian you should provide the date in jalali calender and so on.
Accepted values for `region`:
- Any valid IANA timezone string, e.g.:
'Asia/Tehran', 'Europe/London', 'America/New_York', 'Asia/Riyadh', etc.
"""
from datetime import datetime
import pytz
import jdatetime
from hijri_converter import Gregorian
tz = pytz.timezone(region)
now = datetime.now(tz)
# Gregorian (Miladi) date and time
gregorian_date = now.strftime("%A, %B %d, %Y")
time_str = now.strftime("%I:%M %p")
# Persian (Jalali) date
jalali = jdatetime.datetime.fromgregorian(datetime=now)
persian_date = jalali.strftime("%A, %d %B %Y")
# Hijri (Islamic/Arabic) date
hijri = Gregorian(now.year, now.month, now.day).to_hijri()
hijri_date = f"{hijri.day_name('ar')}, {hijri.day} {hijri.month_name('ar')} {hijri.year}"
return {
"current_date": {
"persian": persian_date,
"miladi": gregorian_date,
"hijri": hijri_date
},
"current_time": time_str
}