2013-03-17 2 views
0

거래 시간을 해결하기 위해 파이썬의 루블을 사용하고 있습니다.RRule은 요일과 시간으로 설정됩니다.

가이 주에 대한 훌륭한 작품을하는 동안 문제가
def get_rset(start_date):  
    # Create a rule to recur every weekday starting today 
    r = rrule.rrule(rrule.DAILY, 
        byweekday=[rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR], 
        dtstart=start_date) 
    # Create a rruleset 
    rs = rrule.rruleset() 
    # Attach our rrule to it 
    rs.rrule(r) 
    # Add holidays as exclusion days 
    for exdate in holidays: 
     rs.exdate(exdate) 
    return rs 

, 나는 다르게 외환 날짜를 계산해야합니다 : 그것은 내가이 매우 사이트에서 발견 된 약간 변형 예를 사용하고, 일에 대한 쉽다. 나는 매시간 작업해야하며 공휴일에 추가해야합니다.

UTC에서 나는 시장이 다음 금요일 금요일 일요일 10 시부 터 밤 10 시까 지 열려 있다고 믿습니다.

이제 이것을 rrule로 만들려면 일요일과 금요일에는 특별 시간이 필요하고 나머지 평일에는 모든 시간이 고려되어야합니다. 나는 내가 rrule byday와 byhour를 혼합해야한다고 확신한다. 그러나 나는 이것을하는 것에 대한 어떤 좋은 예도 할 수 없다.

도움이 매우 환영합니다!

답변

1

나는 Google, 코드 및 클래스 문서와 함께 몇 시간 만에 간단한 방법을 알아 냈습니다. 그것은 약간의 (그러나 적절한) 치트를 사용합니다. 아래 예제 솔루션을 참조하십시오.

from dateutil import rrule 
from datetime import timedelta , datetime 
holidays = [] # This is just a list of dates to exclude 

def datetime_in_x_trading_hours(start_dt,future_hours): 
    # First we add two hours. This is because its simpler to view the timeset 
    # as 24hrs MON - FRI. (This also helps align the dates for the holidays) 
    print start_dt 
    start_dt += timedelta(hours=2) 
    rs = get_fx_rset(start_dt) 
    # Now using the set get the desired time and put the the missing hours 
    future_time = rs[future_hours] 
    future_time -= timedelta(hours=2) 
    return future_time 

def get_fx_rset(start_date_time): 

    # Create a rule to recur every weekday starting today 
    r = rrule.rrule(rrule.HOURLY, 
        byweekday=[rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR], 
        byhour=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23], 
        byminute=[0], 
        bysecond=[0], 
        dtstart=start_date_time) 

    # Create a rruleset 
    rs = rrule.rruleset() 
    # Attach our rrule to it 
    rs.rrule(r) 
    # Add holidays as exclusion days 
    for exdate in holidays: 
     rs.exdate(exdate) 

    return rs 

today = datetime.now() - timedelta(days=2) 
print datetime_in_x_trading_hours(today, 7) 
관련 문제