コード例 #1
0
 /**
  * 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);
 }
コード例 #2
0
 /**
  * (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;
 }
コード例 #3
0
 /**
  * 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);
 }
コード例 #4
0
 public function getData()
 {
     $data = parent::getData();
     return $this->dataConverter->convert($data);
 }
コード例 #5
0
 /**
  * @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);
 }
コード例 #6
0
ファイル: Formatter.php プロジェクト: fruition-sciences/phpfw
 /**
  * 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);
 }
コード例 #7
0
ファイル: ResultSet.php プロジェクト: fruition-sciences/phpfw
 public function getTime($key)
 {
     return DataConverter::parseTime($this->map[$key]);
 }
コード例 #8
0
ファイル: Request.php プロジェクト: fruition-sciences/phpfw
 /**
  * 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;
     }
 }