/** * Get a date (unix timestamp) from the given map. * * Currently, parses the value associated with the key. * Parses the date using the IntlDateFormatter SHORT format, which accepts * year as either 2 or 4 digits. * * TODO: The map should contain 2 fields: * - $key : keeps the formatted date * - $key__time: keeps the formatted time * * @param $map * @param $key * @return unix timestamp */ public function getDate($map, $key) { $value = $this->getValue($map, $key); if (!isset($value) || $value === "" || $value === false) { return null; } $dataConverter = new DataConverter($this->timezoneName, $this->locale); return $dataConverter->parseDate($value); }
/** * (non-PHPdoc) * @see Constraint::doValidate() */ public function doValidate($ctx) { $value = $ctx->getRequest()->getString($this->getName(), ''); // Validate only if there is a value if (!$value) { return true; } $converter = DataConverter::getInstance(); if (!$converter->parseDate($value)) { $msg = sprintf(Application::getTranslator()->_('The field %1$s must be a valid date'), $this->getLabel()); $this->addFieldError($ctx, $this->getName(), $msg); return false; } return true; }
/** * Parse the given formatted date according to the timezone set in this * DataConverter object. * If $pattern is given, parsing will be strictly by this pattern. * Otherwise, will try various localized patterns. Currently tries datetime * and then date. * * @param String $formattedDate formatted date (or date & time) * @param String $pattern Optional pattern to use when formatting or parsing. * Patterns are documented at http://userguide.icu-project.org/formatparse/datetime * @return long unix timestamp */ public function parseDate($formattedDate, $pattern = null) { if (!$formattedDate) { return null; } if ($pattern) { return $this->parseDateByPattern($formattedDate, $pattern); } // First try parsing as datetime $pattern = DataConverter::getDatePattern($this->locale, true, true); $timestamp = $this->parseDateByPattern($formattedDate, $pattern); if (is_numeric($timestamp)) { return $timestamp; } // If parsing failed, try parsing as date only $pattern = DataConverter::getDatePattern($this->locale, true, false); return $this->parseDateByPattern($formattedDate, $pattern); }
public function getData() { $data = parent::getData(); return $this->dataConverter->convert($data); }
/** * @covers DataConverter::getDatePattern * @expectedException IllegalArgumentException * @expectedExceptionMessage Date format must contain date or/and time */ public function testGetDatePatternException() { $actual = \DataConverter::getDatePattern("Europe/Paris", false, false); $expected = "h:mm a"; $this->assertEquals($expected, $actual); }
/** * Format the given timestamp as time. * * @param long $timestamp * @param boolean $showSeconds whether seconds should be included. * @return String formatted time, of the given timestamp in the * timezone set for this Formatter object. */ public function time($timestamp, $showSeconds = false) { if ($timestamp === null) { return ''; } $pattern = DataConverter::getDatePattern($this->getLocaleName(), false, true, $showSeconds); $fmt = new IntlDateFormatter($this->getLocaleName(), null, null, $this->timezone, null, $pattern); return $fmt->format((int) $timestamp); }
public function getTime($key) { return DataConverter::parseTime($this->map[$key]); }
/** * Get the date value associated with the given key. * * @param String $key the key * @param timestamp $defaultValue value to return in case the key doesn't exist in the request. * @param String $timezone If not passed, the user timezone will be used. * @return timestamp Unix timestamp - the number of seconds since January 1 1970 00:00:00 GMT */ public function getDate($key, $defaultValue = self::UNDEFINED, $timezone = null) { try { $str = $this->getString($key); $converter = DataConverter::getInstance(); if ($timezone && $converter->getTimeZoneName() != $timezone) { $converter = new DataConverter($timezone); } return $converter->parseDate($str); } catch (UndefinedKeyException $e) { if ($defaultValue != self::UNDEFINED) { return $defaultValue; } throw $e; } }