detectFrequency method

String? detectFrequency()

Detects the frequency of the time series based on the timestamps.

Returns a frequency string if a consistent pattern is detected, or null if no consistent frequency is found.

Implementation

String? detectFrequency() {
  if (_timestamps.length < 2) {
    return null;
  }

  final differences = <Duration>[];
  for (int i = 1; i < _timestamps.length; i++) {
    differences.add(_timestamps[i].difference(_timestamps[i - 1]));
  }

  // Check if all differences are the same
  final firstDiff = differences.first;
  if (differences.every((diff) => diff == firstDiff)) {
    return _durationToFrequency(firstDiff);
  }

  return null;
}