将日期范围划分为几个特定的​​日期范围块 [英] Split date range into several specific date range chunks

查看:110
本文介绍了将日期范围划分为几个特定的​​日期范围块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期范围的常规列表,并带有特定值:

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.

例如,如果我输入了日期范围 01.01.2016 - 09.02.2016 ,我需要得到一个输出日期范围和系数:

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

但是,如果我输入的日期范围是 01.01.2016-29.04.2017 ,则需要获取以下范围和系数:

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

用于输出数据的类:

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;
    }

推荐答案

因此,我将在这里走出去,假设在您的第二个示例中,开始日期应该早于 01-01-2016 -因为如果我理解这个问题,则希望返回与您传递给该方法的开始到结束时间重叠的所有范围.
如果确实如此,那么您很亲密-但您的情况不正确.
测试两个范围是否重叠的方法是检查一个范围是否先于另一范围开始,反之亦然,正如您在标记:

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.

所以您的方法应该是:

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;
}

这篇关于将日期范围划分为几个特定的​​日期范围块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