Exemplo n.º 1
0
 def __rsub__(self, other):
     if getattr(self, 'bdays', None) is not None:
         other = parse(other)
         if self.bdays == 0:
             while other.weekday() in self.weekdays_off or other in self.holidays:
                 other += rd(days=-1)
     return self.__neg__().__radd__(other)
Exemplo n.º 2
0
 def __rsub__(self, other):
     if getattr(self, 'bdays', None) is not None:
         other = parse(other)
         if self.bdays == 0:
             while other.weekday() in (5, 6) or other in self.holidays:
                 other += rd(days=-1)
     return self.__neg__().__radd__(other)
Exemplo n.º 3
0
def isbday(dt, holidays=None, weekdays_off=None):
    if holidays is None:
        holidays = getattr(isbday, 'holidays', ())
    if weekdays_off is None:
        weekdays_off = getattr(isbday, 'weekdays_off', (5, 6))
    dt = parse(dt)
    return not (dt.weekday() in weekdays_off or dt in holidays)
Exemplo n.º 4
0
 def __new__(cls, *args, **kwargs):
     if len(args) == 1:
         if isinstance(args[0], basetime):
             raise TypeError("bdateutil.date cannot be initialized with "
                             "just a time")
         args = parse(args[0]).timetuple()[:3]
     return basedate.__new__(cls, *args, **kwargs)
Exemplo n.º 5
0
 def __new__(cls, *args, **kwargs):
     if len(args) == 1:
         if isinstance(args[0], basetime):
             raise TypeError("bdateutil.date cannot be initialized with "
                             "just a time")
         args = parse(args[0]).timetuple()[:3]
     return basedate.__new__(cls, *args, **kwargs)
Exemplo n.º 6
0
 def __new__(self, *args, **kwargs):
     if len(args) == 1:
         args = parse(args[0]).timetuple()[:6]
     if len(args) > 2:
         if args[2] == 99:
             args = list(args)
             args[2] = calendar.monthrange(args[0], args[1])[1]
     return basedatetime.__new__(self, *args, **kwargs)
Exemplo n.º 7
0
 def __new__(self, *args, **kwargs):
     if len(args) == 1:
         args = parse(args[0]).timetuple()[:6]
     if len(args) > 2:
         if args[2] == 99:
             args = list(args)
             args[2] = calendar.monthrange(args[0], args[1])[1]
     return basedatetime.__new__(self, *args, **kwargs)
Exemplo n.º 8
0
    def on_data(self, data):
        # Conecta no MySQL para armazenar os Tweets.
            # Decodificando o JSON do Twitter
            datajson = json.loads(data)

            # Pegue os dados desejados do Tweet.
            happening = datajson['text']
            username = datajson['user']['screen_name']
            tweet_id = datajson['id']
            tweeted_at = parser.parse(datajson['created_at']) 

            # Imprimir mensagem do Tweet armazenado.
            print("Tweet armazenado em " + str(tweeted_at))

            # Insert no MySQL.
            store_data(tweeted_at, happening, username, tweet_id)
Exemplo n.º 9
0
def save_results(config, resultset):
    ''' save results to database '''

    r_conn = get_rethink_connection(config)

    eastern = pytz.timezone('US/Eastern')

    total = 0
    for res in resultset:
        for record in res['resultset']:
            try:
                issue_dt = parse(record['issue_date']).date()\
                 .strftime('%Y-%m-%d')
                issue_date = eastern.localize(datetime\
                 .strptime(issue_dt, "%Y-%m-%d"))

                symbol = record['symbol']
                bm = record['benchmark']

                record['id'] = '{}-{}-{}'.format(issue_dt, symbol, bm)
                record['date'] = eastern.localize(datetime.strptime(\
                 get_today().strftime('%Y-%m-%d'), "%Y-%m-%d"))
                record['issue_date'] = issue_date
                record['type'] = res['type']
                record['category'] = res['category']
                logger.info('Record {}'.format(record))

                if symbol != '{{recommendation.symbol | SymbolWithCountry}}':
                    pick_result = rethink.table(STOCK_PICKS)\
                     .insert(record, conflict="replace").run(r_conn)

                    pick_record_ins = pick_result['inserted']
                    total = total + int(pick_record_ins)

                    logger.info('Save Pick: Symbol:{} Firm:{} Date:{} Type:{} Category:{}'\
                     .format(record['symbol'], record['company'], \
                     record['issue_date'],record['type'], record['category']))

            except Exception as ex:
                logger.error('Error in parsing record {}'.format(ex))

    return total
