private static int Getcount(bool distinct, DateTime[] timeList, DateTime end, TimeSpan ts)
{
var datemark = end;
int count = 0;
for (int i = 0; i < timeList.Length; i++)
{
var curdate = timeList[i];
if (curdate > end)
{
//在范围外
continue;
}
if (i == 0)
{
if (curdate < end.Subtract(ts))
return 0;
}
if (curdate > datemark.Subtract(ts) && curdate <= datemark)
{
//如果取唯一值,这里只会执行一次
if (!distinct)
count++;
if (distinct && count == 0)
{
count = 1;
}
continue;
}
else if (curdate > datemark.Subtract(ts).Subtract(ts)
&& curdate <= datemark.Subtract(ts))
{
//步进一刻度
datemark = datemark.Subtract(ts);
count++;
continue;
}
//能到这里已经连续
return count;
}
return count;
}
[TestMethod]
public void MyTestMethod_Count()
{
var reader = new[]
{
DateTime.Parse("2012-1-6 1:0:0"),
DateTime.Parse("2012-1-5 1:0:0"),
DateTime.Parse("2012-1-4 1:0:0"),
//DateTime.Parse("2012-1-4 2:0:0"),
DateTime.Parse("2012-1-3 1:0:0"),
//DateTime.Parse("2012-1-2 1:0:0"),
DateTime.Parse("2012-1-1 1:0:0")
};
//Console.WriteLine(Getcount(true, reader));
Assert.AreEqual(4, Getcount(false, reader));
}
[TestMethod]
public void MyTestMethod_Distinct_Count()
{
var reader = new[]
{
DateTime.Parse("2012-1-6 1:0:0"),
DateTime.Parse("2012-1-5 1:0:0"),
DateTime.Parse("2012-1-4 1:0:0"),
DateTime.Parse("2012-1-4 2:0:0"),
DateTime.Parse("2012-1-4 2:0:0"),
DateTime.Parse("2012-1-4 2:0:0"),
DateTime.Parse("2012-1-3 1:0:0"),
//DateTime.Parse("2012-1-2 1:0:0"),
DateTime.Parse("2012-1-1 1:0:0")
};
//Console.WriteLine(Getcount(true, reader));
Assert.AreEqual(4, Getcount(true, reader));
}
[TestMethod]
public void MyTestMethod_distinct()
{
var reader = new[]
{
DateTime.Parse("2012-1-8 2:0:0"),
DateTime.Parse("2012-1-7 2:0:0"),
DateTime.Parse("2012-1-6 2:0:0"),
DateTime.Parse("2012-1-6 1:0:0"),
DateTime.Parse("2012-1-5 1:0:0"),
DateTime.Parse("2012-1-4 1:0:0"),
DateTime.Parse("2012-1-4 2:0:0"),
DateTime.Parse("2012-1-3 1:0:0"),
//DateTime.Parse("2012-1-2 1:0:0"),
DateTime.Parse("2012-1-1 1:0:0")
};
Assert.AreEqual(4, Getcount(true, reader));
}
[TestMethod]
public void MyTestMethod_0()
{
var reader = new DateTime[]
{
// DateTime.Parse("2012-1-6 2:0:0"),
//DateTime.Parse("2012-1-6 1:0:0"),
//DateTime.Parse("2012-1-5 1:0:0"),
//DateTime.Parse("2012-1-4 1:0:0"),
//DateTime.Parse("2012-1-4 2:0:0"),
//DateTime.Parse("2012-1-3 1:0:0"),
////DateTime.Parse("2012-1-2 1:0:0"),
//DateTime.Parse("2012-1-1 1:0:0")
};
Assert.AreEqual(0, Getcount(true, reader));
}
[TestMethod]
public void MyTestMethod_all()
{
MyTestMethod_Count();
MyTestMethod_distinct();
MyTestMethod_0();
}
private static int Getcount(bool distinct, DateTime[] reader)
{
var seconds = 24 * 60 * 60;
var end = DateTime.Parse("2012-1-7 0:0:0");
TimeSpan ts = new TimeSpan(0, 0, seconds);
return Getcount(distinct, reader, end, ts);
}