<commit_before><?php
namespace asinfotrack\yii2\toolbox\helpers;

use Yii;
use yii\base\InvalidParamException;
use yii\helpers\FormatConverter;
use IntlDateFormatter;
use DateTime;
use DateTimeZone;

/**
 * Helper class for working with UNIX-timestamps
 * 
 * @author <NAME>, AS infotrack AG
 * @link http://www.asinfotrack.ch
 * @license MIT
 */
class Timestamp
{
	
	/**
	 * @var array holds the mapping for the intl date-formatter
	 */
	protected static $_dateFormats = [
        'short'  => 3, // IntlDateFormatter::SHORT,
        'medium' => 2, // IntlDateFormatter::MEDIUM,
        'long'   => 1, // IntlDateFormatter::LONG,
        'full'   => 0, // IntlDateFormatter::FULL,
    ];

	/**
	 * Returns the utc offset of the currently set timezone in php ini
	 * in seconds (-43200 through 50400)
	 *
	 * @return int offset in number of seconds
	 */
	public static function utcOffset()
	{
		return intval(date('Z'));
	}

	/**
	 * Gets today's timestamp without the time part (time will be
	 * set to 00:00:00). By default the stamp of the local time is
	 * returned.
	 *
	 * @param bool $localTime if set to true (default) the stamp for the local
	 * time is returned. If false, UTC is returned
	 * @return int timestamp
	 */
	public static function getTodayStampWithoutTime($localTime=true)
	{
		return $localTime ? mktime(0, 0, 0) : gmmktime(0, 0, 0);
	}
	
	/**
	 * Returns a corrected timestamp which consists only of the date
	 * part. Hours, minutes and seconds are set to zero.
	 * 
	 * @param int $stamp the timestamp to correct
	 * @return int the cleaned timestamp
	 */
	public static function removeTime($stamp)
	{
		return $stamp - ($stamp % 86400);
	}
	
	/**
	 * Checks whether or not a timestamp has a time part. The function returns true, if
	 * the time-part of a stamp differs from the values defined in params 2 to 4.
	 * 
	 * Default setting for no time part is 00:00:00.
	 * 
	 * @param integer $stamp the timestamp to check
	 * @param integer $noTimeHour the hour considered as no time part
	 * @param integer $noTimeMinute the minute considered as no time part
	 * @param integer $noTimeSecond the second considered as no time part
	 * @return boolean true if a time is set differing from the one defined in params 2 to 4
	 * @throws InvalidParamException if values are illegal
	 */
	public static function hasTime($stamp, $noTimeHour=0, $noTimeMinute=0, $noTimeSecond=0)
	{
		//assert no time values are ok
		if ($noTimeHour < 0 || $noTimeHour > 23 || $noTimeMinute < 0 || $noTimeMinute > 59 || $noTimeSecond < 0 || $noTimeSecond > 59) {
			throw new InvalidParamException('Wrong values for no time hour, minute or second received');
		}
		
		//get date parts and compare
		$d = getdate($stamp);
		return $d['hours'] != $noTimeHour || $d['minutes'] != $noTimeMinute || $d['seconds'] != $noTimeSecond;
	}
	