Exemplo n.º 10
0
    def convert_date_and_likes(self):
        dates = self.dates
        like_list = self.like_list
        date_time_array = []
        date_dict, date_double_dict = {}, {}

        # print(len(dates))

        for date in dates:

            parsed_t = dp.parse(date)
            local_time_zone = str(tzlocal.get_localzone())
            t_time = parsed_t.astimezone(
                pytz.timezone(local_time_zone)).strftime('%m/%d/%Y, %H:%M:%S')
            date_time_array.append(t_time)

        for i in range(len(date_time_array)):
            date_dict[i] = date_time_array[i][:10], date_time_array[i][
                12:], like_list[i]

        return date_dict
Exemplo n.º 11
0
 def __add__(self, other):
     if isinstance(other, relativedelta):
         ret = rd.__add__(self, other)
         ret.__class__ = relativedelta
         for attr in ('bdays', 'bhours', 'bminutes', 'bseconds'):
             if getattr(self, attr, None) is not None:
                 if getattr(other, attr, None) is not None:
                     setattr(ret, attr,
                             getattr(self, attr) + getattr(other, attr))
                 else:
                     setattr(ret, attr, getattr(self, attr))
             elif getattr(other, attr, None) is not None:
                 setattr(ret, attr, getattr(other, attr))
         return ret
     ret = parse(other)
     # If we are adding any time (not just dates) the ret object to return
     # must be a datetime object; a date object will not work
     if not isinstance(ret, datetime) \
             and (self.bhours or self.bminutes or self.bseconds):
         ret = datetime.combine(ret, datetime.min.time())
     for attr in ('bseconds', 'bminutes', 'bhours', 'bdays'):
         if getattr(self, attr, None) is not None:
             while ret.weekday() in (5, 6) or ret in self.holidays:
                 ret += rd(days=+1)
             while attr != "bdays" and \
                     (ret.time() < self.btstart or
                      ret.time() >= self.btend):
                 ret += rd(**{attr[1:]: +1})
             i = getattr(self, attr)
             a = +1 if i > 0 else -1
             while i != 0:
                 ret += rd(**{attr[1:]: a})
                 while ret.weekday() in (5, 6) or ret in self.holidays:
                     ret += rd(days=a)
                 while attr != "bdays" and \
                         (ret.time() < self.btstart or
                          ret.time() >= self.btend):
                     ret += rd(**{attr[1:]: a})
                 i -= a
     return rd.__add__(self, ret)
Exemplo n.º 12
0
 def __add__(self, other):
     if isinstance(other, relativedelta):
         ret = rd.__add__(self, other)
         ret.__class__ = relativedelta
         for attr in ('bdays', 'bhours', 'bminutes', 'bseconds'):
             if getattr(self, attr, None) is not None:
                 if getattr(other, attr, None) is not None:
                     setattr(ret, attr,
                             getattr(self, attr) + getattr(other, attr))
                 else:
                     setattr(ret, attr, getattr(self, attr))
             elif getattr(other, attr, None) is not None:
                 setattr(ret, attr, getattr(other, attr))
         return ret
     ret = parse(other)
     # If we are adding any time (not just dates) the ret object to return
     # must be a datetime object; a date object will not work
     if not isinstance(ret, datetime) \
             and (self.bhours or self.bminutes or self.bseconds):
         ret = datetime.combine(ret, datetime.min.time())
     for attr in ('bseconds', 'bminutes', 'bhours', 'bdays'):
         if getattr(self, attr, None) is not None:
             while ret.weekday() in self.weekdays_off or ret in self.holidays:
                 ret += rd(days=+1)
             while attr != "bdays" and \
                     (ret.time() < self.btstart or
                      ret.time() >= self.btend):
                 ret += rd(**{attr[1:]: +1})
             i = getattr(self, attr)
             a = +1 if i > 0 else -1
             while i != 0:
                 ret += rd(**{attr[1:]: a})
                 while ret.weekday() in self.weekdays_off or ret in self.holidays:
                     ret += rd(days=a)
                 while attr != "bdays" and \
                         (ret.time() < self.btstart or
                          ret.time() >= self.btend):
                     ret += rd(**{attr[1:]: a})
                 i -= a
     return rd.__add__(self, ret)
