Converts duration to timedelta

    >>> duration_to_timedelta("10m")
    >>> datetime.timedelta(0, 600)
186781
def duration_to_timedelta(obj):
    
    matches = DURATION_PATTERN.search(obj)
    matches = matches.groupdict(default="0")
    matches = {k: int(v) for k, v in matches.items()}
    return timedelta(**matches)
783216
def to_timedelta(value):
    """"""
    if value is None:
        return None
    if isinstance(value, (six.integer_types, float)):
        return timedelta(microseconds=(float(value) / 10))
    match = _TIMESPAN_PATTERN.match(value)
    if match:
        if match.group(1) == "-":
            factor = -1
        else:
            factor = 1
        return factor * timedelta(
            days=int(match.group("d") or 0),
            hours=int(match.group("h")),
            minutes=int(match.group("m")),
            seconds=float(match.group("s")),
        )
    else:
        raise ValueError("Timespan value '{}' cannot be decoded".format(value))
808562
def ToTimedelta(self):
    """"""
    return timedelta(
        seconds=self.seconds, microseconds=_RoundTowardZero(
            self.nanos, _NANOS_PER_MICROSECOND))
1040973
def to_timedelta(value, strict=True):
    
    if isinstance(value, int):
        return timedelta(seconds=value)  # assuming it's seconds
    elif isinstance(value, timedelta):
        return value
    elif isinstance(value, str):
        hours, minutes, seconds = _parse(value, strict)
    elif isinstance(value, tuple):
        check_tuple(value, strict)
        hours, minutes, seconds = value
    else:
        raise TypeError(
            'Value %s (type %s) not supported' % (
                value, type(value).__name__
            )
        )
    return timedelta(hours=hours, minutes=minutes, seconds=seconds)
841638
def to_timedelta(self):
        """"""
        return timedelta(hours=self.hour, minutes=self.minute,
                seconds=self.second, microseconds=self.microsecond)
995752
def durations(self) -> List[datetime.timedelta]:
        
        return [x.duration() for x in self.intervals]
212064
def total_duration(self) -> datetime.timedelta:
        
        total = datetime.timedelta()
        for interval in self.intervals:
            total += interval.duration()
        return total
778602
def timedelta_seconds(value: datetime.timedelta) -> int:
    
    return SECONDS_PER_DAY * value.days + value.seconds
1187322
def _datetime_to_epoch(self, dt):
        """"""
        if dt:
            td = (dt - datetime.datetime.fromtimestamp(0, tzutc()))
            # don't use total_seconds(), that's only available in 2.7
            total_secs = int((td.microseconds +
                              (td.seconds + td.days * 24 * 3600) *
                              10**6) / 10**6)
            return total_secs
        else:
            return 0
578222
def to_unix_ms(dt: datetime) -> int:
    
    utcoffset = dt.utcoffset()
    ep = epoch if utcoffset is None else epoch_tz
    return as_int((dt - ep).total_seconds() * 1000)
839222
def elapsed(t0=0.0):
    
    now = time()
    dt = now - t0
    dt_sec = Decimal(str(dt)).quantize(Decimal('.0001'), rounding=ROUND_DOWN)
    if dt_sec <= 1:
        dt_str = str(dt_sec) + ' second'
    else:
        dt_str = str(dt_sec) + ' seconds'
    return now, dt_str
1231722
def cumulative_time_to(self,
                           when: datetime.datetime) -> datetime.timedelta:
        
        assert self.no_overlap, self._ONLY_FOR_NO_INTERVAL
        cumulative = datetime.timedelta()
        for interval in self.intervals:
            if interval.start >= when:
                break
            elif interval.end <= when:
                # complete interval precedes "when"
                cumulative += interval.duration()
            else:  # start < when < end
                cumulative += when - interval.start
        return cumulative
90730
def _get_minutes(self, duration):
        
        if isinstance(duration, datetime.datetime):
            from_now = (duration - datetime.datetime.now()).total_seconds()
            from_now = math.ceil(from_now / 60)

            if from_now > 0:
                return from_now

            return

        return duration
139188
def duration(self):
        

        if not self._loaded:
            return 0

        delta = datetime.datetime.now() - self._start_time
        total_secs = (delta.microseconds +
                      (delta.seconds + delta.days * 24 * 3600) *
                      10 ** 6) / 10 ** 6

        return max(0, int(round(total_secs / 60.0)))
1194007
def datetime_delta_to_ms(delta):
    
    delta_ms = delta.days * 24 * 60 * 60 * 1000
    delta_ms += delta.seconds * 1000
    delta_ms += delta.microseconds / 1000
    delta_ms = int(delta_ms)
    return delta_ms
1006483
def create_timedelta(timespec):
    """"""
    duration = timespec.split(':')
    seconds = int(duration[-1])
    minutes = 0
    hours = 0
    days = 0

    if len(duration) > 1:
        minutes = int(duration[-2])
    if len(duration) > 2:
        hours = int(duration[-3])
    if len(duration) > 3:
        days = int(duration[-4])

    return datetime.timedelta(days=days, seconds=seconds, minutes=minutes, hours=hours)
904569
def compile_timedelta(rule):
    
    if isinstance(rule.value, datetime.timedelta):
        return rule
    try:
        # Try numeric type
        return TimedeltaRule(datetime.timedelta(seconds = int(rule.value)))
    except:
        pass
    # Try RFC3339 timestamp string
    res = DURATION_RE.match(str(rule.value))
    if res is not None:
        days, hours, minutes, seconds = (int(n or 0) for n in res.group('days','hrs','mins','secs'))
        return TimedeltaRule(datetime.timedelta(days = days, hours = hours, minutes = minutes, seconds = seconds))
    raise ValueError("Wrong timedelta format '{}'".format(rule.value))