您可以在 postgresql 中实现python dateutil rrule。
CREATE OR REPLACE FUNCTION _pc_after(
t_rule TEXT,
t_after TIMESTAMP,
inc BOOLEAN DEFAULT TRUE)
RETURNS TIMESTAMP AS
$$
from dateutil.rrule import *
from dateutil.parser import *
rule = rrulestr(t_rule)
_after = parse(t_after)
occurence = rule.before(_after, inc=False)
return occurence
$$
LANGUAGE 'plpythonu' VOLATILE;
-- = simple before function =
CREATE OR REPLACE FUNCTION _pc_before(
t_rule TEXT,
t_before TIMESTAMP,
inc BOOLEAN DEFAULT TRUE)
RETURNS TIMESTAMP AS
$$
from dateutil.rrule import *
from dateutil.parser import *
_rule = rrulestr(t_rule)
_before = parse(t_before)
occurence = _rule.before(_before, inc=False)
return occurence
$$
LANGUAGE 'plpythonu' VOLATILE;
-- = simple between function =
CREATE OR REPLACE FUNCTION _pc_between(
t_rule TEXT,
t_after TIMESTAMP,
t_before TIMESTAMP,
inc BOOLEAN DEFAULT FALSE)
RETURNS SETOF TIMESTAMP AS
$$
from dateutil.rrule import *
from dateutil.parser import *
_rule = rrulestr(t_rule)
_after = parse(t_after)
_before = parse(t_before)
occurences = _rule.between(_after, _before, inc)
return occurences
$$
LANGUAGE 'plpythonu' VOLATILE;
CREATE OR REPLACE FUNCTION _pc_between(
t_rule TEXT,
t_after TIMESTAMP,
t_before TIMESTAMP,
inc BOOLEAN DEFAULT FALSE)
RETURNS SETOF TIMESTAMP AS
$$
from dateutil.rrule import *
from dateutil.parser import *
_rule = rrulestr(t_rule)
_after = parse(t_after)
_before = parse(t_before)
occurences = _rule.between(_after, _before, inc)
return occurences
$$
LANGUAGE 'plpythonu' VOLATILE;
所以 :
SELECT _pc_between('DTSTART:20130102T090000
RRULE:FREQ=DAILY;INTERVAL=10;COUNT=25',
cast('2013-01-01' as timestamp),
cast('2013-05-31' as timestamp)
);
将返回 :
"2013-01-02 09:00:00"
"2013-01-12 09:00:00"
"2013-01-22 09:00:00"
"2013-02-01 09:00:00"
"2013-02-11 09:00:00"
"2013-02-21 09:00:00"
"2013-03-03 09:00:00"
"2013-03-13 09:00:00"
"2013-03-23 09:00:00"
"2013-04-02 09:00:00"
"2013-04-12 09:00:00"
"2013-04-22 09:00:00"
"2013-05-02 09:00:00"
"2013-05-12 09:00:00"
"2013-05-22 09:00:00"