最近做个项目,需要对用户输入的开始日期和结束日期进行校验,要求是输入的日期段不能和以前输入的时间段有交集。所有的日期段都会登记在数据库中。
比如:
今天输入20091229-20091231,明天输入20091230-20100101则校验不过。
考虑到还有这种情况:柜员做交易,结果发现20091101-20091102没有做交易,那么这个时间段在算法中应该也许的。
不知道大家有没有什么好的算法?

解决方案 »

  1.   

    应该不用算法,只是数据库中区域检索或是邻近查询的最基本功能。使用SQL应该就可以了。
      

  2.   


    交集...如果是线性连续的时间.那么只要
    set @last = select Max(DataTime_END) from table...
    当前的输入时间的datatime_start 要大于@last...也不要特别的算法吧?不知道你的两个字段是不是分开存放的
      

  3.   


    20091101-20091102
    要写算法也行...把这些数据实例化到一个Container[datespan(start,end)]中
    然后对datespan排序...线性时间...
    Container再写几个方法,就可以快速判断了...
    这个Container要保持最新状态...refresh..
      

  4.   

     select top 1 * 
    from demotab 
    where 
    ('"&start_time&"'<=start_time and '"&end_time&"'>=end_time) or   --判断新日期段与原日期段是否存在外包含
    ('"&start_time&"'>=start_time and '"&end_time&"'<=end_time) or   --判断新日期段与原日期段是否存在内包含
    ('"&start_time&"'<=start_time and '"&end_time&"'<=end_time and '"&end_time&"'>=start_time) or --判断新日期段与原日期段是否存在左交叉 
    ('"&start_time&"'>=start_time and '"&start_time&"'<=end_time and '"&end_time&"'>=end_time) --判断新日期段与原日期段是否存在右交叉