Changeset 9853
- Timestamp:
- 06/25/08 13:11:49 (2 months ago)
- Files:
-
- branches/1.1/lib/i18n/sfI18N.class.php (modified) (4 diffs)
- branches/1.1/test/unit/i18n/sfI18NTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/i18n/sfI18N.class.php
r9128 r9853 82 82 } 83 83 84 85 84 /** 86 85 * Returns the initialization options … … 259 258 260 259 /** 261 * Returns a timestamp from a date formatted with a given culture.262 * 263 * @param string $date The formatted date as string260 * 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 264 263 * @param string $culture The culture 265 264 * 266 265 * @return integer The timestamp 267 266 */ 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); 273 273 } 274 274 … … 320 320 321 321 /** 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 /** 322 366 * Returns true if messages are stored in a file. 323 367 * … … 356 400 $this->setMessageSource($this->configuration->getI18NDirs($event['module'])); 357 401 } 402 358 403 } branches/1.1/test/unit/i18n/sfI18NTest.php
r9178 r9853 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test(2 2, new lime_output_color());13 $t = new lime_test(28, new lime_output_color()); 14 14 15 15 class ProjectConfiguration extends sfProjectConfiguration … … 80 80 $i18n = new sfI18N($configuration, $cache, array('culture' => 'fr')); 81 81 $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'); 82 83 $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'); 83 85 $t->is($i18n->getTimestampForCulture('not a date'), null, '->getTimestampForCulture() returns the day, month and year for a data formatted in the current culture'); 84 86 … … 89 91 $t->is($i18n->getDateForCulture('10/15/2005', 'en_US'), array('15', '10', '2005'), '->getDateForCulture() can take a culture as its second argument'); 90 92 $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');