def test_time_ago_real_time(self):
        expected_time_now = datetime.now().timestamp() * 1000
        time_now = timestamp_to_ms("now")
        assert abs(expected_time_now - time_now) < 10

        sleep(0.2)

        time_now = timestamp_to_ms("now")
        assert abs(expected_time_now - time_now) > 190
    def _get_start(start, input):
        if not isinstance(input, ScheduleInputSpec):
            raise TypeError(
                "`input` argument must be of type ScheduleInputSpec")

        if start == "now" and input._is_aggregates_used():
            largest_granularity_unit = input._largest_granularity_unit()
            start = timestamp_to_ms(start)
            start -= start % largest_granularity_unit
            start += largest_granularity_unit
        else:
            start = timestamp_to_ms(start)

        return start
 def __init__(
     self,
     start: Union[int, str, datetime],
     end: Union[int, str, datetime],
     id: int = None,
     external_id: str = None,
     aggregate: str = None,
     granularity: str = None,
     include_outside_points: bool = None,
 ):
     self.start = timestamp_to_ms(start)
     self.end = timestamp_to_ms(end)
     self.id = id
     self.external_id = external_id
     self.aggregate = aggregate
     self.granularity = granularity
     self.include_outside_points = include_outside_points
    def get_instances(self, start: Union[int, str, datetime],
                      end: Union[int, str, datetime]) -> List[DataSpec]:
        """Returns the DataSpec objects describing the prediction windows executed between start and end.

        Args:
            start (Union[str, int, datetime]): The start of the time period. Can be either milliseconds since epoch,
                time-ago format (e.g. "1d-ago"), or a datetime object.
            end (Union[str, int, datetime]): The end of the time period. Same format as start. Can also be set to "now".

        Returns:
            List[DataSpec]: List of DataSpec objects, one for each prediction window.
        """
        start, end = timestamp_to_ms(start), timestamp_to_ms(end)

        windows = calculate_windows(start=start,
                                    end=end,
                                    stride=self.stride,
                                    window_size=self.window_size,
                                    first=self.start)

        data_specs = []
        for start, end in windows:
            time_series_specs = {
                alias: TimeSeriesSpec(
                    id=spec.id,
                    external_id=spec.external_id,
                    start=start,
                    end=end,
                    aggregate=spec.aggregate,
                    granularity=spec.granularity,
                    include_outside_points=spec.include_outside_points,
                )
                for alias, spec in self.input.time_series.items()
            }
            data_specs.append(
                DataSpec(
                    time_series=time_series_specs,
                    metadata=DataSpecMetadata(
                        ScheduleSettings(stride=self.stride,
                                         window_size=self.window_size,
                                         start=start,
                                         end=end)),
                ))
        return data_specs
    def get_execution_timestamps(self, start: Union[int, str, datetime],
                                 end: Union[int, str, datetime]) -> List[int]:
        """Returns a list of timestamps indicating when each prediction will be executed.

        This corresponds to the end of each DataSpec returned from get_instances().

        Args:
            start (Union[str, int, datetime]): The start of the time period. Can be either milliseconds since epoch,
                    time-ago format (e.g. "1d-ago"), or a datetime object.
            end (Union[str, int, datetime]): The end of the time period. Same format as start. Can also be set to "now".

        Returns:
            List[int]: A list of timestamps.
        """
        start, end = timestamp_to_ms(start), timestamp_to_ms(end)

        windows = calculate_windows(start=start,
                                    end=end,
                                    stride=self.stride,
                                    window_size=self.window_size,
                                    first=self.start)

        return [w[1] for w in windows]
def _convert_df_to_output_format(df: pd.DataFrame):
    return {
        name: list(zip([timestamp_to_ms(ts) for ts in df.index], df[name]))
        for name in df.columns
    }
 def test_now_cache_diff_over_threshold(self, time_mock):
     time_mock.side_effect = [10**9, 10**9 + 0.101]
     t1 = timestamp_to_ms("1d-ago")
     t2 = timestamp_to_ms("1d-ago")
     assert t2 > t1
 def test_negative(self, t):
     with pytest.raises(ValueError, match="negative"):
         timestamp_to_ms(t)
 def test_invalid(self, time_ago_string):
     with pytest.raises(ValueError, match=time_ago_string):
         timestamp_to_ms(time_ago_string)
    def test_time_ago(self, time_mock, time_ago_string, expected_timestamp):
        time_mock.return_value = 10**9

        assert timestamp_to_ms(time_ago_string) == expected_timestamp
 def test_datetime(self):
     assert 1514764800000 == timestamp_to_ms(datetime(2018, 1, 1))
     assert 1546300800000 == timestamp_to_ms(datetime(2019, 1, 1))
 def test_ms(self):
     assert 1514760000000 == timestamp_to_ms(1514760000000)
     assert 1514764800000 == timestamp_to_ms(1514764800000)
 def test_invalid_type(self, t):
     with pytest.raises(TypeError, match="must be"):
         timestamp_to_ms(t)