diff --git a/PIconnect/PIData.py b/PIconnect/PIData.py index 0ece90af7..8e76d1aa6 100644 --- a/PIconnect/PIData.py +++ b/PIconnect/PIData.py @@ -52,6 +52,7 @@ TimestampCalculation, get_enumerated_value, ) +from PIconnect.time import to_af_time_range class PISeries(Series): @@ -214,11 +215,11 @@ def recorded_values( filtered values are always left out entirely. Args: - start_time (str): String containing the date, and possibly time, + start_time (str or datetime): Containing the date, and possibly time, from which to retrieve the values. This is parsed, together with `end_time`, using :afsdk:`AF.Time.AFTimeRange `. - end_time (str): String containing the date, and possibly time, + end_time (str or datetime): Containing the date, and possibly time, until which to retrieve values. This is parsed, together with `start_time`, using :afsdk:`AF.Time.AFTimeRange `. @@ -237,7 +238,7 @@ def recorded_values( `ValueError` is raised. """ - time_range = AF.Time.AFTimeRange(start_time, end_time) + time_range = to_af_time_range(start_time, end_time) boundary_type = self.__boundary_types.get(boundary_type.lower()) filter_expression = self._normalize_filter_expression(filter_expression) if boundary_type is None: @@ -275,11 +276,11 @@ def interpolated_values(self, start_time, end_time, interval, filter_expression= and filtered values are always left out entirely. Args: - start_time (str): String containing the date, and possibly time, + start_time (str or datetime): Containing the date, and possibly time, from which to retrieve the values. This is parsed, together with `end_time`, using :afsdk:`AF.Time.AFTimeRange `. - end_time (str): String containing the date, and possibly time, + end_time (str or datetime): Containing the date, and possibly time, until which to retrieve values. This is parsed, together with `start_time`, using :afsdk:`AF.Time.AFTimeRange `. @@ -293,7 +294,7 @@ def interpolated_values(self, start_time, end_time, interval, filter_expression= Returns: PISeries: Timeseries of the values returned by the SDK """ - time_range = AF.Time.AFTimeRange(start_time, end_time) + time_range = to_af_time_range(start_time, end_time) interval = AF.Time.AFTimeSpan.Parse(interval) filter_expression = self._normalize_filter_expression(filter_expression) pivalues = self._interpolated_values(time_range, interval, filter_expression) @@ -321,10 +322,10 @@ def summary( Return one or more summary values over a single time range. Args: - start_time (str): String containing the date, and possibly time, + start_time (str or datetime): Containing the date, and possibly time, from which to retrieve the values. This is parsed, together with `end_time`, using :afsdk:`AF.Time.AFTimeRange `. - end_time (str): String containing the date, and possibly time, + end_time (str or datetime): Containing the date, and possibly time, until which to retrieve values. This is parsed, together with `start_time`, using :afsdk:`AF.Time.AFTimeRange `. summary_types (int or PIConsts.SummaryType): Type(s) of summaries @@ -342,7 +343,7 @@ def summary( pandas.DataFrame: Dataframe with the unique timestamps as row index and the summary name as column name. """ - time_range = AF.Time.AFTimeRange(start_time, end_time) + time_range = to_af_time_range(start_time, end_time) summary_types = int(summary_types) calculation_basis = int(calculation_basis) time_type = int(time_type) @@ -372,11 +373,11 @@ def summaries( Return one or more summary values for each interval within a time range Args: - start_time (str): String containing the date, and possibly time, + start_time (str or datetime): Containing the date, and possibly time, from which to retrieve the values. This is parsed, together with `end_time`, using :afsdk:`AF.Time.AFTimeRange `. - end_time (str): String containing the date, and possibly time, + end_time (str or datetime): Containing the date, and possibly time, until which to retrieve values. This is parsed, together with `start_time`, using :afsdk:`AF.Time.AFTimeRange `. @@ -398,7 +399,7 @@ def summaries( pandas.DataFrame: Dataframe with the unique timestamps as row index and the summary name as column name. """ - time_range = AF.Time.AFTimeRange(start_time, end_time) + time_range = to_af_time_range(start_time, end_time) interval = AF.Time.AFTimeSpan.Parse(interval) summary_types = int(summary_types) calculation_basis = int(calculation_basis) @@ -435,11 +436,11 @@ def filtered_summaries( Return one or more summary values for each interval within a time range Args: - start_time (str): String containing the date, and possibly time, + start_time (str or datetime): String containing the date, and possibly time, from which to retrieve the values. This is parsed, together with `end_time`, using :afsdk:`AF.Time.AFTimeRange `. - end_time (str): String containing the date, and possibly time, + end_time (str or datetime): String containing the date, and possibly time, until which to retrieve values. This is parsed, together with `start_time`, using :afsdk:`AF.Time.AFTimeRange `. @@ -472,7 +473,7 @@ def filtered_summaries( pandas.DataFrame: Dataframe with the unique timestamps as row index and the summary name as column name. """ - time_range = AF.Time.AFTimeRange(start_time, end_time) + time_range = to_af_time_range(start_time, end_time) interval = AF.Time.AFTimeSpan.Parse(interval) filter_expression = self._normalize_filter_expression(filter_expression) calculation_basis = get_enumerated_value( diff --git a/PIconnect/time.py b/PIconnect/time.py new file mode 100644 index 000000000..26edef8ef --- /dev/null +++ b/PIconnect/time.py @@ -0,0 +1,22 @@ +from datetime import datetime +from typing import Union + +from PIconnect.AFSDK import AF + + +def to_af_time_range(start_time: Union[str, datetime], end_time: Union[str, datetime]): + """to_af_time_range + + Return AF.Time.AFTimeRange object from datetime or string + using :afsdk:`AF.Time.AFTimeRange `. + + If string is used, it is assumed that user knows the format + that should be passed to AF.Time.AFTimeRange. + """ + + if isinstance(start_time, datetime): + start_time = start_time.isoformat() + if isinstance(end_time, datetime): + end_time = end_time.isoformat() + + return AF.Time.AFTimeRange(start_time, end_time)