[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Util/ -> TSimpleDateFormatter.php (source)

   1  <?php
   2  /**
   3   * TSimpleDateFormatter class file
   4   *
   5   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2006 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TSimpleDateFormatter.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Util
  11   */
  12  
  13  /**
  14   * TSimpleDateFormatter class.
  15   *
  16   * Formats and parses dates using the SimpleDateFormat pattern.
  17   * This pattern is compatible with the I18N and java's SimpleDateFormatter.
  18   * <code>
  19   * Pattern |      Description
  20   * ----------------------------------------------------
  21   * d       | Day of month 1 to 31, no padding
  22   * dd      | Day of monath 01 to 31, zero leading
  23   * M       | Month digit 1 to 12, no padding
  24   * MM      | Month digit 01 to 12, zero leading
  25   * yy      | 2 year digit, e.g., 96, 05
  26   * yyyy    | 4 year digit, e.g., 2005
  27   * ----------------------------------------------------
  28   * </code>
  29   *
  30   * Usage example, to format a date
  31   * <code>
  32   * $formatter = new TSimpleDateFormatter("dd/MM/yyy");
  33   * echo $formatter->format(time());
  34   * </code>
  35   *
  36   * To parse the date string into a date timestamp.
  37   * <code>
  38   * $formatter = new TSimpleDateFormatter("d-M-yyy");
  39   * echo $formatter->parse("24-6-2005");
  40   * </code>
  41   *
  42   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  43   * @version $Id: TSimpleDateFormatter.php 1397 2006-09-07 07:55:53Z wei $
  44   * @package System.Util
  45   * @since 3.0
  46   */
  47  class TSimpleDateFormatter
  48  {
  49      /**
  50       * Formatting pattern.
  51       * @var string
  52       */
  53      private $pattern;
  54  
  55      /**
  56       * Charset, default is 'UTF-8'
  57       * @var string
  58       */
  59      private $charset = 'UTF-8';
  60  
  61      /**
  62       * Constructor, create a new date time formatter.
  63       * @param string formatting pattern.
  64       * @param string pattern and value charset
  65       */
  66  	public function __construct($pattern, $charset='UTF-8')
  67      {
  68          $this->setPattern($pattern);
  69          $this->setCharset($charset);
  70      }
  71  
  72      /**
  73       * @return string formatting pattern.
  74       */
  75  	public function getPattern()
  76      {
  77          return $this->pattern;
  78      }
  79  
  80      /**
  81       * @param string formatting pattern.
  82       */
  83  	public function setPattern($pattern)
  84      {
  85          $this->pattern = $pattern;
  86      }
  87  
  88      /**
  89       * @return string formatting charset.
  90       */
  91  	public function getCharset()
  92      {
  93          return $this->charset;
  94      }
  95  
  96      /**
  97       * @param string formatting charset.
  98       */
  99  	public function setCharset($charset)
 100      {
 101          $this->charset = $charset;
 102      }
 103  
 104      /**
 105       * Format the date according to the pattern.
 106       * @param string|int the date to format, either integer or a string readable by strtotime.
 107       * @return string formatted date.
 108       */
 109  	public function format($value)
 110      {
 111          $date = $this->getDate($value);
 112          $bits['yyyy'] = $date['year'];
 113          $bits['yy'] = substr("{$date['year']}", -2);
 114  
 115          $bits['MM'] = str_pad("{$date['mon']}", 2, '0', STR_PAD_LEFT);
 116          $bits['M'] = $date['mon'];
 117  
 118          $bits['dd'] = str_pad("{$date['mday']}", 2, '0', STR_PAD_LEFT);
 119          $bits['d'] = $date['mday'];
 120  
 121          $pattern = preg_replace('/M{3,4}/', 'MM', $this->pattern);
 122          return str_replace(array_keys($bits), $bits, $pattern);
 123      }
 124  
 125  	public function getMonthPattern()
 126      {
 127          if(is_int(strpos($this->pattern, 'MMMM')))
 128              return 'MMMM';
 129          if(is_int(strpos($this->pattern, 'MMM')))
 130              return 'MMM';
 131          if(is_int(strpos($this->pattern, 'MM')))
 132              return 'MM';
 133          if(is_int(strpos($this->pattern, 'M')))
 134              return 'M';
 135          return false;
 136      }
 137  
 138  	public function getDayPattern()
 139      {
 140          if(is_int(strpos($this->pattern, 'dd')))
 141              return 'dd';
 142          if(is_int(strpos($this->pattern, 'd')))
 143              return 'd';
 144          return false;
 145      }
 146  
 147  	public function getYearPattern()
 148      {
 149          if(is_int(strpos($this->pattern, 'yyyy')))
 150              return 'yyyy';
 151          if(is_int(strpos($this->pattern, 'yy')))
 152              return 'yy';
 153          return false;
 154      }
 155  
 156  	public function getDayMonthYearOrdering()
 157      {
 158          $ordering = array();
 159          if(is_int($day= strpos($this->pattern, 'd')))
 160              $ordering['day'] = $day;
 161          if(is_int($month= strpos($this->pattern, 'M')))
 162              $ordering['month'] = $month;
 163          if(is_int($year= strpos($this->pattern, 'yy')))
 164              $ordering['year'] = $year;
 165          asort($ordering);
 166          return array_keys($ordering);
 167      }
 168  
 169      /**
 170       * Gets the time stamp from string or integer.
 171       * @param string|int date to parse
 172       * @return array date info array
 173       */
 174  	private function getDate($value)
 175      {
 176          if(!is_string($value))
 177          {
 178              $s = Prado::createComponent('System.Util.TDateTimeStamp');
 179              return $s->getDate($value);
 180          }
 181          $date = @strtotime($value);
 182          if($date < 0)
 183              throw new TInvalidDataValueException('invalid_date', $value);
 184          return @getdate($date);
 185      }
 186  
 187      /**
 188       * @return boolean true if the given value matches with the date pattern.
 189       */
 190  	public function isValidDate($value)
 191      {
 192          if(is_null($value))
 193              return false;
 194          else
 195              return !is_null($this->parse($value, false));
 196      }
 197  
 198      /**
 199       * Parse the string according to the pattern.
 200       * @param string|int date string or integer to parse
 201       * @return int date time stamp
 202       * @throws TInvalidDataValueException if date string is malformed.
 203       */
 204  	public function parse($value,$defaultToCurrentTime=true)
 205      {
 206          if(is_int($value) || is_float($value))
 207              return $value;
 208          else if(!is_string($value))
 209              throw new TInvalidDataValueException('date_to_parse_must_be_string', $value);
 210  
 211          if(empty($this->pattern)) return time();
 212  
 213          $date = time();
 214  
 215          if($this->length(trim($value)) < 1)
 216              return $defaultToCurrentTime ? $date : null;
 217  
 218          $pattern = $this->pattern;
 219  
 220          $i_val = 0;
 221          $i_format = 0;
 222          $pattern_length = $this->length($pattern);
 223          $c = '';
 224          $token='';
 225          $x=null; $y=null;
 226  
 227  
 228          if($defaultToCurrentTime)
 229          {
 230              $year = "{$date['year']}";
 231              $month = $date['mon'];
 232              $day = $date['mday'];
 233          }
 234          else
 235          {
 236              $year = null;
 237              $month = null;
 238              $day = null;
 239          }
 240  
 241          while ($i_format < $pattern_length)
 242          {
 243              $c = $this->charAt($pattern,$i_format);
 244              $token='';
 245              while ($this->charEqual($pattern, $i_format, $c)
 246                          && ($i_format < $pattern_length))
 247              {
 248                  $token .= $this->charAt($pattern, $i_format++);
 249              }
 250  
 251              if ($token=='yyyy' || $token=='yy' || $token=='y')
 252              {
 253                  if ($token=='yyyy') { $x=4;$y=4; }
 254                  if ($token=='yy')   { $x=2;$y=2; }
 255                  if ($token=='y')    { $x=2;$y=4; }
 256                  $year = $this->getInteger($value,$i_val,$x,$y);
 257                  if(is_null($year))
 258                      return null;
 259                      //throw new TInvalidDataValueException('Invalid year', $value);
 260                  $i_val += strlen($year);
 261                  if(strlen($year) == 2)
 262                  {
 263                      $iYear = intval($year);
 264                      if($iYear > 70)
 265                          $year = $iYear + 1900;
 266                      else
 267                          $year = $iYear + 2000;
 268                  }
 269                  $year = intval($year);
 270              }
 271              elseif($token=='MM' || $token=='M')
 272              {
 273                  $month=$this->getInteger($value,$i_val,
 274                                      $this->length($token),2);
 275                  $iMonth = intval($month);
 276                  if(is_null($month) || $iMonth < 1 || $iMonth > 12 )
 277                      return null;
 278                      //throw new TInvalidDataValueException('Invalid month', $value);
 279                  $i_val += strlen($month);
 280                  $month = $iMonth;
 281              }
 282              elseif ($token=='dd' || $token=='d')
 283              {
 284                  $day = $this->getInteger($value,$i_val,
 285                                      $this->length($token), 2);
 286                  $iDay = intval($day);
 287                  if(is_null($day) || $iDay < 1 || $iDay >31)
 288                      return null;
 289                      //throw new TInvalidDataValueException('Invalid day', $value);
 290                  $i_val += strlen($day);
 291                  $day = $iDay;
 292              }
 293              else
 294              {
 295                  if($this->substring($value, $i_val, $this->length($token)) != $token)
 296                      return null;
 297                      //throw new TInvalidDataValueException("Subpattern '{$this->pattern}' mismatch", $value);
 298                  else
 299                      $i_val += $this->length($token);
 300              }
 301          }
 302          if ($i_val != $this->length($value))
 303              throw new TInvalidDataValueException("Pattern '{$this->pattern}' mismatch", $value);
 304          //var_dump('month is '.$month);
 305          if(!$defaultToCurrentTime && (is_null($month) || is_null($day) || is_null($year)))
 306              return null;
 307          else
 308          {
 309              $day = intval($day) <= 0 ? 1 : intval($day);
 310              $s = Prado::createComponent('System.Util.TDateTimeStamp');
 311              return $s->getTimeStamp(0, 0, 0, $month, $day, $year);
 312          }
 313      }
 314  
 315      /**
 316       * Calculate the length of a string, may be consider iconv_strlen?
 317       */
 318  	private function length($string)
 319      {
 320          //use iconv_strlen or just strlen?
 321          return strlen($string);
 322      }
 323  
 324      /**
 325       * Get the char at a position.
 326       */
 327  	private function charAt($string, $pos)
 328      {
 329          return $this->substring($string, $pos, 1);
 330      }
 331  
 332      /**
 333       * Gets a portion of a string, uses iconv_substr.
 334       */
 335  	private function substring($string, $start, $length)
 336      {
 337          return iconv_substr($string, $start, $length);
 338      }
 339  
 340      /**
 341       * Returns true if char at position equals a particular char.
 342       */
 343  	private function charEqual($string, $pos, $char)
 344      {
 345          return $this->charAt($string, $pos) == $char;
 346      }
 347  
 348      /**
 349       * Gets integer from part of a string, allows integers of any length.
 350       * @param string string to retrieve the integer from.
 351       * @param int starting position
 352       * @param int minimum integer length
 353       * @param int maximum integer length
 354       * @return string integer portition of the string, null otherwise
 355       */
 356  	private function getInteger($str,$i,$minlength,$maxlength)
 357      {
 358          //match for digits backwards
 359          for ($x = $maxlength; $x >= $minlength; $x--)
 360          {
 361              $token= $this->substring($str, $i,$x);
 362              if ($this->length($token) < $minlength)
 363                  return null;
 364              if (preg_match('/^\d+$/', $token))
 365                  return $token;
 366          }
 367          return null;
 368      }
 369  }
 370  
 371  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7