Found some "smelly" code...

Code:
 public void SendHourlyDataByUnitId(int unitId, DateTime fromDate, int fromHour, DateTime toDate, int toHour)
        {
            if (fromDate.Date == toDate.Date) // same day, only h. 
            {
                for (int i = fromHour; i <= toHour; i++)
                {
                   SendHourlyData(unitId, fromDate, i);
                }
                return;
            }
            // Not the same day

            //First day
            for (int i = fromHour; i <= 24; i++)
            {
                SendHourlyData(unitId, fromDate, i);
            }
            // Days in middle
            int datesBetween = ((TimeSpan)(toDate.Date - fromDate.Date)).Days;
            for (int i = 1; i < datesBetween; i++)
            {
                for (int n = 1; n <= 24; n++)
                {
                    SendHourlyData(unitId, fromDate + new TimeSpan(i * 1, 0, 0, 0, 0), n);
                }
            }
            // Last day
            for (int i = 1; i <= toHour; i++)
            {
                SendHourlyData(unitId, toDate, i);
            }
        }
As the title says, I am curious how one could refactor the above code into something that might be considered "clean code"

There is a lot of repetition going on three separate for loops that does pretty much the same thing, and the first and the last day in the interval need to be checked for start hour and stop hour, with a special case if the interval is limited to one day only. Surely this special case is not needed?

The SendHourlyData method makes a call to a repository, and should not be called more than one time, and it should also be injected into the class, so this particular unit of code could be unit tested properly.

I think that one loop should be enough, where all the data to send could be collected into a list, and then that list should be parsed to the SendHourlyData method, given that the signature of this method changes to accept a list...

yours
H

/H