选择SQLDateTimes的范围

时间:2011-08-08 18:10:17

标签: wolfram-mathematica date-range sqldatetime

table = {{ID1, SQLDateTime[{1978, 1, 10, 0, 0, 0.`}]}, 
{ID2, SQLDateTime[{1999, 1, 10, 0, 0, 0.`}]}, 
{ID3, SQLDateTime[{2010, 9, 10, 0, 0, 0.`}]}, 
{ID4, SQLDateTime[{2011, 1, 10, 0, 0, 0.`}]}}

我想在table中返回SQLDateTime在去年(DatePlus[{-1, "Year"}])内的所有案例。如何指定搜索这些案例?

2 个答案:

答案 0 :(得分:6)

您也可以使用DateDifference

Cases[table, {a_, SQLDateTime[b_]} /; 
  DateDifference[b, DateList[], "Year"][[1]] <= 1]

答案 1 :(得分:4)

Select[table, (AbsoluteTime[ DatePlus[{-1, "Year"}]] <= 
    AbsoluteTime[ #[[2, 1]]] <= AbsoluteTime[ ] &)]

(* ==> {{ID3, SQLDateTime[{2010, 9, 10, 0, 0, 0.}]}, 
        {ID4,SQLDateTime[{2011, 1, 10, 0, 0, 0.}]}
        } 
*)

根据Leonid的评论,小更新(Date []的预缓存):

With[
 {date = Date[]},
 Select[table, 
    (AbsoluteTime[ DatePlus[date, {-1, "Year"}]] <= 
     AbsoluteTime[ #[[2, 1]]] <= AbsoluteTime[date ] &)]
 ]

这也消除了原始DatePlus[{-1, "Year"}]的问题,该问题仅考虑今天的日期,而不是当前的时间