Пример #1
0
def nearest_lr_date(date: pd.Timestamp, how: str = "previous") -> pd.Timestamp:
    """
    Given any date, returns the date of the nearest LR (Friday) date.

    Can specify "previous" or "next" LR Friday. "previous" is default.

    Parameters
    ----------
    date: pd.Timestamp
        Any date, within reason. 
    how: str
        Tell the function how to find the nearest LR date:
            "this_week" -> LR Friday of the week in which date falls (Mon to Sun)
            "previous" -> most recent LR Friday before date
            "next" -> next LR Friday after date

    Returns
    -------
    pd.Timestamp
        Date of nearest LR Friday to date
    """
    # Looking for the previous LR Friday
    # dateutil.relativedelta (aliased as 'rd') has a list of 2-letter daycodes so FR
    # rd.FR(-1) means the last Friday before now.
    # Does nothing if date is already a Friday.
    if how == "previous":
        lr_date = date + rd.relativedelta(weekday=rd.FR(-1))

    # Looking for the next LR Friday
    # rd.FR(-1) means the next Friday after now.
    # Does nothing if date is already a Friday.
    if how == "next":
        lr_date = date + rd.relativedelta(weekday=rd.FR(+1))

    return lr_date
Пример #2
0
 def setUp(self):
     self._req_id = self._wrapper.next_req_id
     self._end = (
         datetime.datetime.now() +
         relativedelta.relativedelta(weekday=relativedelta.FR(-1))).replace(
             hour=12, minute=0).astimezone(_global.TZ)
     self._start = (self._end - datetime.timedelta(minutes=5)).astimezone(
         _global.TZ)
Пример #3
0
 def get_month_option_expiration(self, day_str, day_str_format='%Y%m%d'):
     day = datetime.datetime.strptime(day_str, day_str_format)
     rr = dr.rrule(dr.MONTHLY,
                   byweekday=drel.FR(3),
                   count=1,
                   dtstart=datetime.datetime(day.year, day.month, 1))
     third_friday = rr[0]
     expire_day = third_friday
     if (not self.is_trade_day(third_friday)):
         expire_day = self.previous_day(third_friday)
     return datetime.datetime.strftime(expire_day, day_str_format)
Пример #4
0
def thirdFridayNum(nDate):
    theFirst = nDate + relativedelta(day=1)
    rr = dr.rrule(dr.WEEKLY, byweekday=drel.FR(3), dtstart=theFirst, count=5, cache=True)
    count = 1;
    for fri3 in rr:
        if fri3 == nDate:
            return count
        else:
            count += 1
    if count > 5:
        return -1.0 * nDate.weekday()

    return count
Пример #5
0
class SimpleObservanceCalendar(Calendar):
    """
    A simple calendar with a couple of holidays with typical observance rules:
    If a holiday falls on a weekend, then its observance is shifted to a
    nearby weekday.
    """
    include_new_years_day = False

    FIXED_HOLIDAYS = (
        Holiday(
            date(2000, 12, 24), 'Christmas Eve', indication='December 24th',
            observance_shift=dict(weekday=rd.FR(-1)),
        ),
        Holiday(date(2000, 12, 25), 'Christmas', indication='December 25th'),
    )

    def get_weekend_days(self):
        return SAT, SUN
def meetup_day(year, month, targetday, targetweek):

    weekdays = dict(zip(list(calendar.day_name), range(7)))
    steps = dict(zip(['1st', '2nd', '3rd', '4th', 'last'], range(7, 36, 7)))
    steps['teenth'] = 19
    targetday = weekdays[targetday]
    getDay = {
        0: relativedelta.MO(-1),
        1: relativedelta.TU(-1),
        2: relativedelta.WE(-1),
        3: relativedelta.TH(-1),
        4: relativedelta.FR(-1),
        5: relativedelta.SA(-1),
        6: relativedelta.SU(-1),
    }

    d = datetime.date(year, month, 1) + relativedelta.relativedelta(
        day=steps[targetweek], weekday=getDay[targetday])
    return d
Пример #7
0
def all_weekdays(year, month, week, day_of_week):
    dt = date(year, month, 1)

    if week == 'last':
        clean_week = int(-1)
        if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
            day_offset = 31
        elif month == 2:
            day_offset = 28
        else:
            day_offset = 30
    else:
        clean_week = +int(week.strip("stndrh"))
        day_offset = 0

    if day_of_week == 'Monday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.MO(clean_week))
        return the_date
    elif day_of_week == 'Tuesday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.TU(clean_week))
        return the_date
    elif day_of_week == 'Wednesday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.WE(clean_week))
        return the_date
    elif day_of_week == 'Thursday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.TH(clean_week))
        return the_date
    elif day_of_week == 'Friday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.FR(clean_week))
        return the_date
    elif day_of_week == 'Saturday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.SA(clean_week))
        return the_date
    elif day_of_week == 'Sunday':
        the_date = dt + rdelta.relativedelta(days=day_offset,
                                             weekday=rdelta.SU(clean_week))
        return the_date
Пример #8
0
 def setUp(self):
     self._end = (
         datetime.datetime.now() +
         relativedelta.relativedelta(weekday=relativedelta.FR(-1))).replace(
             hour=12, minute=0, second=0, microsecond=0)
     self._start = self._end - datetime.timedelta(minutes=5)
Пример #9
0
def isThirdFriday(nDate):
    rr = dr.rrule(dr.MONTHLY, byweekday=drel.FR(3), dtstart=nDate, count=1, cache=True)
    if rr[0] == nDate:
        return True
    else:
        return False
Пример #10
0
def fri(a):
    friday = str(today + rdelta.relativedelta(weekday=rdelta.FR(a)))
    output = friday[8:10] + '/' + friday[5:7] + '/' + friday[2:4]
    return output