Source code for langchain_google_community.calendar.current_datetime
"""Get the current datetime according to the calendar timezone."""fromdatetimeimportdatetimefromtypingimportOptional,Typefromlangchain_core.callbacksimportCallbackManagerForToolRunfrompydanticimportBaseModel,FieldfromzoneinfoimportZoneInfofromlangchain_google_community.calendar.baseimportCalendarBaseTool
[docs]classCurrentDatetimeSchema(BaseModel):"""Input for GetCurrentDatetime."""calendar_id:Optional[str]=Field(default="primary",description="The calendar ID. Defaults to 'primary'.")
[docs]classGetCurrentDatetime(CalendarBaseTool):# type: ignore[override, override]"""Tool that gets the current datetime according to the calendar timezone."""name:str="get_current_datetime"description:str=("Use this tool to get the current datetime according to the calendar timezone.""The output datetime format is 'YYYY-MM-DD HH:MM:SS'")args_schema:Type[CurrentDatetimeSchema]=CurrentDatetimeSchema
[docs]defget_timezone(self,calendar_id:Optional[str])->str:"""Get the timezone of the specified calendar."""calendars=self.api_resource.calendarList().list().execute().get("items",[])ifnotcalendars:raiseValueError("No calendars found.")ifcalendar_id=="primary":returncalendars[0]["timeZone"]else:foritemincalendars:ifitem["id"]==calendar_idanditem["accessRole"]!="reader":returnitem["timeZone"]raiseValueError(f"Timezone not found for calendar ID: {calendar_id}")
def_run(self,calendar_id:Optional[str]="primary",run_manager:Optional[CallbackManagerForToolRun]=None,)->str:"""Run the tool to create an event in Google Calendar."""try:timezone=self.get_timezone(calendar_id)date_time=datetime.now(ZoneInfo(timezone)).strftime("%Y-%m-%d %H:%M:%S")returnf"Time zone: {timezone}, Date and time: {date_time}"exceptExceptionaserror:raiseException(f"An error occurred: {error}")fromerror