<commit_before><?php

namespace X509\Certificate;

use ASN1\Element;
use ASN1\Type\Primitive\GeneralizedTime;
use ASN1\Type\Primitive\UTCTime;
use ASN1\Type\TimeType;


/**
 * Implements <i>Time</i> ASN.1 type.
 *
 * @link https://tools.ietf.org/html/rfc5280#section-4.1
 */
class Time
{
	/**
	 * Datetime
	 *
	 * @var \DateTimeImmutable $_dt
	 */
	protected $_dt;
	
	/**
	 * Time ASN.1 type tag
	 *
	 * @var int $_type
	 */
	protected $_type;
	
	/**
	 * Constructor
	 *
	 * @param \DateTimeImmutable $dt
	 */
	public function __construct(\DateTimeImmutable $dt) {
		$this->_dt = $dt;
		$this->_type = self::_determineType($dt);
	}
	
	/**
	 * Initialize from ASN.1.
	 *
	 * @param TimeType $el
	 * @return self
	 */
	public static function fromASN1(TimeType $el) {
		$obj = new self($el->dateTime());
		$obj->_type = $el->tag();
		return $obj;
	}
	
	/**
	 * Initialize from date string.
	 *
	 * @param string|null $time
	 * @param string|null $tz
	 * @return self
	 */
	public static function fromString($time, $tz = null) {
		$timezone = isset($tz) ? new \DateTimeZone($tz) : null;
		$dt = new \DateTimeImmutable($time, $timezone);
		return new self($dt);
	}
	
	/**
	 * Get datetime.
	 *
	 * @return \DateTimeImmutable
	 */
	public function dateTime() {
		return $this->_dt;
	}
	
	/**
	 * Generate ASN.1.
	 *
	 * @throws \UnexpectedValueException
	 * @return TimeType
	 */
	public function toASN1() {
		$dt = $this->_dt;
		switch ($this->_type) {
		case Element::TYPE_UTC_TIME:
			return new UTCTime($dt);
		case Element::TYPE_GENERALIZED_TIME:
			// GeneralizedTime must not contain fractional seconds
			// (rfc5280 4.1.2.5.2)
			if ($dt->format("u") != 0) {<commit_msg>Add date helper trait<commit_after><?php

namespace X509\Certificate;

use ASN1\Element;
use ASN1\Type\Primitive\GeneralizedTime;
use ASN1\Type\Primitive\UTCTime;
use ASN1\Type\TimeType;
use X509\Feature\DateTimeHelper;


/**
 * Implements <i>Time</i> ASN.1 type.
 *
 * @link https://tools.ietf.org/html/rfc5280#section-4.1
 */
class Time
{
	use DateTimeHelper;
	
	/**
	 * Datetime
	 *
	 * @var \DateTimeImmutable $_dt
	 */
	protected $_dt;
	
	/**
	 * Time ASN.1 type tag
	 *
	 * @var int $_type
	 */
	protected $_type;
	
	/**
	 * Constructor
	 *
	 * @param \DateTimeImmutable $dt
	 */
	public function __construct(\DateTimeImmutable $dt) {
		$this->_dt = $dt;
		$this->_type = self::_determineType($dt);
	}
	
	/**
	 * Initialize from ASN.1.
	 *
	 * @param TimeType $el
	 * @return self
	 */
	public static function fromASN1(TimeType $el) {
		$obj = new self($el->dateTime());
		$obj->_type = $el->tag();
		return $obj;
	}
	
	/**
	 * Initialize from date string.
	 *
	 * @param string|null $time
	 * @param string|null $tz
	 * @return self
	 */
	public static function fromString($time, $tz = null) {
		return new self(self::_createDateTime($time, $tz));
	}
	
	/**
	 * Get datetime.
	 *
	 * @return \DateTimeImmutable
	 */
	public function dateTime() {
		return $this->_dt;
	}
	
	/**
	 * Generate ASN.1.
	 *
	 * @throws \UnexpectedValueException
	 * @return TimeType
	 */
	public function toASN1() {
		$dt = $this->_dt;
		switch ($this->_type) {
		case Element::TYPE_UTC_TIME:
			return new UTCTime($dt);
		case Element::TYPE_GENERALIZED_TIME:
			// GeneralizedTime must not contain fractional seconds
			// (rfc5280 4.1.2.5.2)
			if ($dt->format("u") != 0) {
{'fraction_non_alphanumeric': 0.16356542617046818, 'fraction_numerical': 0.018307322929171668, 'mean_word_length': 3.06959706959707, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5499157', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1187, 'n_words': 271}