Exemplo n.º 13
0
def main():
    if len(sys.argv) != 4:
        sys.exit("USAGE: %s <wikiname> <language> <domain>" % sys.argv[0])
    found = Wiki.objects.filter(name=sys.argv[1], language=sys.argv[2],
                                domain=sys.argv[3])
    if found:
        w = found[0]
    else:
        sys.exit("The Wiki is not in the database."
                 " Use add-wiki.py to fix that.")
    with transaction.atomic():
        for line in sys.stdin:
            parsed = json.loads(line)
            # {"ip": "81.43.234.228", "id": "378742081", "timestamp":
            #  "2010-08-13T17:25:26Z", "title": "User talk:Furret Boy"}
            e = Edit()
            e.ip = parsed['ip']
            e.title = parsed['title']
            e.time = dateparser.parse(parsed['timestamp'])
            e.wikipedia_edit_id = parsed['id']
            e.wiki = w
            e.save()
Exemplo n.º 14
0
def main():
    if len(sys.argv) != 4:
        sys.exit("USAGE: %s <wikiname> <language> <domain>" % sys.argv[0])
    found = Wiki.objects.filter(name=sys.argv[1],
                                language=sys.argv[2],
                                domain=sys.argv[3])
    if found:
        w = found[0]
    else:
        sys.exit("The Wiki is not in the database."
                 " Use add-wiki.py to fix that.")
    with transaction.atomic():
        for line in sys.stdin:
            parsed = json.loads(line)
            # {"ip": "81.43.234.228", "id": "378742081", "timestamp":
            #  "2010-08-13T17:25:26Z", "title": "User talk:Furret Boy"}
            e = Edit()
            e.ip = parsed['ip']
            e.title = parsed['title']
            e.time = dateparser.parse(parsed['timestamp'])
            e.wikipedia_edit_id = parsed['id']
            e.wiki = w
            e.save()
Exemplo n.º 15
0
def isbday(dt, holidays=None):
    if holidays is None:
        holidays = getattr(isbday, 'holidays', ())
    dt = parse(dt)
    return not (dt.weekday() in (5, 6) or dt in holidays)
Exemplo n.º 16
0
 def __new__(cls, *args, **kwargs):
     if len(args) == 1:
         if isinstance(args[0], basetime):
             args = (basedatetime.combine(basedatetime.today(), args[0]), )
         args = parse(args[0]).timetuple()[:6]
     return basedatetime.__new__(cls, *args, **kwargs)
Exemplo n.º 17
0
def isbday(dt, holidays=None):
    if holidays is None:
        holidays = HOLIDAYS
    dt = parse(dt)
    return dt.weekday() in WORKDAYS and dt not in holidays
Exemplo n.º 18
0
 def __new__(cls, *args, **kwargs):
     if len(args) == 1:
         if isinstance(args[0], basetime):
             args = (basedatetime.combine(basedatetime.today(), args[0]), )
         args = parse(args[0]).timetuple()[:6]
     return basedatetime.__new__(cls, *args, **kwargs)
