<commit_before><?php
/**
 * Time-related utility methods.
 *
 * @package SimpleSAMLphp
 */

namespace SimpleSAML\Utils;


class Time
{

    /**
     * This function generates a timestamp on the form used by the SAML protocols.
     *
     * @param int $instant The time the timestamp should represent. Defaults to current time.
     *
     * @return string The timestamp.
     * @author <NAME>, UNINETT AS <<EMAIL>>
     */
    public static function generateTimestamp($instant = null)
    {
        if ($instant === null) {
            $instant = time();
        }
        return gmdate('Y-m-d\TH:i:s\Z', $instant);
    }


    /**
     * Initialize the timezone.
     *
     * This function should be called before any calls to date().
     *
     * @author <NAME>, UNINETT AS <<EMAIL>>
     */
    public static function initTimezone()
    {
        static $initialized = false;

        if ($initialized) {
            return;
        }

        $initialized = true;

        $globalConfig = \SimpleSAML_Configuration::getInstance();

        $timezone = $globalConfig->getString('timezone', null);
        if ($timezone !== null) {
            if (!date_default_timezone_set($timezone)) {
                throw new \SimpleSAML_Error_Exception('Invalid timezone set in the "timezone" option in config.php.');
            }
            return;
        }
        // we don't have a timezone configured

        /*
         * The date_default_timezone_get() function is likely to cause a warning.
         * Since we have a custom error handler which logs the errors with a backtrace,
         * this error will be logged even if we prefix the function call with '@'.
         * Instead we temporarily replace the error handler.
         */
        set_error_handler(function () {
            return true;
        });
        $serverTimezone = date_default_timezone_get();
        restore_error_handler();

        // set the timezone to the default
        date_default_timezone_set($serverTimezone);
    }


    /**
     * Interpret a ISO8601 duration value relative to a given timestamp. Please note no fractions are allowed, neither
     * durations specified in the formats PYYYYMMDDThhmmss nor P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss].
     *
     * @param string $duration The duration, as a string.
     * @param int    $timestamp The unix timestamp we should apply the duration to. Optional, default to the current
     *     time.
     *
     * @return int The new timestamp, after the duration is applied.
     * @throws \InvalidArgumentException If $duration is not a valid ISO 8601 duration or if the input parameters do
     *     not have the right data types.
     */
    public static function parseDuration($duration, $timestamp = null)
    {
        if (!(is_string($duration) && (is_int($timestamp) || is_null($timestamp)))) {
            throw new \InvalidArgumentException('Invalid input parameters');
        }<commit_msg>No need to disable the error handler momentarily when guessing the timezone from the OS. Mask errors instead.<commit_after><?php
/**
 * Time-related utility methods.
 *
 * @package SimpleSAMLphp
 */

namespace SimpleSAML\Utils;

use SimpleSAML\Logger;


class Time
{

    /**
     * This function generates a timestamp on the form used by the SAML protocols.
     *
     * @param int $instant The time the timestamp should represent. Defaults to current time.
     *
     * @return string The timestamp.
     * @author <NAME>, UNINETT AS <<EMAIL>>
     */
    public static function generateTimestamp($instant = null)
    {
        if ($instant === null) {
            $instant = time();
        }
        return gmdate('Y-m-d\TH:i:s\Z', $instant);
    }


    /**
     * Initialize the timezone.
     *
     * This function should be called before any calls to date().
     *
     * @author <NAME>, UNINETT AS <<EMAIL>>
     */
    public static function initTimezone()
    {
        static $initialized = false;

        if ($initialized) {
            return;
        }

        $initialized = true;

        $globalConfig = \SimpleSAML_Configuration::getInstance();

        $timezone = $globalConfig->getString('timezone', null);
        if ($timezone !== null) {
            if (!date_default_timezone_set($timezone)) {
                throw new \SimpleSAML_Error_Exception('Invalid timezone set in the "timezone" option in config.php.');
            }
            return;
        }
        // we don't have a timezone configured

        Logger::maskErrors(E_ALL);
        $serverTimezone = date_default_timezone_get();
        Logger::popErrorMask();

        // set the timezone to the default
        date_default_timezone_set($serverTimezone);
    }


    /**
     * Interpret a ISO8601 duration value relative to a given timestamp. Please note no fractions are allowed, neither
     * durations specified in the formats PYYYYMMDDThhmmss nor P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss].
     *
     * @param string $duration The duration, as a string.
     * @param int    $timestamp The unix timestamp we should apply the duration to. Optional, default to the current
     *     time.
     *
     * @return int The new timestamp, after the duration is applied.
     * @throws \InvalidArgumentException If $duration is not a valid ISO 8601 duration or if the input parameters do
     *     not have the right data types.
     */
    public static function parseDuration($duration, $timestamp = null)
    {
        if (!(is_string($duration) && (is_int($timestamp) || is_null($timestamp)))) {
            throw new \InvalidArgumentException('Invalid input parameters');
        }
{'fraction_non_alphanumeric': 0.10007122507122507, 'fraction_numerical': 0.002849002849002849, 'mean_word_length': 2.3454437164979156, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1528591', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1483, 'n_words': 516}