<commit_before><?php

/**
 * Copyright (c) dotBlue (http://dotblue.net)
 */

namespace DotBlue\Nette\Forms;

use DateTime;
use InvalidArgumentException;
use Nette;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Container;


class CalendarPicker extends BaseControl
{

	const DEFAULT_MASK_DATE = 'j. n. Y';
	const DEFAULT_MASK_DATETIME = 'j. n. Y H:i:s';

	public static $jsConverter = array(
		'j' => 'd',
		'd' => 'dd',
		'D' => 'D',
		'l' => 'DD',
		'z' => 'o',
		// '' => 'oo', unsupported in PHP
		'n' => 'm',
		'm' => 'mm',
		// 'M' => 'M',
		'F' => 'MM',
		'y' => 'yy',
		'Y' => 'yyyy',
		'U' => '@',
		// time
		'a' => 'p',
		'A' => 'P',
		// '' => 's', unsupported in PHP
		's' => 'ss',
		// '' => 'i', unsupported in PHP
		'i' => 'ii',
		'G' => 'h',
		'H' => 'hh',
		'g' => 'H',
		'h' => 'HH',
	); // compatible with http://www.malot.fr/bootstrap-datetimepicker/

	/** @var bool */
	private $useTime = FALSE;

	/** @var int */
	private $year;

	/** @var int */
	private $month;

	/** @var int */
	private $day;

	/** @var int */
	private $hour;

	/** @var int */
	private $minute;

	/** @var int */
	private $second;

	/** @var string */
	private $phpMask;

	/** @var string */
	private $jsMask;

	/** @var callable */
	private $parseDateCallback;

	/** @var callable */
	private $formatDateCallback;



	/**
	 * @param  string|NULL
	 */
	public function __construct($label = NULL)
	{
		parent::__construct($label);
		$this->setMask($this->useTime ? self::DEFAULT_MASK_DATETIME : self::DEFAULT_MASK_DATE);
		$this->addRule($this->validateDate);

		$this->parseDateCallback = array($this, 'parseDate');
		$this->formatDateCallback = array($this, 'formatDate');
	}



	/**
	 * Sets custom parser of date string.
	 *
	 * @param  callable
	 * @return CalendarPicker provides a fluent interface
	 */
	public function setParser($callback)
	{
		$this->parseDateCallback = $callback;
		return $this;
	}



	/**
	 * Sets custom formatter of date object.
	 *
	 * @param  callable
	 * @return CalendarPicker provides a fluent interface
	 */
	public function setFormatter($callback)
	{
		$this->formatDateCallback = $callback;
		return $this;
	}



	/**
	 * Adds UI for setting time of day.
	 * Don't forget to set correct mask if you don't use default one.
	 *
	 * @return CalendarPicker provides a fluent interface
	 */
	public function useTime()
	{
		$this->useTime = TRUE;
		if ($this->phpMask === self::DEFAULT_MASK_DATE) {
			$this->setMask(self::DEFAULT_MASK_DATETIME);
		}
		$this->addRule($this->validateTime);
		return $this;
	}



	/**
	 * Sets mask used by PHP for initial rendering.
	 *
	 * @param  string
	 * @return CalendarPicker provides a fluent interface
	 */
	public function setMask($phpMask)
	{
		$this->phpMask = $phpMask;
		$this->jsMask = $this->createJsMask($phpMask);
		return $this;
	}



	public function setValue($value)
	{
		if ($value) {
			if (is_string($value)) {
				$date = call_user_func($this->parseDateCallback, $this->phpMask, $value);
			} else {
				$date = Nette\DateTime::from($value);
			}

			if (!$date instanceof DateTime) {
				throw new InvalidArgumentException("Invalid input for calendar picker: '$value'");
			}

			$this->year = $date->format('Y');
			$this->month = $date->format('n');
			$this->day = $date->format('j');
			if ($this->useTime) {
				$this->day = $date->format('G');
				$this->day = (int) $date->format('i');
				$this->day = (int) $date->format('s');
			}
		} else {
			$this->year = $this->month = $this->day = NULL;
			if ($this->useTime) {
				$this->hour = $this->minute = $this->second = NULL;
			}
		}
	}
<commit_msg>Method setValue() correctly sets time<commit_after><?php

/**
 * Copyright (c) dotBlue (http://dotblue.net)
 */

namespace DotBlue\Nette\Forms;

use DateTime;
use InvalidArgumentException;
use Nette;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Container;


class CalendarPicker extends BaseControl
{

