Format the supplied date interval as a retention period.

@param   \DateInterval   $interval
@return  string
548315
public static function format_retention_period(\DateInterval $interval) : string {
        // It is one or another.
        if ($interval->y) {
            $formattedtime = get_string('numyears', 'moodle', $interval->format('%y'));
        } else if ($interval->m) {
            $formattedtime = get_string('nummonths', 'moodle', $interval->format('%m'));
        } else if ($interval->d) {
            $formattedtime = get_string('numdays', 'moodle', $interval->format('%d'));
        } else {
            $formattedtime = get_string('retentionperiodzero', 'tool_dataprivacy');
        }

        return $formattedtime;
    }
1160610
protected function _getPeriodByDate(DateTime $Date, $interval = null) {
		$Range = new DateRange(clone $Date);
		return $Range->period($this->_getInterval($interval));
	}
1027827
final public static function intervalToSeconds($interval)
    {
        try {
            $start = new \DateTimeImmutable();
            $end = $start->add($interval);
            return $end->getTimestamp() - $start->getTimestamp();
        } catch (\Exception $e) {
            return 0;
        }

    }
1101729
public static function toSecondsUsingDays(\DateInterval $interval)
    {
        $seconds = (string) $interval->s;

        if ($interval->i) {
            $seconds = bcadd($seconds, bcmul($interval->i, self::SECONDS_MINUTE));
        }

        if ($interval->h) {
            $seconds = bcadd($seconds, bcmul($interval->h, self::SECONDS_HOUR));
        }

        if ((false !== $interval->days) && (0 <= $interval->days)) {
            $seconds = bcadd($seconds, bcmul($interval->days, self::SECONDS_DAY));
        } else {
            throw new InvalidArgumentException(
                'The "days" property is not set.'
            );
        }

        return $seconds;
    }
1229153
public function setExpiresIn(DateInterval $interval)
    {
        $now = new DateTimeImmutable();
        $expire = $now->add($interval);
        $this->setExpiresInSeconds($expire->getTimestamp() - $now->getTimestamp());
        return $this;
    }
492953
public function equals(DateInterval $interval)
	{
		$years   = $this->y == $interval->y;
		$months  = $this->m == $interval->m;
		$days    = $this->d == $interval->d;
		$hours   = $this->h == $interval->h;
		$minutes = $this->i == $interval->i;
		$seconds = $this->s == $interval->s;
		$invert  = $this->invert == $interval->invert;

		return $years && $months && $days && $hours && $minutes && $seconds && $invert;
	}
258422
public String interval(Date target) {
		double unit = 1000.0D;
		double dayUnit = DAY / unit;
		double hourUnit = HOUR / unit;
		double minUnit = MINUTE / unit;
		double interval = sinceMillis(target) / unit;
		IntervalDesc desc = theIntervalDesc.get();
		
		if (interval >= 0.0D) {
			if (interval / (12 * 30 * dayUnit) > 1.0D) {
				return asText(target);
			}
			if (interval / (30 * dayUnit) > 1.0D) {
				return String.format("%s%s", (int) (interval / (30 * dayUnit)), desc.getMonthAgo());
			}
			if (interval / (7 * dayUnit) > 1.0D) {
				return String.format("7%s", desc.getDayAgo());
			}
			if ((interval / (7 * dayUnit) <= 1.0D) && (interval / dayUnit >= 1.0D)) {
				return String.format("%s%s", (int) (interval / dayUnit), desc.getDayAgo());
			}
			if ((interval / dayUnit < 1.0D) && (interval / hourUnit >= 1.0D)) {
				return String.format("%s%s", (int) (interval / hourUnit), desc.getHourAgo());
			}
			if ((interval < hourUnit) && (interval >= minUnit)) {
				return String.format("%s%s", (int) (interval / minUnit), desc.getMinuteAgo());
			}
			
			return desc.getJustNow();
		}
		
		return asText(target);
	}
817359
protected static function intervalHasTime(DateInterval $interval)
    {
        // The array_key_exists and get_object_vars are used as a workaround to check microsecond support.
        // Both isset and property_exists will fail on PHP 7.0.14 - 7.0.21 due to the following bug:
        // https://bugs.php.net/bug.php?id=74852
        return $interval->h || $interval->i || $interval->s || array_key_exists('f', get_object_vars($interval)) && $interval->f;
    }
