[Locale] refactored hour manipulation

This commit is contained in:
Eriksen Costa 2011-02-27 18:14:02 -03:00
parent 348dd7ba24
commit e78c6c2b1a
6 changed files with 71 additions and 33 deletions

View File

@ -181,36 +181,15 @@ class FullTransformer
$minute = $datetime['minute'];
$second = $datetime['second'];
$marker = $datetime['marker'];
$hourType = $datetime['hourType'];
$hourInstance = $datetime['hourInstance'];
// If month is false, return immediately
if (false === $month) {
return false;
}
// If the AM/PM marker is AM or null, the hour is 12 (1-12) and the capture was 'h' or 'hh', the hour is 0
if ('1201' === $hourType && 'PM' !== $marker && 12 === $hour) {
$hour = 0;
}
// If PM and hour is not 12 (1-12), sum 12 hour
if ('1201' === $hourType && 'PM' === $marker && 12 !== $hour) {
$hour = $hour + 12;
}
// If PM, sum 12 hours when 12 hour (0-11)
if ('1200' === $hourType && 'PM' === $marker) {
$hour = $hour + 12;
}
// If 24 hours (0-23 or 1-24) and marker is set, hour is 0
if (('2400' === $hourType || '2401' === $hourType) && null !== $marker) {
$hour = 0;
}
// If 24 hours (1-24) and hour is 24, hour is 0
if ('2401' === $hourType && 24 === $hour) {
$hour = 0;
if ($hourInstance instanceof HourTransformer) {
$hour = $hourInstance->getMktimeHour($hour, $marker);
}
// Set the timezone
@ -235,7 +214,7 @@ class FullTransformer
'minute' => isset($datetime['minute']) ? $datetime['minute'] : 0,
'second' => isset($datetime['second']) ? $datetime['second'] : 0,
'marker' => isset($datetime['marker']) ? $datetime['marker'] : null,
'hourType' => isset($datetime['hourType']) ? $datetime['hourType'] : null,
'hourInstance' => isset($datetime['hourInstance']) ? $datetime['hourInstance'] : null,
);
}
}

View File

@ -16,7 +16,7 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour1200Transformer extends Transformer
class Hour1200Transformer extends HourTransformer
{
public function format(\DateTime $dateTime, $length)
{
@ -25,6 +25,15 @@ class Hour1200Transformer extends Transformer
return $this->padLeft($hourOfDay, $length);
}
public function getMktimeHour($hour, $marker = null)
{
if ('PM' === $marker) {
$hour += 12;
}
return $hour;
}
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
@ -34,7 +43,7 @@ class Hour1200Transformer extends Transformer
{
return array(
'hour' => (int) $matched,
'hourType' => '1200'
'hourInstance' => $this
);
}
}

View File

@ -16,13 +16,25 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour1201Transformer extends Transformer
class Hour1201Transformer extends HourTransformer
{
public function format(\DateTime $dateTime, $length)
{
return $this->padLeft($dateTime->format('g'), $length);
}
public function getMktimeHour($hour, $marker = null)
{
if ('PM' !== $marker && 12 === $hour) {
$hour = 0;
} elseif ('PM' === $marker && 12 !== $hour) {
// If PM and hour is not 12 (1-12), sum 12 hour
$hour = $hour + 12;
}
return $hour;
}
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
@ -32,7 +44,7 @@ class Hour1201Transformer extends Transformer
{
return array(
'hour' => (int) $matched,
'hourType' => '1201'
'hourInstance' => $this
);
}
}

View File

@ -16,13 +16,22 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour2400Transformer extends Transformer
class Hour2400Transformer extends HourTransformer
{
public function format(\DateTime $dateTime, $length)
{
return $this->padLeft($dateTime->format('G'), $length);
}
public function getMktimeHour($hour, $marker = null)
{
if (null !== $marker) {
$hour = 0;
}
return $hour;
}
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
@ -32,7 +41,7 @@ class Hour2400Transformer extends Transformer
{
return array(
'hour' => (int) $matched,
'hourType' => '2400'
'hourInstance' => $this
);
}
}

View File

@ -16,7 +16,7 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class Hour2401Transformer extends Transformer
class Hour2401Transformer extends HourTransformer
{
public function format(\DateTime $dateTime, $length)
{
@ -25,6 +25,15 @@ class Hour2401Transformer extends Transformer
return $this->padLeft($hourOfDay, $length);
}
public function getMktimeHour($hour, $marker = null)
{
if (null !== $marker || 24 === $hour) {
$hour = 0;
}
return $hour;
}
public function getReverseMatchingRegExp($length)
{
return '\d{1,2}';
@ -34,7 +43,7 @@ class Hour2401Transformer extends Transformer
{
return array(
'hour' => (int) $matched,
'hourType' => '2401'
'hourInstance' => $this
);
}
}

View File

@ -0,0 +1,20 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Locale\Stub\DateFormat;
/**
* Base class for hour transformers.
*/
abstract class HourTransformer extends Transformer
{
abstract public function getMktimeHour($hour, $marker = null);
}