	const DEFAULT_MASK_DATE = 'j. n. Y';
	const DEFAULT_MASK_DATETIME = 'j. n. Y H:i:s';

	public static $jsConverter = array(
		'j' => 'd',
		'd' => 'dd',
		'D' => 'D',
		'l' => 'DD',
		'z' => 'o',
		// '' => 'oo', unsupported in PHP
		'n' => 'm',
		'm' => 'mm',
		// 'M' => 'M',
		'F' => 'MM',
		'y' => 'yy',
		'Y' => 'yyyy',
		'U' => '@',
		// time
		'a' => 'p',
		'A' => 'P',
		// '' => 's', unsupported in PHP
		's' => 'ss',
		// '' => 'i', unsupported in PHP
		'i' => 'ii',
		'G' => 'h',
		'H' => 'hh',
		'g' => 'H',
		'h' => 'HH',
	); // compatible with http://www.malot.fr/bootstrap-datetimepicker/

	/** @var bool */
	private $useTime = FALSE;

	/** @var int */
	private $year;

	/** @var int */
	private $month;

	/** @var int */
	private $day;

	/** @var int */
	private $hour;

	/** @var int */
	private $minute;

	/** @var int */
	private $second;

	/** @var string */
	private $phpMask;

	/** @var string */
	private $jsMask;

	/** @var callable */
	private $parseDateCallback;

	/** @var callable */
	private $formatDateCallback;



	/**
	 * @param  string|NULL
	 */
	public function __construct($label = NULL)
	{
		parent::__construct($label);
		$this->setMask($this->useTime ? self::DEFAULT_MASK_DATETIME : self::DEFAULT_MASK_DATE);
		$this->addRule($this->validateDate);

		$this->parseDateCallback = array($this, 'parseDate');
		$this->formatDateCallback = array($this, 'formatDate');
	}



	/**
	 * Sets custom parser of date string.
	 *
	 * @param  callable
	 * @return CalendarPicker provides a fluent interface
	 */
	public function setParser($callback)
	{
		$this->parseDateCallback = $callback;
		return $this;
	}



	/**
	 * Sets custom formatter of date object.
	 *
	 * @param  callable
	 * @return CalendarPicker provides a fluent interface
	 */
	public function setFormatter($callback)
	{
		$this->formatDateCallback = $callback;
		return $this;
	}



	/**
	 * Adds UI for setting time of day.
	 * Don't forget to set correct mask if you don't use default one.
	 *
	 * @return CalendarPicker provides a fluent interface
	 */
	public function useTime()
	{
		$this->useTime = TRUE;
		if ($this->phpMask === self::DEFAULT_MASK_DATE) {
			$this->setMask(self::DEFAULT_MASK_DATETIME);
		}
		$this->addRule($this->validateTime);
		return $this;
	}



	/**
	 * Sets mask used by PHP for initial rendering.
	 *
	 * @param  string
	 * @return CalendarPicker provides a fluent interface
	 */
	public function setMask($phpMask)
	{
		$this->phpMask = $phpMask;
		$this->jsMask = $this->createJsMask($phpMask);
		return $this;
	}



	public function setValue($value)
	{
		if ($value) {
			if (is_string($value)) {
				$date = call_user_func($this->parseDateCallback, $this->phpMask, $value);
			} else {
				$date = Nette\DateTime::from($value);
			}

			if (!$date instanceof DateTime) {
				throw new InvalidArgumentException("Invalid input for calendar picker: '$value'");
			}

			$this->year = $date->format('Y');
			$this->month = $date->format('n');
			$this->day = $date->format('j');
			if ($this->useTime) {
				$this->hour = $date->format('G');
				$this->minute = (int) $date->format('i');
				$this->second = (int) $date->format('s');
			}
		} else {
			$this->year = $this->month = $this->day = NULL;
			if ($this->useTime) {
				$this->hour = $this->minute = $this->second = NULL;
			}
		}
	}

{'fraction_non_alphanumeric': 0.19493985897967647, 'fraction_numerical': 0.0, 'mean_word_length': 3.1887666473653735, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '774848', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2538, 'n_words': 624}