From 20de227d3a5fe149a799062ca596823d0b59631e Mon Sep 17 00:00:00 2001 From: Samuel Date: Thu, 2 Jun 2022 13:25:43 +0200 Subject: [PATCH] Fix previous_summary from summary timer --- lib/archethic/beacon_chain/summary_timer.ex | 25 +++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/archethic/beacon_chain/summary_timer.ex b/lib/archethic/beacon_chain/summary_timer.ex index b15c9d0940..cc2ecd0a51 100644 --- a/lib/archethic/beacon_chain/summary_timer.ex +++ b/lib/archethic/beacon_chain/summary_timer.ex @@ -42,19 +42,20 @@ defmodule Archethic.BeaconChain.SummaryTimer do Returns the list of previous summaries times from the given date """ @spec previous_summary(DateTime.t()) :: DateTime.t() - def previous_summary(date_from = %DateTime{microsecond: {0, 0}}) do - get_interval() - |> CronParser.parse!(true) - |> CronScheduler.get_previous_run_dates(DateTime.to_naive(date_from)) - |> Enum.at(1) - |> DateTime.from_naive!("Etc/UTC") - end - def previous_summary(date_from = %DateTime{}) do - get_interval() - |> CronParser.parse!(true) - |> CronScheduler.get_previous_run_date!(DateTime.to_naive(date_from)) - |> DateTime.from_naive!("Etc/UTC") + cron_expression = CronParser.parse!(get_interval(), true) + naive_date_from = DateTime.to_naive(date_from) + + if Crontab.DateChecker.matches_date?(cron_expression, naive_date_from) do + cron_expression + |> CronScheduler.get_previous_run_dates(naive_date_from) + |> Enum.at(1) + |> DateTime.from_naive!("Etc/UTC") + else + cron_expression + |> CronScheduler.get_previous_run_date!(naive_date_from) + |> DateTime.from_naive!("Etc/UTC") + end end @doc """