Esempio n. 1
0
def test_relative_daterange():
    from whoosh.support.relativedelta import relativedelta
    dt = datetime

    schema = fields.Schema(id=fields.STORED, date=fields.DATETIME)
    ix = RamStorage().create_index(schema)
    basedate = datetime(2001, 1, 1)
    count = 0
    with ix.writer() as w:
        while basedate < datetime(2001, 12, 1):
            w.add_document(id=count, date=basedate)
            basedate += timedelta(days=14, hours=16)
            count += 1

    with ix.searcher() as s:
        gap = relativedelta(months=1)
        rf = sorting.DateRangeFacet("date", dt(2001, 1, 1),
                                    dt(2001, 12, 31), gap)
        r = s.search(query.Every(), groupedby={"date": rf})
        assert r.groups("date") == {(dt(2001, 1, 1), dt(2001, 2, 1)): [0, 1, 2],
                                    (dt(2001, 2, 1), dt(2001, 3, 1)): [3, 4],
                                    (dt(2001, 3, 1), dt(2001, 4, 1)): [5, 6],
                                    (dt(2001, 4, 1), dt(2001, 5, 1)): [7, 8],
                                    (dt(2001, 5, 1), dt(2001, 6, 1)): [9, 10],
                                    (dt(2001, 6, 1), dt(2001, 7, 1)): [11, 12],
                                    (dt(2001, 7, 1), dt(2001, 8, 1)): [13, 14],
                                    (dt(2001, 8, 1), dt(2001, 9, 1)): [15, 16],
                                    (dt(2001, 9, 1), dt(2001, 10, 1)): [17, 18],
                                    (dt(2001, 10, 1), dt(2001, 11, 1)): [19, 20],
                                    (dt(2001, 11, 1), dt(2001, 12, 1)): [21, 22],
                                    }
Esempio n. 2
0
def test_relative_daterange():
    from whoosh.support.relativedelta import relativedelta

    dt = datetime

    schema = fields.Schema(id=fields.STORED, date=fields.DATETIME)
    ix = RamStorage().create_index(schema)
    basedate = datetime(2001, 1, 1)
    count = 0
    with ix.writer() as w:
        while basedate < datetime(2001, 12, 1):
            w.add_document(id=count, date=basedate)
            basedate += timedelta(days=14, hours=16)
            count += 1

    with ix.searcher() as s:
        gap = relativedelta(months=1)
        rf = sorting.DateRangeFacet("date", dt(2001, 1, 1), dt(2001, 12, 31), gap)
        r = s.search(query.Every(), groupedby={"date": rf})
        assert r.groups("date") == {
            (dt(2001, 1, 1), dt(2001, 2, 1)): [0, 1, 2],
            (dt(2001, 2, 1), dt(2001, 3, 1)): [3, 4],
            (dt(2001, 3, 1), dt(2001, 4, 1)): [5, 6],
            (dt(2001, 4, 1), dt(2001, 5, 1)): [7, 8],
            (dt(2001, 5, 1), dt(2001, 6, 1)): [9, 10],
            (dt(2001, 6, 1), dt(2001, 7, 1)): [11, 12],
            (dt(2001, 7, 1), dt(2001, 8, 1)): [13, 14],
            (dt(2001, 8, 1), dt(2001, 9, 1)): [15, 16],
            (dt(2001, 9, 1), dt(2001, 10, 1)): [17, 18],
            (dt(2001, 10, 1), dt(2001, 11, 1)): [19, 20],
            (dt(2001, 11, 1), dt(2001, 12, 1)): [21, 22],
        }
Esempio n. 3
0
 def props_to_date(self, p, dt):
     if p.dir == "-":
         dir = -1
     else:
         dir = 1
 
     delta = relativedelta(years=(p.get("years") or 0) * dir,
                           months=(p.get("months") or 0) * dir,
                           weeks=(p.get("weeks") or 0) * dir,
                           days=(p.get("days") or 0) * dir,
                           hours=(p.get("hours") or 0) * dir,
                           minutes=(p.get("mins") or 0) * dir,
                           seconds=(p.get("secs") or 0) * dir)
     return dt + delta