Exemplo n.º 19
0
 def __init__(self, dt1=None, dt2=None, bdays=None, holidays=None,
              bhours=None, bminutes=None, bseconds=None,
              btstart=None, btend=None, weekdays_off=None, *args, **kwargs):
     self.holidays = holidays
     if self.holidays is None:
         self.holidays = getattr(relativedelta, 'holidays', ())
     self.weekdays_off = weekdays_off
     if self.weekdays_off is None:
         self.weekdays_off = getattr(relativedelta, 'weekdays_off', (5, 6))
     self.btstart = btstart
     if self.btstart is None:
         self.btstart = getattr(relativedelta, 'btstart', time(9))
     self.btend = btend
     if self.btend is None:
         self.btend = getattr(relativedelta, 'btend', time(17))
     if dt1 and dt2:
         # Convert to datetime objects
         dt1 = parse(dt1)
         dt2 = parse(dt2)
         if isinstance(dt1, date) and not isinstance(dt1, datetime):
             dt1 = datetime.combine(dt1, datetime.min.time())
         elif isinstance(dt1, time):
             dt1 = datetime.combine(datetime.now(), dt1)
         if isinstance(dt2, date) and not isinstance(dt2, datetime):
             dt2 = datetime.combine(dt2, datetime.min.time())
         elif isinstance(dt2, time):
             dt2 = datetime.combine(datetime.now(), dt2)
         # Call super init before setting self.bdays to avoid base __radd__
         # from calling child __add__ and creating infinite loop
         rd.__init__(self, dt1, dt2, *args, **kwargs)
         c = defaultdict(int)
         d1 = max(dt1, dt2)
         d2 = min(dt1, dt2)
         if d1.weekday() in self.weekdays_off or d1 in self.holidays:
             c['bdays'] += 1
         for attr in ('bhours', 'bminutes', 'bseconds'):
             while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                 d2 += rd(**{attr[1:]: +1})
                 if d2.time() >= self.btstart and d2.time() < self.btend:
                     c[attr] += 1
         while d1 > d2:
             d2 += rd(days=+1)
             if d2.weekday() not in self.weekdays_off and d2 not in self.holidays:
                 c['bdays'] += 1
         self.bdays = c['bdays']
         self.bhours = c['bhours']
         self.bminutes = c['bminutes']
         self.bseconds = c['bseconds']
         if dt2 > dt1:
             self.bdays *= -1
             self.bhours *= -1
             self.bminutes *= -1
             self.bseconds *= -1
     else:
         self.bdays = bdays
         self.bhours = bhours
         self.bminutes = bminutes
         self.bseconds = bseconds
         bd = rd(datetime.combine(datetime.now(), self.btend),
                 datetime.combine(datetime.now(), self.btstart))
         if isinstance(self.bdays, float):
             self.bhours = self.bhours or 0
             self.bhours += (self.bdays % 1) * \
                            (bd.hours + bd.minutes / 60 +
                             bd.seconds / 60 / 60)
             self.bdays = int(math.floor(self.bdays))
             if self.bdays == 0:
                 self.bdays = None
         if isinstance(self.bhours, float):
             self.bminutes = self.bminutes or 0
             self.bminutes += (self.bhours % 1) * 60
             self.bhours = int(math.floor(self.bhours))
             if self.bhours == 0:
                 self.bhours = None
         if isinstance(self.bminutes, float):
             self.bseconds = self.bseconds or 0
             self.bseconds += int((self.bminutes % 1) * 60)
             self.bminutes = int(math.floor(self.bminutes))
             if self.bminutes == 0:
                 self.bminutes = None
         rd.__init__(self, dt1, dt2, *args, **kwargs)
Exemplo n.º 20
0
 def __new__(self, *args, **kwargs):
     if len(args) == 1:
         args = parse(args[0]).timetuple()[3:6]
     return basetime.__new__(self, *args, **kwargs)
Exemplo n.º 21
0
def isbday(dt, holidays=None):
    if holidays is None:
        holidays = getattr(isbday, 'holidays', ())
    dt = parse(dt)
    return not (dt.weekday() in (5, 6) or dt in holidays)
