Skip to content

Commit

Permalink
Merge pull request #1912 from kylekatarnls/feature/356-picker-date-ti…
Browse files Browse the repository at this point in the history
…me-precision

#356 Add toDateTimeLocalString precision parameter
  • Loading branch information
kylekatarnls authored Oct 16, 2019
2 parents 443fe5f + 697e5c3 commit ae0a03e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 14 deletions.
27 changes: 23 additions & 4 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,15 @@ public function getSettings();
*/
public static function getTestNow();

/**
* Return a format from H:i to H:i:s.u according to given unit precision.
*
* @param string $unitPrecision "minute", "second", "millisecond" or "microsecond"
*
* @return string
*/
public static function getTimeFormatByPrecision($unitPrecision);

/**
* Get the translation of the current week day name (with context for languages with multiple forms).
*
Expand Down Expand Up @@ -4161,11 +4170,15 @@ public function toDateTimeImmutable();
* @example
* ```
* echo Carbon::now()->toDateTimeLocalString();
* echo "\n";
* echo Carbon::now()->toDateTimeLocalString('minute'); // You can specify precision among: minute, second, millisecond and microsecond
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toDateTimeLocalString();
public function toDateTimeLocalString($unitPrecision = 'second');

/**
* Format the instance as date and time
Expand All @@ -4175,9 +4188,11 @@ public function toDateTimeLocalString();
* echo Carbon::now()->toDateTimeString();
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toDateTimeString();
public function toDateTimeString($unitPrecision = 'second');

/**
* Format the instance with day, date and time
Expand Down Expand Up @@ -4246,9 +4261,11 @@ public function toIso8601String();
* echo Carbon::now()->toIso8601ZuluString();
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toIso8601ZuluString();
public function toIso8601ZuluString($unitPrecision = 'second');

/**
* Return the ISO-8601 string (ex: 1977-04-22T06:00:00Z) with UTC timezone.
Expand Down Expand Up @@ -4438,9 +4455,11 @@ public function toString();
* echo Carbon::now()->toTimeString();
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toTimeString();
public function toTimeString($unitPrecision = 'second');

/**
* Format the instance as W3C
Expand Down
1 change: 1 addition & 0 deletions src/Carbon/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @method int getMidDayAt() get midday/noon hour
* @method Closure|Carbon getTestNow() Get the Carbon instance (real or mock) to be returned when a "now"
* instance is created.
* @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision.
* @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key.
* @method \Symfony\Component\Translation\TranslatorInterface getTranslator() Get the default translator instance in use.
* @method int getWeekEndsAt() Get the last day of week
Expand Down
1 change: 1 addition & 0 deletions src/Carbon/FactoryImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @method int getMidDayAt() get midday/noon hour
* @method Closure|CarbonImmutable getTestNow() Get the Carbon instance (real or mock) to be returned when a "now"
* instance is created.
* @method string getTimeFormatByPrecision($unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision.
* @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key.
* @method \Symfony\Component\Translation\TranslatorInterface getTranslator() Get the default translator instance in use.
* @method int getWeekEndsAt() Get the last day of week
Expand Down
52 changes: 44 additions & 8 deletions src/Carbon/Traits/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Closure;
use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;

/**
* Trait Converter.
Expand Down Expand Up @@ -162,11 +163,13 @@ public function toFormattedDateString()
* echo Carbon::now()->toTimeString();
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toTimeString()
public function toTimeString($unitPrecision = 'second')
{
return $this->rawFormat('H:i:s');
return $this->rawFormat(static::getTimeFormatByPrecision($unitPrecision));
}

/**
Expand All @@ -177,11 +180,38 @@ public function toTimeString()
* echo Carbon::now()->toDateTimeString();
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toDateTimeString()
public function toDateTimeString($unitPrecision = 'second')
{
return $this->rawFormat('Y-m-d H:i:s');
return $this->rawFormat('Y-m-d '.static::getTimeFormatByPrecision($unitPrecision));
}

/**
* Return a format from H:i to H:i:s.u according to given unit precision.
*
* @param string $unitPrecision "minute", "second", "millisecond" or "microsecond"
*
* @return string
*/
public static function getTimeFormatByPrecision($unitPrecision)
{
switch (static::singularUnit($unitPrecision)) {
case 'minute':
return 'H:i';
case 'second':
return 'H:i:s';
case 'm':
case 'millisecond':
return 'H:i:s.v';
case 'µ':
case 'microsecond':
return 'H:i:s.u';
}

throw new InvalidArgumentException('Precision unit expected among: minute, second, millisecond and microsecond.');
}

/**
Expand All @@ -190,13 +220,17 @@ public function toDateTimeString()
* @example
* ```
* echo Carbon::now()->toDateTimeLocalString();
* echo "\n";
* echo Carbon::now()->toDateTimeLocalString('minute'); // You can specify precision among: minute, second, millisecond and microsecond
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toDateTimeLocalString()
public function toDateTimeLocalString($unitPrecision = 'second')
{
return $this->rawFormat('Y-m-d\TH:i:s');
return $this->rawFormat('Y-m-d\T'.static::getTimeFormatByPrecision($unitPrecision));
}

/**
Expand Down Expand Up @@ -282,11 +316,13 @@ public function toRfc822String()
* echo Carbon::now()->toIso8601ZuluString();
* ```
*
* @param string $unitPrecision
*
* @return string
*/
public function toIso8601ZuluString()
public function toIso8601ZuluString($unitPrecision = 'second')
{
return $this->copy()->utc()->rawFormat('Y-m-d\TH:i:s\Z');
return $this->copy()->utc()->rawFormat('Y-m-d\T'.static::getTimeFormatByPrecision($unitPrecision).'\Z');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2368,7 +2368,7 @@ public function setUnit($unit, $value = null)
*/
public static function singularUnit(string $unit): string
{
$unit = rtrim(strtolower($unit), 's');
$unit = rtrim(mb_strtolower($unit), 's');

if ($unit === 'centurie') {
return 'century';
Expand Down
17 changes: 16 additions & 1 deletion tests/Carbon/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Carbon\CarbonInterface;
use Carbon\Factory;
use DateTime;
use InvalidArgumentException;
use Tests\AbstractTestCase;
use Tests\Carbon\Fixtures\BadIsoCarbon;
use Tests\Carbon\Fixtures\MyCarbon;
Expand Down Expand Up @@ -115,8 +116,22 @@ public function testToDateString()

public function testToDateTimeLocalString()
{
$d = Carbon::create(1975, 12, 25, 14, 15, 16);
$d = Carbon::create(1975, 12, 25, 14, 15, 16.615342);
$this->assertSame('1975-12-25T14:15:16', $d->toDateTimeLocalString());
$this->assertSame('1975-12-25T14:15', $d->toDateTimeLocalString('minute'));
$this->assertSame('1975-12-25T14:15:16', $d->toDateTimeLocalString('second'));
$this->assertSame('1975-12-25T14:15:16.615', $d->toDateTimeLocalString('millisecond'));
$this->assertSame('1975-12-25T14:15:16.615342', $d->toDateTimeLocalString('µs'));

$message = null;

try {
$d->toDateTimeLocalString('hour');
} catch (InvalidArgumentException $exception) {
$message = $exception->getMessage();
}

$this->assertSame('Precision unit expected among: minute, second, millisecond and microsecond.', $message);
}

public function testToFormattedDateString()
Expand Down

0 comments on commit ae0a03e

Please sign in to comment.