	/**
	 * Parses a date value into a UNIX-Timestamp
	 * 
	 * @param string $value string representing date
     * @param string $format the expected date format
	 * @param string $locale string the locale ID that is used to localize the date parsing.
     * This is only effective when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed.
     * If not set, the locale of the [[\yii\base\Application::formatter|formatter]] will be used.
     * See also [[\yii\i18n\Formatter::locale]].
     * @param string $timeZone the timezone to use for parsing date and time values.
     * This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php)
     * e.g. `UTC`, `Europe/Berlin` or `America/Chicago`.
     * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
     * If this property is not set, [[\yii\base\Application::timeZone]] will be used.
     * @return integer|boolean a UNIX timestamp or `false` on failure.
	 */
	public static function parseFromDate($value, $format, $locale=null, $timeZone='UTC')
	{
		//default values<commit_msg>BREAKING CHANGE in Timestamp-helper: now correctly implementing timezone handling and therefore renamed methods<commit_after><?php
namespace asinfotrack\yii2\toolbox\helpers;

use Yii;
use yii\base\InvalidParamException;
use yii\helpers\FormatConverter;
use IntlDateFormatter;
use DateTime;
use DateTimeZone;

/**
 * Helper class for working with UNIX-timestamps
 * 
 * @author <NAME>, AS infotrack AG
 * @link http://www.asinfotrack.ch
 * @license MIT
 */
class Timestamp
{

	/**
	 * Holds the name of the utc timezone
	 */
	const TIMEZONE_UTC = 'UTC';
	
	/**
	 * @var array holds the mapping for the intl date-formatter
	 */
	protected static $_dateFormats = [
        'short'  => 3, // IntlDateFormatter::SHORT,
        'medium' => 2, // IntlDateFormatter::MEDIUM,
        'long'   => 1, // IntlDateFormatter::LONG,
        'full'   => 0, // IntlDateFormatter::FULL,
    ];

	/**
	 * Returns the utc offset of the currently set timezone in yii config
	 * in seconds (-43200 through 50400)
	 *
	 * @param string $timezone optional timezone (if not specified current timezone is taken)
	 * @return int offset in number of seconds
	 */
	public static function utcOffset($timezone=null)
	{
		if ($timezone === null) {
			return intval(date('Z'));
		} else {
			$tzSpecified = new DateTimeZone($timezone);
			return $tzSpecified->getOffset(new DateTime('now', $tzSpecified));
		}
	}

	/**
	 * Converts a utc timestamp into local time timestamp by adding the utc
	 * offset. That way 00:02:30 in represented in UTC will be offset in a way,
	 * that the new timestamp represents the same time in the timezone specified.
	 *
	 * @param int $utcTimestamp the utc timestamp
	 * @param string $timezone the desired timezone (defaults to currently set timezone)
	 * @return int timestamp representing the same time in local time
	 */
	public static function convertUtcToLocalTime($utcTimestamp, $timezone=null)
	{
		return $utcTimestamp + static::utcOffset($timezone);
	}

	/**
	 * Gets the midnight timestamp (00:00:00) of today (default) or a day specifiable via
	 * params. If no timezone is specified, the currently set timezone via yii config will
	 * be taken
	 *
	 * @param string $timezone the desired timezone (defaults to currently set timezone)
	 * @param int $month the optional month
	 * @param int $day the optional day
	 * @param int $year the optional year
	 * @return int timestamp the midnight-timestamp in the desired timezone
	 */
	public static function getMidnightTimestamp($timezone=null, $month=null, $day=null, $year=null)
	{
		$month = $month === null ? date('m') : $month;
		$day = $day === null ? date('d') : $day;
		$year = $year === null ? date('Y') : $year;

		if (!checkdate($month, $day, $year)) {
			$msg = Yii::t('app', 'The date specified does not exist!');
			throw new InvalidParamException($msg);
		}

		$offset = static::utcOffset($timezone !== null ? $timezone : Yii::$app->timeZone);
		return gmmktime(0, 0, 0, $month, $day, $year) + $offset;
	}
	
	/**
	 * This method takes a timestamp and sets is time to midnight on the same day in the
	 * specified timezone.
	 * 
	 * @param int $timestamp the timestamp to set the time to midnight
	 * @param string $timezoneSource the timezone to calculate the day for
	 * @return int the midnight timestamp of the same timezone as timezone source
	 */
	public static function makeMidnight($timestamp, $timezoneSource=null)
	{
		$tzSource = new DateTimeZone($timezoneSource === null ? Yii::$app->timeZone : $timezoneSource);
		$dt = new DateTime();
		$dt->setTimezone($tzSource);
		$dt->setTimestamp($timestamp);
		$dt->setTime(0, 0, 0);

		return intval($dt->format('U'));
	}
	
	/**
	 * Checks whether or not a timestamp has a time part. The function returns true, if
	 * the time-part of a stamp differs from the values defined in params 2 to 4.
	 * 
	 * Default setting for no time part is 00:00:00.
	 * 
	 * @param integer $stamp the timestamp to check
	 * @param string $timezone the timezone the timestamp is in (defaults to UTC)
	 * @return boolean true if a time is set differing from the one defined in params 2 to 4
	 * @throws InvalidParamException if values are illegal
	 */
	public static function hasTime($stamp, $timezone=self::TIMEZONE_UTC)
	{
		return ($stamp + static::utcOffset($timezone)) % 86400 != 0;
	}
	
	/**
	 * Parses a date value into a UNIX-Timestamp
	 * 
	 * @param string $value string representing date
     * @param string $format the expected date format
	 * @param string $locale string the locale ID that is used to localize the date parsing.
     * This is only effective when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is installed.
     * If not set, the locale of the [[\yii\base\Application::formatter|formatter]] will be used.
     * See also [[\yii\i18n\Formatter::locale]].
     * @param string $timeZone the timezone to use for parsing date and time values.
     * This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php)
     * e.g. `UTC`, `Europe/Berlin` or `America/Chicago`.
     * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
     * If this property is not set, [[\yii\base\Application::timeZone]] will be used.
     * @return integer|boolean a UNIX timestamp or `false` on failure.
	 */
	public static function parseFromDate($value, $format, $locale=null, $timeZone='UTC')
	{
		//default values
{'fraction_non_alphanumeric': 0.11421346538826256, 'fraction_numerical': 0.01127383837319566, 'mean_word_length': 3.614487117160914, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '3354784', 'n_tokens_mistral': 3098, 'n_tokens_neox': 2830, 'n_words': 1069}