Exemplo n.º 22
0
 def __init__(self, dt1=None, dt2=None, bdays=None,
              bhours=None, bminutes=None, bseconds=None,
              holidays=None, workdays=None, btstart=None, btend=None,
              *args, **kwargs):
     self.holidays = holidays
     if self.holidays is None:
         self.holidays = bdateutil.HOLIDAYS
     self.workdays = workdays
     if self.workdays is None:
         self.workdays = bdateutil.WORKDAYS
     if not self.workdays or self.workdays[0] not in range(7):
         raise ValueError("workdays must contain integers 0-6")
     self.btstart = btstart
     if self.btstart is None:
         self.btstart = bdateutil.BTSTART
     self.btend = btend
     if self.btend is None:
         self.btend = bdateutil.BTEND
     if not isinstance(self.btstart, time):
         raise TypeError("btstart must be of type time")
     if not isinstance(self.btend, time):
         raise TypeError("btend must be of type time")
     if self.btstart >= self.btend:
         raise ValueError("btend must be greater than btstart")
     if dt1 and dt2:
         # Convert to datetime objects
         dt1 = parse(dt1)
         dt2 = parse(dt2)
         if isinstance(dt1, date) and not isinstance(dt1, datetime):
             dt1 = datetime.combine(dt1, datetime.min.time())
         elif isinstance(dt1, time):
             dt1 = datetime.combine(datetime.now(), dt1)
         if isinstance(dt2, date) and not isinstance(dt2, datetime):
             dt2 = datetime.combine(dt2, datetime.min.time())
         elif isinstance(dt2, time):
             dt2 = datetime.combine(datetime.now(), dt2)
         # Call super init before setting self.bdays to avoid base __radd__
         # from calling child __add__ and creating infinite loop
         rd.__init__(self, dt1, dt2, *args, **kwargs)
         c = defaultdict(int)
         d1 = max(dt1, dt2)
         d2 = min(dt1, dt2)
         if d1.weekday() not in self.workdays or d1 in self.holidays:
             c['bdays'] += 1
         for attr in ('bhours', 'bminutes', 'bseconds'):
             while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                 d2 += rd(**{attr[1:]: +1})
                 if d2.time() >= self.btstart and d2.time() < self.btend:
                     c[attr] += 1
         while d1 > d2:
             d2 += rd(days=+1)
             if d2.weekday() in self.workdays and d2 not in self.holidays:
                 c['bdays'] += 1
         self.bdays = c['bdays']
         self.bhours = c['bhours']
         self.bminutes = c['bminutes']
         self.bseconds = c['bseconds']
         if dt2 > dt1:
             self.bdays *= -1
             self.bhours *= -1
             self.bminutes *= -1
             self.bseconds *= -1
     else:
         self.bdays = bdays
         self.bhours = bhours
         self.bminutes = bminutes
         self.bseconds = bseconds
         bd = rd(datetime.combine(datetime.now(), self.btend),
                 datetime.combine(datetime.now(), self.btstart))
         if isinstance(self.bdays, float):
             self.bhours = self.bhours or 0
             self.bhours += (self.bdays % 1) * \
                            (bd.hours + bd.minutes / 60 +
                             bd.seconds / 60 / 60)
             self.bdays = int(math.floor(self.bdays))
             if self.bdays == 0:
                 self.bdays = None
         if isinstance(self.bhours, float):
             self.bminutes = self.bminutes or 0
             self.bminutes += (self.bhours % 1) * 60
             self.bhours = int(math.floor(self.bhours))
             if self.bhours == 0:
                 self.bhours = None
         if isinstance(self.bminutes, float):
             self.bseconds = self.bseconds or 0
             self.bseconds += int((self.bminutes % 1) * 60)
             self.bminutes = int(math.floor(self.bminutes))
             if self.bminutes == 0:
                 self.bminutes = None
         rd.__init__(self, dt1, dt2, *args, **kwargs)
