Split date range into several specific date range chunks

ε祈祈猫儿з 提交于 2021-01-28 02:44:29

问题


I have regular list of date ranges with specific value:

14.09.2012 - 31.12.2015 = 8.25
01.01.2016 - 13.06.2016 = 11.00
14.06.2016 - 18.09.2016 = 10.50
19.09.2016 - 26.03.2017 = 10.00
27.03.2017 - 01.05.2017 = 9.75
02.05.2017 - 18.06.2017 = 9.25
19.06.2017 - 17.09.2017 = 9.00
18.09.2017 - 29.10.2017 = 8.50
30.10.2017 - 17.12.2017 = 8.25
18.12.2017 - 11.02.2018 = 7.75
12.02.2018 - 25.03.2018 = 7.50
26.03.2018 - 16.09.2018 = 7.25
17.09.2018 - NOW = 7.50

I am looking for a method that divide one input data range into above data ranges taking into account coefficient value.

For example, if I have input date range 01.01.2016 - 09.02.2016, I need to get one output date range and coefficient:

01.01.2016 - 13.06.2016 = 11.00

But if I have input date range 01.01.2016 - 29.04.2017, I need to get following ranges and coefficients:

14.09.2012 - 31.12.2015 = 8.25 
01.01.2016 - 13.06.2016 = 11.00 
14.06.2016 - 18.09.2016 = 10.50 
19.09.2016 - 26.03.2017 = 10.00
27.03.2017 - 01.05.2017 = 9.75

Class for output data:

public class OutputItem 
{

    public OutputItem()
    {

    }

    public DateTime Start { get; set; } = new DateTime();

    public DateTime End { get; set; } = new DateTime();

    public double Coeff { get; set; } = 0;
}

Method that I try to get output data

    private List<OutputItem> GetOutput(DateTime start, DateTime end, List<OutputItem> specificRanges)
    {
        List<OutputItem> periods = new List<OutputItem>();

        foreach (OutputItem sr in specificRanges)
        {
            if (start >= sr.Start && sr.End <= end)
            {
                periods.Add(new OutputItem { Start = sr.Start, End = sr.End, Coeff = sr.Coeff });
            }
        }

        return periods;
    }

回答1:


So I'm going to go out on a limb here and assume that in your second example the start date should have been before 01-01-2016 - because if I understand the question, you are looking to return all the ranges that overlap the start to end time you are passing to the method.
If that is indeed the case, you are close - but your condition is wrong.
The way to test if two ranges overlap is to check if one starts before the other ends and vice versa, as you can see in the wiki of the overlap tag:

Two or more elements overlap when they partially or totally cover one another. The way to find if the elements overlap or not is to test if one elements begins before the second one ends, while the second one begins before the first one ends.

So your method should be:

private List<OutputItem> GetOutput(DateTime start, DateTime end, List<OutputItem> specificRanges)
{
    List<OutputItem> periods = new List<OutputItem>();

    foreach (OutputItem sr in specificRanges)
    {
        if (start >= sr.End && sr.Start <= end)
        {
            periods.Add(new OutputItem { Start = sr.Start, End = sr.End, Coeff = sr.Coeff });
        }
    }

    return periods;
}


来源:https://stackoverflow.com/questions/53020632/split-date-range-into-several-specific-date-range-chunks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!