## Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending#""" This module defines functions for working with business calendars. """fromtypingimportOptional,Union,ListimportjpyfromdeephavenimportDHErrorfromdeephaven.dtypesimportBusinessCalendar_JCalendars=jpy.get_type("io.deephaven.time.calendar.Calendars")
[docs]defremove_calendar(name:str)->None:""" Removes the calendar with the given name from the set of available options. Args: name (str): the name of the calendar to remove Raises: DHError """try:_JCalendars.removeCalendar(name)exceptExceptionase:raiseDHError(e,f"failed to remove calendar '{name}'")frome
[docs]defadd_calendar(cal:Union[BusinessCalendar,str])->None:""" Adds a new business calendar to the set of available options. Args: cal (Union[BusinessCalendar, str]): business calendar or a path to a business calendar file Raises: DHError """ifcalisNone:raiseDHError(message="cal must be specified")elifisinstance(cal,str):try:_JCalendars.addCalendarFromFile(cal)exceptExceptionase:raiseDHError(e,f"failed to add calendar from file '{cal}'")fromeelse:try:_JCalendars.addCalendar(cal)exceptExceptionase:raiseDHError(e,f"failed to add calendar")frome
[docs]defset_calendar(name:str)->None:""" Sets the default calendar. Args: name (str): the name of the calendar Raises: DHError """try:_JCalendars.setCalendar(name)exceptExceptionase:raiseDHError(e,f"failed to set the default calendar name to '{name}'")frome
[docs]defcalendar_names()->List[str]:""" Returns the names of all available calendar names. Returns: a list of all available calendar names Raises: DHError """try:returnlist(_JCalendars.calendarNames())exceptExceptionase:raiseDHError(e,"failed to obtain the available calendar names.")frome
[docs]defcalendar_name()->str:""" Returns the default business calendar name. The default business calendar is set by the 'Calendar.default' property or by calling 'set_calendar'. Returns: the default business calendar name Raises: DHError """try:return_JCalendars.calendarName()exceptExceptionase:raiseDHError(e,"failed to get the default calendar name.")frome
[docs]defcalendar(name:Optional[str]=None)->BusinessCalendar:""" Returns the calendar with the given name. The returned calendar is an 'io.deephaven.time.calendar.BusinessCalendar' Java object that can be used in Python. For details on the available methods, see https://deephaven.io/core/javadoc/io/deephaven/time/calendar/BusinessCalendar.html. These methods must be called with string arguments or with Java date-time objects. To convert Python date-time objects to Java date-time objects, use the conversion functions in the 'deephaven.time' module. Args: name (str): the name of the calendar. If not specified, the default calendar is returned. Returns: the calendar with the given name or the defalt calendar if name is not specified. Raises: DHError """try:ifnameisNone:return_JCalendars.calendar()else:return_JCalendars.calendar(name)exceptExceptionase:raiseDHError(e,"failed to get the default calendar.")frome