Development

Changeset 9853

You must first sign up to be able to contribute.

Changeset 9853

Show
Ignore:
Timestamp:
06/25/08 13:11:49 (2 months ago)
Author:
FabianLange
Message:

1.1: fixed I18N timestamp handling, using Dustins r9760 + added tests. closes #2896, #2570.

Files:

Legend:

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

    r9128 r9853  
    8282  } 
    8383 
    84  
    8584  /** 
    8685   * Returns the initialization options 
     
    259258 
    260259  /** 
    261    * Returns a timestamp from a date formatted with a given culture. 
    262    * 
    263    * @param  string  $date  The formatted date as string 
     260   * Returns a timestamp from a date with time formatted with a given culture. 
     261   * 
     262   * @param  string  $dateTime  The formatted date with time as string 
    264263   * @param  string  $culture The culture 
    265264   * 
    266265   * @return integer The timestamp 
    267266   */ 
    268   public function getTimestampForCulture($date, $culture = null) 
    269   { 
    270     list($d, $m, $y) = $this->getDateForCulture($date, is_null($culture) ? $this->culture : $culture); 
    271  
    272     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); 
    273273  } 
    274274 
     
    320320 
    321321  /** 
     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  /** 
    322366   * Returns true if messages are stored in a file. 
    323367   * 
     
    356400    $this->setMessageSource($this->configuration->getI18NDirs($event['module'])); 
    357401  } 
     402 
    358403} 
  • branches/1.1/test/unit/i18n/sfI18NTest.php

    r9178 r9853  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(22, new lime_output_color()); 
     13$t = new lime_test(28, new lime_output_color()); 
    1414 
    1515class ProjectConfiguration extends sfProjectConfiguration 
     
    8080$i18n = new sfI18N($configuration, $cache, array('culture' => 'fr')); 
    8181$t->is($i18n->getTimestampForCulture('15/10/2005'), mktime(0, 0, 0, '10', '15', '2005'), '->getTimestampForCulture() returns the timestamp for a data formatted in the current culture'); 
     82$t->is($i18n->getTimestampForCulture('15/10/2005 15:33'), mktime(15, 33, 0, '10', '15', '2005'), '->getTimestampForCulture() returns the timestamp for a data formatted in the current culture'); 
    8283$t->is($i18n->getTimestampForCulture('10/15/2005', 'en_US'), mktime(0, 0, 0, '10', '15', '2005'), '->getTimestampForCulture() can take a culture as its second argument'); 
     84$t->is($i18n->getTimestampForCulture('10/15/2005 15:33', 'en_US'), mktime(15, 33, 0, '10', '15', '2005'), '->getTimestampForCulture() can take a culture as its second argument'); 
    8385$t->is($i18n->getTimestampForCulture('not a date'), null, '->getTimestampForCulture() returns the day, month and year for a data formatted in the current culture'); 
    8486 
     
    8991$t->is($i18n->getDateForCulture('10/15/2005', 'en_US'), array('15', '10', '2005'), '->getDateForCulture() can take a culture as its second argument'); 
    9092$t->is($i18n->getDateForCulture(null), null, '->getDateForCulture() returns null in case of conversion problem'); 
    91 $t->is($i18n->getDateForCulture('not a date'), null, '->getDateForCulture() returns the day, month and year for a data formatted in the current culture'); 
     93$t->is($i18n->getDateForCulture('not a date'), null, '->getDateForCulture() returns null in case of conversion problem'); 
     94 
     95// ->getTimeForCulture() 
     96$t->diag('->getTimeForCulture()'); 
     97$i18n = new sfI18N($configuration, $cache, array('culture' => 'fr')); 
     98$t->is($i18n->getTimeForCulture('15:33'), array('15', '33'), '->getTimeForCulture() returns the hour and minuter for a time formatted in the current culture'); 
     99$t->is($i18n->getTimeForCulture('15:33', 'en_US'), array('15', '33'), '->getTimeForCulture() can take a culture as its second argument'); 
     100$t->is($i18n->getTimeForCulture(null), null, '->getTimeForCulture() returns null in case of conversion problem'); 
     101$t->is($i18n->getTimeForCulture('not a time'), null, '->getTimeForCulture() returns null in case of conversion problem');