1256377
private function parseDateInterval($interval)
    {
        try {
            if (!$this->isInstanceOf($interval, 'DateInterval')) {
                $interval = new StringObject($interval);
                if ($interval->startsWith('P')) {
                    $interval = new \DateInterval($interval);
                } else {
                    $interval = \DateInterval::createFromDateString($interval);
                }
            }
        } catch (\Exception $e) {
            throw new DateTimeObjectException(DateTimeObjectException::MSG_INVALID_DATE_INTERVAL, [$interval]);
        }

        return $interval;
    }
1137789
private function abandonedIntervalToDate($date)
    {
        $dateInterval = "P".(String)self::VALID_RANGE_DAYS."D";
        $date->add(new \DateInterval($dateInterval));

        return $date->format('d/m/Y H:i:s');
    }
603809
public static function dateIntervalToIntervalSpec(DateInterval $di): string
    {
        $time_parts = ['h', 'i', 's', 'f'];
        $time_token_set = false;
        $interval_spec = 'P';

        foreach (array_keys(self::$tokens) as $token) {
            if (in_array($token, $time_parts) && $time_token_set === false && $di->$token > 0) {
                $interval_spec .= 'T';
                $time_token_set = true;
            }
            if ($di->$token > 0) {
                $token_string = $token;
                if (array_key_exists($token, self::$tokensSubstitution)) {
                    $token_string = self::$tokensSubstitution[$token];
                }
                $interval_spec .= $di->$token.strtoupper($token_string);
            }
        }

        return $interval_spec;
    }
62485
private function getIntervalUnits(\DateTime $date1, \DateTime $date2, string $intervalFormat)
    {
        $result = 0;
        try {
            $interval = new \DateInterval($intervalFormat);
            $periods  = new \DatePeriod($date1, $interval, $date2);

            $result = max(0, iterator_count($periods) - 1);
        } catch (\Exception $e) {
        }

        return $result;
    }
1211444
public function add(DateInterval $interval)
    {
        $sign = $interval->invert === 1 ? -1 : 1;

        if (self::wasCreatedFromDiff($interval)) {
            $this->dayz = $this->dayz + $interval->days * $sign;
        } else {
            $this->years = $this->years + $interval->y * $sign;
            $this->months = $this->months + $interval->m * $sign;
            $this->dayz = $this->dayz + $interval->d * $sign;
            $this->hours = $this->hours + $interval->h * $sign;
            $this->minutes = $this->minutes + $interval->i * $sign;
            $this->seconds = $this->seconds + $interval->s * $sign;
        }

        return $this;
    }
199672
public function setDateInterval($interval)
    {
        if (!$interval = CarbonInterval::make($interval)) {
            throw new InvalidArgumentException('Invalid interval.');
        }

        if ($interval->spec() === 'PT0S') {
            throw new InvalidArgumentException('Empty interval is not accepted.');
        }

        $this->dateInterval = $interval;

        $this->isDefaultInterval = false;

        $this->handleChangedParameters();

        return $this;
    }
905678
public function indexOf(Period $interval)
    {
        foreach ($this->intervals as $offset => $period) {
            if ($period->equals($interval)) {
                return $offset;
            }
        }

        return false;
    }
664940
private function getRandomChar(string $interval): string
    {
        $size = \strlen($interval) - 1;
        $int = \random_int(0, $size);

        return $interval[$int];
    }
714543
private function formatSimple(\DateInterval $diff)
    {
        $str = '';
        $pre = '';
        if ($diff->y > 0 || $diff->m > 0 || $diff->d == 1 || $diff->h == 1) {
            return $this->formatNormal($diff);
        } else if ($diff->d > 0) {
            $str = "few days";
        } else if ($diff->h > 0) {
            $str = "few hours";
        } else if ($diff->i < 2) {
            $str = "minute";
            $pre = "a ";
        } else if ($diff->i < 20) {
            $str = "15 minutes";
        } else if ($diff->i < 40) {
            $str = "half hour";
        } else if ($diff->i < 60) {
            $str = "hour";
            $pre = "an ";
        }

        return $this->translator->trans($diff->invert ? "next " . $str : $pre . $str . " ago");
    }