Development

Changeset 9760

You must first sign up to be able to contribute.

Changeset 9760

Show
Ignore:
Timestamp:
06/23/08 04:29:29 (5 months ago)
Author:
dwhittle
Message:

dwhittle: tweaked sfI18n to better handle time (relates to #2896)

Files:

Legend:

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

    r9133 r9760  
    265265   * @return integer The timestamp 
    266266   */ 
    267   public function getTimestampForCulture($date, $culture = null) 
    268   { 
    269     list($d, $m, $y) = $this->getDateForCulture($date, is_null($culture) ? $this->culture : $culture); 
    270  
    271     return is_null($d) ? null : mktime(0, 0, 0, $m, $d, $y); 
     267  public function getTimestampForCulture($dateTime, $culture = null) 
     268  { 
     269    list($day, $month, $year) = $this->getDateForCulture($dateTime, is_null($culture) ? $this->culture : $culture); 
     270    list($hour, $minute) = $this->getTimeForCulture($dateTime, is_null($culture) ? $this->culture : $culture); 
     271 
     272    return is_null($day) ? null : mktime($hour, $minute, 0, $month, $day, $year); 
    272273  } 
    273274 
     
    319320 
    320321  /** 
     322   * Returns the hour, minute from a date formatted with a given culture. 
     323   * 
     324   * @param  string  $date    The formatted date as string 
     325   * @param  string  $culture The culture 
     326   * 
     327   * @return array   An array with the hour and minute 
     328   */ 
     329  public function getTimeForCulture($time, $culture) 
     330  { 
     331    if (!$time) return 0; 
     332 
     333    $culture = is_null($culture) ? $this->culture : $culture; 
     334 
     335    $timeFormatInfo = @sfDateTimeFormatInfo::getInstance($culture); 
     336    $timeFormat = $timeFormatInfo->getShortTimePattern(); 
     337 
     338    // We construct the regexp based on time format 
     339    $timeRegexp = preg_replace(array('/[^hm:]+/i', '/[hm]+/i'), array('', '(\d+)'), $timeFormat); 
     340 
     341    // We parse time format to see where things are (h, m) 
     342    $a = array( 
     343      'h' => strpos($timeFormat, 'H') !== false ? strpos($timeFormat, 'H') : strpos($timeFormat, 'h'), 
     344      'm' => strpos($timeFormat, 'm') 
     345    ); 
     346    $tmp = array_flip($a); 
     347    ksort($tmp); 
     348    $i = 0; 
     349    $c = array(); 
     350    foreach ($tmp as $value) $c[++$i] = $value; 
     351    $timePositions = array_flip($c); 
     352 
     353    // We find all elements 
     354    if (preg_match("~$timeRegexp~", $time, $matches)) 
     355    { 
     356      // We get matching timestamp 
     357      return array($matches[$timePositions['h']], $matches[$timePositions['m']]); 
     358    } 
     359    else 
     360    { 
     361      return null; 
     362    } 
     363  } 
     364 
     365  /** 
    321366   * Returns true if messages are stored in a file. 
    322367   * 
     
    355400    $this->setMessageSource($this->configuration->getI18NDirs($event['module'])); 
    356401  } 
     402 
    357403}