[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/i18n/src/datetime/ -> lmbLocaleDate.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/core/src/exception/lmbException.class.php');
  10  lmb_require('limb/datetime/src/lmbDate.class.php');
  11  
  12  /**

  13   * class lmbLocaleDate.

  14   *

  15   * @package i18n

  16   * @version $Id: lmbLocaleDate.class.php 5945 2007-06-06 08:31:43Z pachanga $

  17   */
  18  class lmbLocaleDate extends lmbDate
  19  {
  20    /**

  21     *  Formats the date in the given format according to locale settings, much like

  22     *  strftime().  Most strftime() attributes are supported.

  23     *

  24     *  %a    abbreviated weekday name (Sun, Mon, Tue)

  25     *  %A    full weekday name (Sunday, Monday, Tuesday)

  26     *  %b    abbreviated month name (Jan, Feb, Mar)

  27     *  %B    full month name (January, February, March)

  28     *  %C    century number (the year divided by 100 and truncated to an integer, range 00 to 99)

  29     *  %d    day of month (range 00 to 31)

  30     *  %D    same as "%m/%d/%y"

  31     *  %e    day of month, single digit (range 0 to 31)

  32     *  %E    number of days since unspecified epoch

  33     *  %H    hour as decimal number (00 to 23)

  34     *  %I    hour as decimal number on 12-hour clock (01 to 12)

  35     *  %j    day of year (range 001 to 366)

  36     *  %m    month as decimal number (range 01 to 12)

  37     *  %M    minute as a decimal number (00 to 59)

  38     *  %n    newline character (\n)

  39     *  %O    dst-corrected timezone offset expressed as "+/-HH:MM"

  40     *  %o    raw timezone offset expressed as "+/-HH:MM"

  41     *  %p    either 'am' or 'pm' depending on the time

  42     *  %P    either 'AM' or 'PM' depending on the time

  43     *  %r    time in am/pm notation, same as "%I:%M:%S %p"

  44     *  %R    time in 24-hour notation, same as "%H:%M"

  45     *  %S    seconds as a decimal number (00 to 59)

  46     *  %t    tab character (\t)

  47     *  %T    current time, same as "%H:%M:%S"

  48     *  %w    weekday as decimal (0 = Sunday)

  49     *  %U    week number of current year, first sunday as first week

  50     *  %y    year as decimal (range 00 to 99)

  51     *  %Y    year as decimal including century (range 0000 to 9999)

  52     *  %%    literal '%'

  53     */
  54    function localeStrftime($format, $locale=null)
  55    {
  56      $output = '';
  57  
  58      for($strpos=0; $strpos < strlen($format); $strpos++)
  59      {
  60        $char = substr($format, $strpos, 1);
  61        if($char != '%')
  62        {
  63          $output .= $char;
  64          continue;
  65        }
  66  
  67        $nextchar = substr($format, $strpos + 1, 1);
  68        switch($nextchar)
  69        {
  70          case 'a':
  71              $this->_ensureLocale($locale);
  72              $output .= $locale->getDayName($this->getPhpDayOfWeek(), true);
  73              break;
  74          case 'A':
  75              $this->_ensureLocale($locale);
  76              $output .= $locale->getDayName($this->getPhpDayOfWeek());
  77              break;
  78          case 'b':
  79              $this->_ensureLocale($locale);
  80              $output .= $locale->getMonthName($this->getMonth() - 1, true);
  81              break;
  82          case 'B':
  83              $this->_ensureLocale($locale);
  84              $output .= $locale->getMonthName($this->getMonth() - 1);
  85              break;
  86          case 'p':
  87              $this->_ensureLocale($locale);
  88              $output .= $locale->getMeridiemName($this->getHour());
  89              break;
  90          case 'P':
  91              $this->_ensureLocale($locale);
  92              $output .= $locale->getMeridiemName($this->getHour(), true);
  93              break;
  94          case 'C':
  95              $output .= sprintf("%02d", intval($this->getYear()/100));
  96              break;
  97          case 'd':
  98              $output .= sprintf("%02d", $this->getDay());
  99              break;
 100          case 'D':
 101              $output .= sprintf("%02d/%02d/%02d", $this->getMonth(), $this->getDay(), substr($this->getYear(), 2));
 102              break;
 103          case 'e':
 104              $output .= $this->getDay();
 105              break;
 106          case 'E':
 107              $output .= $this->getDateDays();
 108              break;
 109          case 'H':
 110              $output .= sprintf("%02d", $this->getHour());
 111              break;
 112          case 'I':
 113              $hour = ($this->getHour() + 1) > 12 ? $this->getHour() - 12 : $this->getHour();
 114              $output .= sprintf("%02d", $hour==0 ? 12 : $hour);
 115              break;
 116          case 'j':
 117              $output .= sprintf("%03d", $this->getDayOfYear());
 118              break;
 119          case 'm':
 120              $output .= sprintf("%02d",$this->getMonth());
 121              break;
 122          case 'M':
 123              $output .= sprintf("%02d",$this->getMinute());
 124              break;
 125          case 'n':
 126              $output .= "\n";
 127              break;
 128          case 'O':
 129              $offms = $this->getTimeZone()->getOffset($this);
 130              $direction = $offms >= 0 ? '+' : '-';
 131              $offmins = abs($offms) / 1000 / 60;
 132              $hours = $offmins / 60;
 133              $minutes = $offmins % 60;
 134              $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
 135              break;
 136          case 'o':
 137              $offms = $this->getTimeZone()->getRawOffset($this);
 138              $direction = $offms >= 0 ? '+' : '-';
 139              $offmins = abs($offms) / 1000 / 60;
 140              $hours = $offmins / 60;
 141              $minutes = $offmins % 60;
 142              $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
 143              break;
 144          case 'r':
 145              $hour = ($this->getHour() + 1) > 12 ? $this->getHour() - 12 : $this->getHour();
 146              $output .= sprintf("%02d:%02d:%02d %s", $hour==0 ?  12 : $hour, $this->getMinute(), $this->getSecond(), $this->getHour() >= 12 ? "PM" : "AM");
 147              break;
 148          case 'R':
 149              $output .= sprintf("%02d:%02d", $this->getHour(), $this->getMinute());
 150              break;
 151          case 'S':
 152              $output .= sprintf("%02d", $this->getSecond());
 153              break;
 154          case 't':
 155              $output .= "\t";
 156              break;
 157          case 'T':
 158              $output .= sprintf("%02d:%02d:%02d", $this->getHour(), $this->getMinute(), $this->getSecond());
 159              break;
 160          case 'w':
 161              $output .= $this->getPhpDayOfWeek();
 162              break;
 163          case 'U':
 164              $output .= $this->getWeekOfYear();
 165              break;
 166          case 'y':
 167              $output .= substr($this->getYear() . '', 2);
 168              break;
 169          case 'Y':
 170              $output .= sprintf("%04d", $this->getYear());
 171              break;
 172          case 'Z':
 173              $output .= $this->getTimeZone()->isInDaylightTime() ? $this->getTimeZone()->getDSTShortName() : $this->getTimeZone()->getShortName();
 174              break;
 175          case '%':
 176              $output .= '%';
 177              break;
 178          default:
 179              $output .= $char . $nextchar;
 180        }
 181        $strpos++;
 182      }
 183      return $output;
 184    }
 185  
 186    /**

 187    *  Tries to guess time values in time string $time_string formatted with $fmt

 188    *  Returns an array('hour','minute','second','month','day','year')

 189    *  At this moment only most common tags are supported.

 190    */
 191    static function parseTimeString($locale, $time_string, $fmt)
 192    {
 193      $hour = 0;
 194      $minute = 0;
 195      $second = 0;
 196      $month = 0;
 197      $day = 0;
 198      $year = 0;
 199  
 200      if(!($time_array = self :: explodeTimeStringByFormat($time_string, $fmt)))
 201        return -1;
 202  
 203      foreach($time_array as $time_char => $value)
 204      {
 205        switch($time_char)
 206        {
 207          case '%p':
 208          case '%P':
 209            if(strtolower($value) == $locale->getPmName())
 210              $hour += 12;
 211          break;
 212  
 213          case '%I':
 214          case '%H':
 215            $hour = (int)$value;
 216          break;
 217  
 218          case '%M':
 219            $minute = (int)$value;
 220          break;
 221  
 222          case '%S':
 223            $second = (int)$value;
 224          break;
 225  
 226          case '%m':
 227            $month = (int)$value;
 228          break;
 229  
 230          case '%b':
 231          case '%h':
 232            if(($index = array_search($value, $locale->getMonthNames(true))) !== false)
 233            {
 234              if($index !== false)
 235                $month = $index + 1;
 236            }
 237          break;
 238  
 239          case '%B':
 240            if(($index = array_search($value, $locale->getMonthNames())) !== false)
 241            {
 242              if($index !== false)
 243                $month = $index + 1;
 244            }
 245          break;
 246  
 247          case '%d':
 248            $day = (int)$value;
 249          break;
 250  
 251          case '%Y':
 252            $year = (int)$value;
 253          break;
 254          case '%y':
 255            if($value < 40)
 256              $year = 2000 + $value;
 257            else
 258              $year = 1900 + $value;
 259          break;
 260  
 261          case '%T':
 262            if ($regs = explode(':', $value))
 263            {
 264              $hour   = (int)$regs[1];
 265              $minute = (int)$regs[2];
 266              $second = (int)$regs[3];
 267            }
 268          break;
 269  
 270          case '%D':
 271            if ($regs = explode('/', $value))
 272            {
 273              $hour   = (int)$regs[1];
 274              $minute = (int)$regs[2];
 275              $second = (int)$regs[3];
 276            }
 277          break;
 278  
 279          case '%R':
 280            if ($regs = explode(':', $value))
 281            {
 282              $hour   = (int)$regs[1];
 283              $minute = (int)$regs[2];
 284            }
 285          break;
 286        }
 287      }
 288      return array('hour' => $hour, 'minute' => $minute, 'second' => $second, 'month' => $month, 'day' => $day, 'year' => $year);
 289    }
 290  
 291    static function explodeTimeStringByFormat($time_string, $fmt)
 292    {
 293      $fmt_len = strlen($fmt);
 294      $time_string_len = strlen($time_string);
 295  
 296      $time_array = array();
 297  
 298      $fmt_pos = 0;
 299      $time_string_pos = 0;
 300  
 301      while(($fmt_pos = strpos($fmt, '%', $fmt_pos)) !== false)
 302      {
 303        $current_time_char = $fmt{++$fmt_pos};
 304  
 305        if(($fmt_pos+1) >= $fmt_len)
 306          $delimiter_pos = $time_string_len;
 307        elseif($time_string_pos <= $time_string_len)
 308        {
 309          $current_delimiter = $fmt{++$fmt_pos};
 310          $delimiter_pos = strpos($time_string, $current_delimiter, $time_string_pos);
 311          if($delimiter_pos === false)
 312            $delimiter_pos = $time_string_len;
 313        }
 314  
 315        $delimiter_len = $delimiter_pos - $time_string_pos;
 316  
 317        $value = substr($time_string, $time_string_pos, $delimiter_len);
 318  
 319        if(preg_match("/[-\/]/", $value))
 320          throw new lmbException("Wrong date format: $time_string does not matches $fmt format");
 321  
 322        $time_array['%' . $current_time_char] = $value;
 323  
 324        $time_string_pos += ($delimiter_len + 1);
 325      }
 326  
 327      return $time_array;
 328    }
 329  
 330    protected function _ensureLocale(&$locale)
 331    {
 332      if(!is_object($locale))
 333        $locale = lmbToolkit :: instance()->getLocaleObject();
 334    }
 335  
 336    static function localStringToDate($locale, $string, $format = null)
 337    {
 338      if(!$format)
 339        $format = $locale->getShortDateFormat();
 340  
 341      $arr = self :: parseTimeString($locale, $string, $format);
 342      return new lmbDate($arr['year'], $arr['month'], $arr['day'], $arr['hour'], $arr['minute'], $arr['second']);
 343    }
 344  
 345    static function isLocalStringValid($locale, $string, $format = null)
 346    {
 347      try
 348      {
 349        lmbLocaleDate :: localStringToDate($locale, $string, $format);
 350        return true;
 351      }
 352      catch(lmbException $e)
 353      {
 354        return false;
 355      }
 356    }
 357  }
 358  
 359  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7