File size: 2,964 Bytes
1c75184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
        }