Exemplo n.º 23
0
 def __init__(self, dt1=None, dt2=None, bdays=None, holidays=None,
              bhours=None, bminutes=None, bseconds=None,
              btstart=None, btend=None, *args, **kwargs):
     self.holidays = holidays
     if self.holidays is None:
         self.holidays = getattr(relativedelta, 'holidays', ())
     self.btstart = btstart
     if self.btstart is None:
         self.btstart = getattr(relativedelta, 'btstart', time(9))
     self.btend = btend
     if self.btend is None:
         self.btend = getattr(relativedelta, 'btend', time(17))
     if dt1 and dt2:
         # Convert to datetime objects
         dt1 = parse(dt1)
         dt2 = parse(dt2)
         if isinstance(dt1, date) and not isinstance(dt1, datetime):
             dt1 = datetime.combine(dt1, datetime.min.time())
         elif isinstance(dt1, time):
             dt1 = datetime.combine(datetime.now(), dt1)
         if isinstance(dt2, date) and not isinstance(dt2, datetime):
             dt2 = datetime.combine(dt2, datetime.min.time())
         elif isinstance(dt2, time):
             dt2 = datetime.combine(datetime.now(), dt2)
         # Call super init before setting self.bdays to avoid base __radd__
         # from calling child __add__ and creating infinite loop
         rd.__init__(self, dt1, dt2, *args, **kwargs)
         c = defaultdict(int)
         d1 = max(dt1, dt2)
         d2 = min(dt1, dt2)
         if d1.weekday() in (5, 6) or d1 in self.holidays:
             c['bdays'] += 1
         for attr in ('bhours', 'bminutes', 'bseconds'):
             while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                 d2 += rd(**{attr[1:]: +1})
                 if d2.time() >= self.btstart and d2.time() < self.btend:
                     c[attr] += 1
         while d1 > d2:
             d2 += rd(days=+1)
             if d2.weekday() not in (5, 6) and d2 not in self.holidays:
                 c['bdays'] += 1
         self.bdays = c['bdays']
         self.bhours = c['bhours']
         self.bminutes = c['bminutes']
         self.bseconds = c['bseconds']
         if dt2 > dt1:
             self.bdays *= -1
             self.bhours *= -1
             self.bminutes *= -1
             self.bseconds *= -1
     else:
         self.bdays = bdays
         self.bhours = bhours
         self.bminutes = bminutes
         self.bseconds = bseconds
         bd = rd(datetime.combine(datetime.now(), self.btend),
                 datetime.combine(datetime.now(), self.btstart))
         if isinstance(self.bdays, float):
             self.bhours = self.bhours or 0
             self.bhours += (self.bdays % 1) * \
                            (bd.hours + bd.minutes / 60 +
                             bd.seconds / 60 / 60)
             self.bdays = int(math.floor(self.bdays))
             if self.bdays == 0:
                 self.bdays = None
         if isinstance(self.bhours, float):
             self.bminutes = self.bminutes or 0
             self.bminutes += (self.bhours % 1) * 60
             self.bhours = int(math.floor(self.bhours))
             if self.bhours == 0:
                 self.bhours = None
         if isinstance(self.bminutes, float):
             self.bseconds = self.bseconds or 0
             self.bseconds += int((self.bminutes % 1) * 60)
             self.bminutes = int(math.floor(self.bminutes))
             if self.bminutes == 0:
                 self.bminutes = None
         rd.__init__(self, dt1, dt2, *args, **kwargs)
Exemplo n.º 24
0
 def __new__(self, *args, **kwargs):
     if len(args) == 1:
         args = parse(args[0]).timetuple()[3:6]
     return basetime.__new__(self, *args, **kwargs)
Exemplo n.º 25
0
def isbday(dt, holidays=()):
    dt = parse(dt)
    return not (dt.weekday() in (5, 6) or dt in holidays)
