shiftTimeserieToNow.tsx 599 B

123456789101112131415161718192021
  1. import type {TimeseriesData} from '../common/types';
  2. export function shiftTimeserieToNow(timeserie: TimeseriesData): TimeseriesData {
  3. const currentTimestamp = new Date().getTime();
  4. const lastDatum = timeserie.data.at(-1);
  5. if (!lastDatum) {
  6. return timeserie;
  7. }
  8. const lastTimestampInTimeserie = new Date(lastDatum.timestamp).getTime();
  9. const diff = currentTimestamp - lastTimestampInTimeserie;
  10. return {
  11. ...timeserie,
  12. data: timeserie.data.map(datum => ({
  13. ...datum,
  14. timestamp: new Date(new Date(datum.timestamp).getTime() + diff).toISOString(),
  15. })),
  16. };
  17. }