[Locale] refactored hour manipulation
This commit is contained in:
parent
348dd7ba24
commit
e78c6c2b1a
@ -181,36 +181,15 @@ class FullTransformer
|
|||||||
$minute = $datetime['minute'];
|
$minute = $datetime['minute'];
|
||||||
$second = $datetime['second'];
|
$second = $datetime['second'];
|
||||||
$marker = $datetime['marker'];
|
$marker = $datetime['marker'];
|
||||||
$hourType = $datetime['hourType'];
|
$hourInstance = $datetime['hourInstance'];
|
||||||
|
|
||||||
// If month is false, return immediately
|
// If month is false, return immediately
|
||||||
if (false === $month) {
|
if (false === $month) {
|
||||||
return false;
|
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 ($hourInstance instanceof HourTransformer) {
|
||||||
if ('1201' === $hourType && 'PM' !== $marker && 12 === $hour) {
|
$hour = $hourInstance->getMktimeHour($hour, $marker);
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the timezone
|
// Set the timezone
|
||||||
@ -235,7 +214,7 @@ class FullTransformer
|
|||||||
'minute' => isset($datetime['minute']) ? $datetime['minute'] : 0,
|
'minute' => isset($datetime['minute']) ? $datetime['minute'] : 0,
|
||||||
'second' => isset($datetime['second']) ? $datetime['second'] : 0,
|
'second' => isset($datetime['second']) ? $datetime['second'] : 0,
|
||||||
'marker' => isset($datetime['marker']) ? $datetime['marker'] : null,
|
'marker' => isset($datetime['marker']) ? $datetime['marker'] : null,
|
||||||
'hourType' => isset($datetime['hourType']) ? $datetime['hourType'] : null,
|
'hourInstance' => isset($datetime['hourInstance']) ? $datetime['hourInstance'] : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
|
|||||||
*
|
*
|
||||||
* @author Igor Wiedler <igor@wiedler.ch>
|
* @author Igor Wiedler <igor@wiedler.ch>
|
||||||
*/
|
*/
|
||||||
class Hour1200Transformer extends Transformer
|
class Hour1200Transformer extends HourTransformer
|
||||||
{
|
{
|
||||||
public function format(\DateTime $dateTime, $length)
|
public function format(\DateTime $dateTime, $length)
|
||||||
{
|
{
|
||||||
@ -25,6 +25,15 @@ class Hour1200Transformer extends Transformer
|
|||||||
return $this->padLeft($hourOfDay, $length);
|
return $this->padLeft($hourOfDay, $length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getMktimeHour($hour, $marker = null)
|
||||||
|
{
|
||||||
|
if ('PM' === $marker) {
|
||||||
|
$hour += 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $hour;
|
||||||
|
}
|
||||||
|
|
||||||
public function getReverseMatchingRegExp($length)
|
public function getReverseMatchingRegExp($length)
|
||||||
{
|
{
|
||||||
return '\d{1,2}';
|
return '\d{1,2}';
|
||||||
@ -34,7 +43,7 @@ class Hour1200Transformer extends Transformer
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'hour' => (int) $matched,
|
'hour' => (int) $matched,
|
||||||
'hourType' => '1200'
|
'hourInstance' => $this
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,25 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
|
|||||||
*
|
*
|
||||||
* @author Igor Wiedler <igor@wiedler.ch>
|
* @author Igor Wiedler <igor@wiedler.ch>
|
||||||
*/
|
*/
|
||||||
class Hour1201Transformer extends Transformer
|
class Hour1201Transformer extends HourTransformer
|
||||||
{
|
{
|
||||||
public function format(\DateTime $dateTime, $length)
|
public function format(\DateTime $dateTime, $length)
|
||||||
{
|
{
|
||||||
return $this->padLeft($dateTime->format('g'), $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)
|
public function getReverseMatchingRegExp($length)
|
||||||
{
|
{
|
||||||
return '\d{1,2}';
|
return '\d{1,2}';
|
||||||
@ -32,7 +44,7 @@ class Hour1201Transformer extends Transformer
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'hour' => (int) $matched,
|
'hour' => (int) $matched,
|
||||||
'hourType' => '1201'
|
'hourInstance' => $this
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,22 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
|
|||||||
*
|
*
|
||||||
* @author Igor Wiedler <igor@wiedler.ch>
|
* @author Igor Wiedler <igor@wiedler.ch>
|
||||||
*/
|
*/
|
||||||
class Hour2400Transformer extends Transformer
|
class Hour2400Transformer extends HourTransformer
|
||||||
{
|
{
|
||||||
public function format(\DateTime $dateTime, $length)
|
public function format(\DateTime $dateTime, $length)
|
||||||
{
|
{
|
||||||
return $this->padLeft($dateTime->format('G'), $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)
|
public function getReverseMatchingRegExp($length)
|
||||||
{
|
{
|
||||||
return '\d{1,2}';
|
return '\d{1,2}';
|
||||||
@ -32,7 +41,7 @@ class Hour2400Transformer extends Transformer
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'hour' => (int) $matched,
|
'hour' => (int) $matched,
|
||||||
'hourType' => '2400'
|
'hourInstance' => $this
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ namespace Symfony\Component\Locale\Stub\DateFormat;
|
|||||||
*
|
*
|
||||||
* @author Igor Wiedler <igor@wiedler.ch>
|
* @author Igor Wiedler <igor@wiedler.ch>
|
||||||
*/
|
*/
|
||||||
class Hour2401Transformer extends Transformer
|
class Hour2401Transformer extends HourTransformer
|
||||||
{
|
{
|
||||||
public function format(\DateTime $dateTime, $length)
|
public function format(\DateTime $dateTime, $length)
|
||||||
{
|
{
|
||||||
@ -25,6 +25,15 @@ class Hour2401Transformer extends Transformer
|
|||||||
return $this->padLeft($hourOfDay, $length);
|
return $this->padLeft($hourOfDay, $length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getMktimeHour($hour, $marker = null)
|
||||||
|
{
|
||||||
|
if (null !== $marker || 24 === $hour) {
|
||||||
|
$hour = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $hour;
|
||||||
|
}
|
||||||
|
|
||||||
public function getReverseMatchingRegExp($length)
|
public function getReverseMatchingRegExp($length)
|
||||||
{
|
{
|
||||||
return '\d{1,2}';
|
return '\d{1,2}';
|
||||||
@ -34,7 +43,7 @@ class Hour2401Transformer extends Transformer
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'hour' => (int) $matched,
|
'hour' => (int) $matched,
|
||||||
'hourType' => '2401'
|
'hourInstance' => $this
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
Reference in New Issue
Block a user