zoukankan      html  css  js  c++  java
  • 检测是一个时间串或一个数字串是否连续

     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);
            }
  • 相关阅读:
    一个例子说明如何在DataSnap中使用FireDAC
    DataSnap Demo:TFDConnection、最大连接数、客户端回叫功能、多线程模拟、压力测试等
    DataSnap Demo:TFDConnection、最大连接数、客户端回叫功能、多线程模拟、压力测试等
    Delphi2010中DataSnap技术网摘
    Delphi2010中DataSnap技术网摘
    推荐大家使用的CSS书写规范、顺序
    Windows autoKeras的下载与安装连接
    vscode Python 运行环境配置
    react框架
    关于k Line Chart (k线图)
  • 原文地址:https://www.cnblogs.com/zbw911/p/3105563.html
Copyright © 2011-2022 走看看