Returns a datetime.timedelta of business hours between d1 and d2,
        based on the length of the businessday
550323
def businesstime_hours(self, d1, d2):
        
        open_hours = self.open_hours.seconds / 3600
        btd = self.businesstimedelta(d1, d2)
        btd_hours = btd.seconds / 3600
        return datetime.timedelta(hours=(btd.days * open_hours + btd_hours))
647884
public static String formatDateDifference(Date d1, Date d2) {
		long[] td = DateHelper.getTimeDifference(d1, d2);
		if (td[0] > 0) {
			return td[0] + " day(s), " + td[1] + " hour(s)";
		}
		if (td[1] > 0) {
			return td[1] + " hour(s), " + td[2] + " minute(s)";
		}
		if (td[2] > 0) {
			return td[2] + " minute(s), " + td[3] + " second(s)";
		}
		return td[3] + " second(s)";
	}
1359962
def iterweekdays(self, d1, d2):
        
        for dt in self.iterdays(d1, d2):
            if not self.isweekend(dt):
                yield dt
657155
def subtract_days(self, days: int) -> datetime:
        
        self.value = self.value - relativedelta(days=days)
        return self.value
69507
def _datetime(self):
        
        if 'dt' not in self._cache.keys():
            self._cache['dt'] = self.MJD_T0 + timedelta(days=self._d, seconds=self._s)
        return self._cache['dt']
410365
def session(df, start='17:00', end='16:00'):
    
    if df.empty:
        return df

    # get start/end/now as decimals
    int_start = list(map(int, start.split(':')))
    int_start = (int_start[0] + int_start[1] - 1 / 100) - 0.0001
    int_end = list(map(int, end.split(':')))
    int_end = int_end[0] + int_end[1] / 100
    int_now = (df[-1:].index.hour[0] + (df[:1].index.minute[0]) / 100)

    # same-dat session?
    is_same_day = int_end > int_start

    # set pointers
    curr = prev = df[-1:].index[0].strftime('%Y-%m-%d')

    # globex/forex session
    if not is_same_day:
        prev = (datetime.strptime(curr, '%Y-%m-%d') -
                timedelta(1)).strftime('%Y-%m-%d')

    # slice
    if int_now >= int_start:
        df = df[df.index >= curr + ' ' + start]
    else:
        df = df[df.index >= prev + ' ' + start]

    return df.copy()
1309725
function isDayBetween(d, d1, d2) {
  var date = clone(d);
  date.setHours(0, 0, 0, 0);
  return isDayAfter(date, d1) && isDayBefore(date, d2) || isDayAfter(date, d2) && isDayBefore(date, d1);
}
553490
def timedelta_div(first: datetime.timedelta,
                  second: datetime.timedelta) -> Optional[float]:
    
    first_seconds = timedelta_seconds(first)
    second_seconds = timedelta_seconds(second)

    if not second_seconds:
        return None

    return first_seconds / second_seconds
1292416
def get_t_factor(t1, t2):
    
    t_factor = None
    if t1 is not None and t2 is not None and t1 != t2:  
        dt = t2 - t1
        year = timedelta(days=365.25)
        t_factor = abs(dt.total_seconds() / year.total_seconds()) 
    return t_factor
268505
def date2doy(time: Union[str, datetime.datetime]) -> Tuple[int, int]:
    

    T = np.atleast_1d(time)

    year = np.empty(T.size, dtype=int)
    doy = np.empty_like(year)

    for i, t in enumerate(T):
        yd = str(datetime2yeardoy(t)[0])

        year[i] = int(yd[:4])
        doy[i] = int(yd[4:])

    assert ((0 < doy) & (doy < 366)).all(), 'day of year must be 0 < doy < 366'

    return doy, year
612914
function isDayAfter(d1, d2) {
  var day1 = clone(d1).setHours(0, 0, 0, 0);
  var day2 = clone(d2).setHours(0, 0, 0, 0);
  return day1 > day2;
}
92667
def next_bday(self):
        
        if self.n >= 0:
            nb_offset = 1
        else:
            nb_offset = -1
        if self._prefix.startswith('C'):
            # CustomBusinessHour
            return CustomBusinessDay(n=nb_offset,
                                     weekmask=self.weekmask,
                                     holidays=self.holidays,
                                     calendar=self.calendar)
        else:
            return BusinessDay(n=nb_offset)
788093
def subtract_business_days(date, delta)
      date = roll_backward(date)
      delta.times do
        begin
          date -= day_interval_for(date)
        end until business_day?(date)
      end
      date
    end
1071700
def round_to_quarter(start_time, end_time):
    
    # We don't care about the date (only about the time) but Python
    # can substract only datetime objects, not time ones
    today = datetime.date.today()
    start_date = datetime.datetime.combine(today, start_time)
    end_date = datetime.datetime.combine(today, end_time)

    difference_minutes = (end_date - start_date).seconds / 60
    remainder = difference_minutes % 15
    # Round up
    difference_minutes += 15 - remainder if remainder > 0 else 0

    return (
        start_date + datetime.timedelta(minutes=difference_minutes)
    ).time()
1112997
def datetime_range(start_dt, end_dt, unit,
                   n_units=1, inclusive_end=False):
    """"""

    def done(a, b, inclusive_end):
        if inclusive_end:
            return a <= b
        else:
            return a < b

    current = start_dt
    while done(current, end_dt, inclusive_end):
        yield current
        current += datetime.timedelta(**{unit: n_units})
1359391
def timedelta2period(duration):
    """"""
    seconds = duration.seconds
    minutes = (seconds % 3600) // 60
    seconds = (seconds % 60)
    return '{0:0>2}:{1:0>2}'.format(minutes, seconds)
856124
def delta(d1, d2, opt='d'):
    
    delta = 0

    if opt == 'm':
        while True:
            mdays = monthrange(d1.year, d1.month)[1]
            d1 += timedelta(days=mdays)
            if d1 <= d2:
                delta += 1
            else:
                break
    else:
        delta = (d2 - d1).days

    return delta