Development

Changeset 10841

You must first sign up to be able to contribute.

Changeset 10841

Show
Ignore:
Timestamp:
08/13/08 14:51:18 (11 months ago)
Author:
noel
Message:

[1.0] fixed Hours & Minutes not parsed by sfI18N::getTimestampForCulture() (backported from r9853) (closes #2896)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0/lib/i18n/sfI18N.class.php

    r4340 r10841  
    157157  public static function getTimestampForCulture($date, $culture) 
    158158  { 
    159     list($d, $m, $y) = self::getDateForCulture($date, $culture); 
    160     return mktime(0, 0, 0, $m, $d, $y); 
     159    list($d, $m, $y)     = self::getDateForCulture($date, $culture); 
     160    list($hour, $minute) = self::getTimeForCulture($date, $culture); 
     161 
     162    return mktime($hour, $minute, 0, $m, $d, $y); 
    161163  } 
    162164 
     
    196198    } 
    197199  } 
     200 
     201  /** 
     202   * Returns the hour, minute from a date formatted with a given culture. 
     203   * 
     204   * @param  string  $date    The formatted date as string 
     205   * @param  string  $culture The culture 
     206   * 
     207   * @return array   An array with the hour and minute 
     208   */ 
     209  protected static function getTimeForCulture($time, $culture) 
     210  { 
     211    if (!$time) return 0; 
     212 
     213    $culture = is_null($culture) ? $this->culture : $culture; 
     214 
     215    $timeFormatInfo = @sfDateTimeFormatInfo::getInstance($culture); 
     216    $timeFormat = $timeFormatInfo->getShortTimePattern(); 
     217 
     218    // We construct the regexp based on time format 
     219    $timeRegexp = preg_replace(array('/[^hm:]+/i', '/[hm]+/i'), array('', '(\d+)'), $timeFormat); 
     220 
     221    // We parse time format to see where things are (h, m) 
     222    $a = array( 
     223      'h' => strpos($timeFormat, 'H') !== false ? strpos($timeFormat, 'H') : strpos($timeFormat, 'h'), 
     224      'm' => strpos($timeFormat, 'm') 
     225    ); 
     226    $tmp = array_flip($a); 
     227    ksort($tmp); 
     228    $i = 0; 
     229    $c = array(); 
     230 
     231    foreach ($tmp as $value) 
     232    { 
     233      $c[++$i] = $value; 
     234    } 
     235 
     236    $timePositions = array_flip($c); 
     237 
     238    // We find all elements 
     239    if (preg_match("~$timeRegexp~", $time, $matches)) 
     240    { 
     241      // We get matching timestamp 
     242      return array($matches[$timePositions['h']], $matches[$timePositions['m']]); 
     243    } 
     244    else 
     245    { 
     246      return null; 
     247    } 
     248  } 
     249 
    198250} 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.