Exemplo n.º 26
0
    def __init__(self,
                 dt1=None,
                 dt2=None,
                 bdays=None,
                 bhours=None,
                 bminutes=None,
                 bseconds=None,
                 holidays=None,
                 workdays=None,
                 btstart=None,
                 btend=None,
                 *args,
                 **kwargs):
        self.holidays = holidays
        if self.holidays is None:
            self.holidays = bdateutil.HOLIDAYS
        self.workdays = workdays
        if self.workdays is None:
            self.workdays = bdateutil.WORKDAYS
        if not self.workdays or self.workdays[0] not in range(7):
            raise ValueError("workdays must contain integers 0-6")
        self.btstart = btstart
        if self.btstart is None:
            self.btstart = bdateutil.BTSTART
        self.btend = btend
        if self.btend is None:
            self.btend = bdateutil.BTEND
        if not isinstance(self.btstart, time):
            raise TypeError("btstart must be of type time")
        if not isinstance(self.btend, time):
            raise TypeError("btend must be of type time")
        if self.btstart >= self.btend:
            raise ValueError("btend must be greater than btstart")
        if dt1 and dt2:
            # Convert to datetime objects
            dt1 = parse(dt1)
            dt2 = parse(dt2)
            if isinstance(dt1, date) and not isinstance(dt1, datetime):
                dt1 = datetime.combine(dt1, datetime.min.time())
            elif isinstance(dt1, time):
                dt1 = datetime.combine(datetime.now(), dt1)
            if isinstance(dt2, date) and not isinstance(dt2, datetime):
                dt2 = datetime.combine(dt2, datetime.min.time())
            elif isinstance(dt2, time):
                dt2 = datetime.combine(datetime.now(), dt2)
            # Call super init before setting self.bdays to avoid base __radd__
            # from calling child __add__ and creating infinite loop
            rd.__init__(self, dt1, dt2, *args, **kwargs)
            c = defaultdict(int)
            d1 = max(dt1, dt2)
            d2 = min(dt1, dt2)
            while d2.weekday() not in self.workdays or d2 not in self.holidays:
                d2 += rd(days=+1)
            for attr in ('bhours', 'bminutes', 'bseconds'):
                while getattr(d1, attr[1:-1]) != getattr(d2, attr[1:-1]):
                    d2 += rd(**{attr[1:]: +1})
                    if d2.weekday() not in self.workdays \
                            or d2 not in self.holidays:
                        d2 += rd(days=+1)
                    if d2.time() >= self.btstart and d2.time() < self.btend:
                        c[attr] += 1
            while d1 >= d2:
                if d2.weekday() in self.workdays and d2 not in self.holidays:
                    c['bdays'] += 1
                d2 += rd(days=+1)

            self.bdays = c['bdays']
            self.bhours = c['bhours']
            self.bminutes = c['bminutes']
            self.bseconds = c['bseconds']
            if dt2 > dt1:
                self.bdays *= -1
                self.bhours *= -1
                self.bminutes *= -1
                self.bseconds *= -1
        else:
            self.bdays = bdays
            self.bhours = bhours
            self.bminutes = bminutes
            self.bseconds = bseconds
            bd = rd(datetime.combine(datetime.now(), self.btend),
                    datetime.combine(datetime.now(), self.btstart))
            if isinstance(self.bdays, float):
                self.bhours = self.bhours or 0
                self.bhours += (self.bdays % 1) * \
                               (bd.hours + bd.minutes / 60 +
                                bd.seconds / 60 / 60)
                self.bdays = int(math.floor(self.bdays))
                if self.bdays == 0:
                    self.bdays = None
            if isinstance(self.bhours, float):
                self.bminutes = self.bminutes or 0
                self.bminutes += (self.bhours % 1) * 60
                self.bhours = int(math.floor(self.bhours))
                if self.bhours == 0:
                    self.bhours = None
            if isinstance(self.bminutes, float):
                self.bseconds = self.bseconds or 0
                self.bseconds += int((self.bminutes % 1) * 60)
                self.bminutes = int(math.floor(self.bminutes))
                if self.bminutes == 0:
                    self.bminutes = None
            rd.__init__(self, dt1, dt2, *args, **kwargs)
Exemplo n.º 27
0
def isbday(dt, holidays=None):
    if holidays is None:
        holidays = HOLIDAYS
    dt = parse(dt)
    return dt.weekday() in WORKDAYS and dt not in holidays