Correct capitalization of microsecond

This commit is contained in:
Ben Ramsey
2020-03-04 16:51:46 -06:00
parent a21eb6c3ca
commit b3c26661ca
18 changed files with 83 additions and 83 deletions
+1 -1
View File
@@ -233,7 +233,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
instead of `mixed`; this simplifies the interface and makes it consistent:
* `NumberConverterInterface::fromHex(string $hex): string`
* `NumberConverterInterface::toHex(string $number): string`
* `TimeConverterInterface::calculateTime(string $seconds, string $microSeconds): array`
* `TimeConverterInterface::calculateTime(string $seconds, string $microseconds): array`
* `UnsupportedOperationException` is now descended from `\LogicException`.
Previously, it descended from `\RuntimeException`.
* When encoding to bytes or decoding from bytes, `OrderedTimeCodec` now checks
@@ -41,9 +41,9 @@ class BigNumberTimeConverter implements TimeConverterInterface
* @inheritDoc
* @psalm-pure
*/
public function calculateTime(string $seconds, string $microSeconds): Hexadecimal
public function calculateTime(string $seconds, string $microseconds): Hexadecimal
{
return $this->converter->calculateTime($seconds, $microSeconds);
return $this->converter->calculateTime($seconds, $microseconds);
}
/**
+3 -3
View File
@@ -62,9 +62,9 @@ class GenericTimeConverter implements TimeConverterInterface
* @inheritDoc
* @psalm-pure
*/
public function calculateTime(string $seconds, string $microSeconds): Hexadecimal
public function calculateTime(string $seconds, string $microseconds): Hexadecimal
{
$timestamp = new Time($seconds, $microSeconds);
$timestamp = new Time($seconds, $microseconds);
// Convert the seconds into a count of 100-nanosecond intervals.
$sec = $this->calculator->multiply(
@@ -74,7 +74,7 @@ class GenericTimeConverter implements TimeConverterInterface
// Convert the microseconds into a count of 100-nanosecond intervals.
$usec = $this->calculator->multiply(
$timestamp->getMicroSeconds(),
$timestamp->getMicroseconds(),
new IntegerObject(self::MICROSECOND_INTERVALS)
);
+4 -4
View File
@@ -92,15 +92,15 @@ class PhpTimeConverter implements TimeConverterInterface
* @inheritDoc
* @psalm-pure
*/
public function calculateTime(string $seconds, string $microSeconds): Hexadecimal
public function calculateTime(string $seconds, string $microseconds): Hexadecimal
{
$seconds = new IntegerObject($seconds);
$microSeconds = new IntegerObject($microSeconds);
$microseconds = new IntegerObject($microseconds);
// Calculate the count of 100-nanosecond intervals since the Gregorian
// calendar epoch for the given seconds and microseconds.
$uuidTime = ((int) $seconds->toString() * self::SECOND_INTERVALS)
+ ((int) $microSeconds->toString() * self::MICROSECOND_INTERVALS)
+ ((int) $microseconds->toString() * self::MICROSECOND_INTERVALS)
+ self::GREGORIAN_TO_UNIX_INTERVALS;
// Check to see whether we've overflowed the max/min integer size.
@@ -108,7 +108,7 @@ class PhpTimeConverter implements TimeConverterInterface
if (!is_int($uuidTime)) {
return $this->fallbackConverter->calculateTime(
$seconds->toString(),
$microSeconds->toString()
$microseconds->toString()
);
}
+2 -2
View File
@@ -32,14 +32,14 @@ interface TimeConverterInterface
*
* @param string $seconds A string representation of the number of seconds
* since the Unix epoch for the time to calculate
* @param string $microSeconds A string representation of the micro-seconds
* @param string $microseconds A string representation of the micro-seconds
* associated with the time to calculate
*
* @return Hexadecimal The full UUID timestamp as a Hexadecimal value
*
* @psalm-pure
*/
public function calculateTime(string $seconds, string $microSeconds): Hexadecimal;
public function calculateTime(string $seconds, string $microseconds): Hexadecimal;
/**
* Converts a timestamp extracted from a UUID to a Unix timestamp
+1 -1
View File
@@ -148,7 +148,7 @@ trait DeprecatedUuidMethodsTrait
'@'
. $time->getSeconds()->toString()
. '.'
. str_pad($time->getMicroSeconds()->toString(), 6, '0', STR_PAD_LEFT)
. str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
);
} catch (Throwable $e) {
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
+1 -1
View File
@@ -96,7 +96,7 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
$uuidTime = $this->timeConverter->calculateTime(
$time->getSeconds()->toString(),
$time->getMicroSeconds()->toString()
$time->getMicroseconds()->toString()
);
$timeHex = str_pad($uuidTime->toString(), 16, '0', STR_PAD_LEFT);
+1 -1
View File
@@ -87,7 +87,7 @@ final class UuidV6 extends Uuid implements UuidInterface
'@'
. $time->getSeconds()->toString()
. '.'
. str_pad($time->getMicroSeconds()->toString(), 6, '0', STR_PAD_LEFT)
. str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
);
} catch (Throwable $e) {
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
+1 -1
View File
@@ -53,7 +53,7 @@ class FixedTimeProvider implements TimeProviderInterface
*/
public function setSec($value): void
{
$this->fixedTime = new Time($value, $this->fixedTime->getMicroSeconds());
$this->fixedTime = new Time($value, $this->fixedTime->getMicroseconds());
}
public function getTime(): Time
+1 -1
View File
@@ -83,7 +83,7 @@ final class UuidV1 extends Uuid implements UuidInterface
'@'
. $time->getSeconds()->toString()
. '.'
. str_pad($time->getMicroSeconds()->toString(), 6, '0', STR_PAD_LEFT)
. str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
);
} catch (Throwable $e) {
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
+1 -1
View File
@@ -100,7 +100,7 @@ final class UuidV2 extends Uuid implements UuidInterface
'@'
. $time->getSeconds()->toString()
. '.'
. str_pad($time->getMicroSeconds()->toString(), 6, '0', STR_PAD_LEFT)
. str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
);
} catch (Throwable $e) {
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
+8 -8
View File
@@ -40,16 +40,16 @@ final class Time implements TypeInterface
/**
* @var IntegerObject
*/
private $microSeconds;
private $microseconds;
/**
* @param mixed $seconds
* @param mixed $microSeconds
* @param mixed $microseconds
*/
public function __construct($seconds, $microSeconds = 0)
public function __construct($seconds, $microseconds = 0)
{
$this->seconds = new IntegerObject($seconds);
$this->microSeconds = new IntegerObject($microSeconds);
$this->microseconds = new IntegerObject($microseconds);
}
public function getSeconds(): IntegerObject
@@ -57,14 +57,14 @@ final class Time implements TypeInterface
return $this->seconds;
}
public function getMicroSeconds(): IntegerObject
public function getMicroseconds(): IntegerObject
{
return $this->microSeconds;
return $this->microseconds;
}
public function toString(): string
{
return $this->seconds->toString() . '.' . $this->microSeconds->toString();
return $this->seconds->toString() . '.' . $this->microseconds->toString();
}
public function __toString(): string
@@ -79,7 +79,7 @@ final class Time implements TypeInterface
{
return [
'seconds' => $this->getSeconds()->toString(),
'microseconds' => $this->getMicroSeconds()->toString(),
'microseconds' => $this->getMicroseconds()->toString(),
];
}
@@ -17,11 +17,11 @@ class BigNumberTimeConverterTest extends TestCase
public function testCalculateTimeReturnsArrayOfTimeSegments(): void
{
$seconds = BigInteger::of(5);
$microSeconds = BigInteger::of(3);
$microseconds = BigInteger::of(3);
$calculatedTime = BigInteger::zero()
->plus($seconds->multipliedBy(10000000))
->plus($microSeconds->multipliedBy(10))
->plus($microseconds->multipliedBy(10))
->plus(BigInteger::fromBase('01b21dd213814000', 16));
$maskLow = BigInteger::fromBase('ffffffff', 16);
@@ -33,7 +33,7 @@ class BigNumberTimeConverterTest extends TestCase
$expected .= sprintf('%08s', $calculatedTime->and($maskLow)->toBase(16));
$converter = new BigNumberTimeConverter();
$returned = $converter->calculateTime((string) $seconds, (string) $microSeconds);
$returned = $converter->calculateTime((string) $seconds, (string) $microseconds);
$this->assertInstanceOf(Hexadecimal::class, $returned);
$this->assertSame($expected, $returned->toString());
@@ -60,7 +60,7 @@ class BigNumberTimeConverterTest extends TestCase
$converter->calculateTime('12.34', '5678');
}
public function testCalculateTimeThrowsExceptionWhenMicroSecondsIsNotOnlyDigits(): void
public function testCalculateTimeThrowsExceptionWhenMicrosecondsIsNotOnlyDigits(): void
{
$converter = new BigNumberTimeConverter();
@@ -14,12 +14,12 @@ class GenericTimeConverterTest extends TestCase
/**
* @dataProvider provideCalculateTime
*/
public function testCalculateTime(string $seconds, string $microSeconds, string $expected): void
public function testCalculateTime(string $seconds, string $microseconds, string $expected): void
{
$calculator = new BrickMathCalculator();
$converter = new GenericTimeConverter($calculator);
$result = $converter->calculateTime($seconds, $microSeconds);
$result = $converter->calculateTime($seconds, $microseconds);
$this->assertSame($expected, $result->toString());
}
@@ -32,7 +32,7 @@ class GenericTimeConverterTest extends TestCase
return [
[
'seconds' => '-12219146756',
'microSeconds' => '0',
'microseconds' => '0',
'expected' => '000001540901e600',
],
[
@@ -50,7 +50,7 @@ class GenericTimeConverterTest extends TestCase
// 1582-10-15 00:00:00.000000
[
'seconds' => '-12219292800',
'microSeconds' => '0',
'microseconds' => '0',
'expected' => '0000000000000000',
],
@@ -82,7 +82,7 @@ class GenericTimeConverterTest extends TestCase
/**
* @dataProvider provideConvertTime
*/
public function testConvertTime(Hexadecimal $uuidTimestamp, string $unixTimestamp, string $microSeconds): void
public function testConvertTime(Hexadecimal $uuidTimestamp, string $unixTimestamp, string $microseconds): void
{
$calculator = new BrickMathCalculator();
$converter = new GenericTimeConverter($calculator);
@@ -90,7 +90,7 @@ class GenericTimeConverterTest extends TestCase
$result = $converter->convertTime($uuidTimestamp);
$this->assertSame($unixTimestamp, $result->getSeconds()->toString());
$this->assertSame($microSeconds, $result->getMicroSeconds()->toString());
$this->assertSame($microseconds, $result->getMicroseconds()->toString());
}
/**
@@ -102,17 +102,17 @@ class GenericTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('1e1c57dff6f8cb0'),
'unixTimestamp' => '1341368074',
'microSeconds' => '491000',
'microseconds' => '491000',
],
[
'uuidTimestamp' => new Hexadecimal('1ea333764c71df6'),
'unixTimestamp' => '1578612359',
'microSeconds' => '521023',
'microseconds' => '521023',
],
[
'uuidTimestamp' => new Hexadecimal('fffffffff9785f6'),
'unixTimestamp' => '103072857659',
'microSeconds' => '999999',
'microseconds' => '999999',
],
// This is the last possible time supported by v1 UUIDs. When
@@ -121,7 +121,7 @@ class GenericTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('fffffffffffffffa'),
'unixTimestamp' => '1832455114570',
'microSeconds' => '955161',
'microseconds' => '955161',
],
// This is the earliest possible date supported by v1 UUIDs:
@@ -129,7 +129,7 @@ class GenericTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('000000000000'),
'unixTimestamp' => '-12219292800',
'microSeconds' => '0',
'microseconds' => '0',
],
// This is the Unix epoch:
@@ -137,7 +137,7 @@ class GenericTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('1b21dd213814000'),
'unixTimestamp' => '0',
'microSeconds' => '0',
'microseconds' => '0',
],
];
}
+16 -16
View File
@@ -20,11 +20,11 @@ class PhpTimeConverterTest extends TestCase
public function testCalculateTimeReturnsArrayOfTimeSegments(): void
{
$seconds = BigInteger::of(5);
$microSeconds = BigInteger::of(3);
$microseconds = BigInteger::of(3);
$calculatedTime = BigInteger::zero()
->plus($seconds->multipliedBy(10000000))
->plus($microSeconds->multipliedBy(10))
->plus($microseconds->multipliedBy(10))
->plus(BigInteger::fromBase('01b21dd213814000', 16));
$maskLow = BigInteger::fromBase('ffffffff', 16);
@@ -36,7 +36,7 @@ class PhpTimeConverterTest extends TestCase
$expected .= sprintf('%08s', $calculatedTime->and($maskLow)->toBase(16));
$converter = new PhpTimeConverter();
$returned = $converter->calculateTime((string) $seconds, (string) $microSeconds);
$returned = $converter->calculateTime((string) $seconds, (string) $microseconds);
$this->assertSame($expected, $returned->toString());
}
@@ -55,7 +55,7 @@ class PhpTimeConverterTest extends TestCase
$converter->calculateTime('12.34', '5678');
}
public function testCalculateTimeThrowsExceptionWhenMicroSecondsIsNotOnlyDigits(): void
public function testCalculateTimeThrowsExceptionWhenMicrosecondsIsNotOnlyDigits(): void
{
/** @var Mockery\MockInterface & PhpTimeConverter $converter */
$converter = Mockery::mock(PhpTimeConverter::class)->makePartial();
@@ -72,7 +72,7 @@ class PhpTimeConverterTest extends TestCase
/**
* @dataProvider provideConvertTime
*/
public function testConvertTime(Hexadecimal $uuidTimestamp, string $unixTimestamp, string $microSeconds): void
public function testConvertTime(Hexadecimal $uuidTimestamp, string $unixTimestamp, string $microseconds): void
{
$calculator = new BrickMathCalculator();
$fallbackConverter = new GenericTimeConverter($calculator);
@@ -81,7 +81,7 @@ class PhpTimeConverterTest extends TestCase
$result = $converter->convertTime($uuidTimestamp);
$this->assertSame($unixTimestamp, $result->getSeconds()->toString());
$this->assertSame($microSeconds, $result->getMicroSeconds()->toString());
$this->assertSame($microseconds, $result->getMicroseconds()->toString());
}
/**
@@ -93,17 +93,17 @@ class PhpTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('1e1c57dff6f8cb0'),
'unixTimestamp' => '1341368074',
'microSeconds' => '491000',
'microseconds' => '491000',
],
[
'uuidTimestamp' => new Hexadecimal('1ea333764c71df6'),
'unixTimestamp' => '1578612359',
'microSeconds' => '521023',
'microseconds' => '521023',
],
[
'uuidTimestamp' => new Hexadecimal('fffffffff9785f6'),
'unixTimestamp' => '103072857659',
'microSeconds' => '999999',
'microseconds' => '999999',
],
// This is the last possible time supported by v1 UUIDs. When
@@ -112,7 +112,7 @@ class PhpTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('fffffffffffffffa'),
'unixTimestamp' => '1832455114570',
'microSeconds' => '955161',
'microseconds' => '955161',
],
// This is the earliest possible date supported by v1 UUIDs:
@@ -120,7 +120,7 @@ class PhpTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('000000000000'),
'unixTimestamp' => '-12219292800',
'microSeconds' => '0',
'microseconds' => '0',
],
// This is the Unix epoch:
@@ -128,7 +128,7 @@ class PhpTimeConverterTest extends TestCase
[
'uuidTimestamp' => new Hexadecimal('1b21dd213814000'),
'unixTimestamp' => '0',
'microSeconds' => '0',
'microseconds' => '0',
],
];
}
@@ -136,13 +136,13 @@ class PhpTimeConverterTest extends TestCase
/**
* @dataProvider provideCalculateTime
*/
public function testCalculateTime(string $seconds, string $microSeconds, string $expected): void
public function testCalculateTime(string $seconds, string $microseconds, string $expected): void
{
$calculator = new BrickMathCalculator();
$fallbackConverter = new GenericTimeConverter($calculator);
$converter = new PhpTimeConverter($calculator, $fallbackConverter);
$result = $converter->calculateTime($seconds, $microSeconds);
$result = $converter->calculateTime($seconds, $microseconds);
$this->assertSame($expected, $result->toString());
}
@@ -155,7 +155,7 @@ class PhpTimeConverterTest extends TestCase
return [
[
'seconds' => '-12219146756',
'microSeconds' => '0',
'microseconds' => '0',
'expected' => '000001540901e600',
],
[
@@ -173,7 +173,7 @@ class PhpTimeConverterTest extends TestCase
// 1582-10-15 00:00:00.000000
[
'seconds' => '-12219292800',
'microSeconds' => '0',
'microseconds' => '0',
'expected' => '0000000000000000',
],
+2 -2
View File
@@ -532,8 +532,8 @@ class ExpectedBehaviorTest extends TestCase
$timeConverter = \Mockery::mock('Ramsey\Uuid\Converter\TimeConverterInterface');
$timeConverter
->shouldReceive('calculateTime')
->andReturnUsing(function ($seconds, $microSeconds) {
return new Hexadecimal('abcd' . dechex($microSeconds) . dechex($seconds));
->andReturnUsing(function ($seconds, $microseconds) {
return new Hexadecimal('abcd' . dechex($microseconds) . dechex($seconds));
});
$timeProvider = \Mockery::mock('Ramsey\Uuid\Provider\TimeProviderInterface', [
@@ -24,17 +24,17 @@ class FixedTimeProviderTest extends TestCase
$provider = new FixedTimeProvider($time);
$this->assertSame('1458844556', $provider->getTime()->getSeconds()->toString());
$this->assertSame('200997', $provider->getTime()->getMicroSeconds()->toString());
$this->assertSame('200997', $provider->getTime()->getMicroseconds()->toString());
$provider->setSec(1050804050);
$this->assertSame('1050804050', $provider->getTime()->getSeconds()->toString());
$this->assertSame('200997', $provider->getTime()->getMicroSeconds()->toString());
$this->assertSame('200997', $provider->getTime()->getMicroseconds()->toString());
$provider->setUsec(30192);
$this->assertSame('1050804050', $provider->getTime()->getSeconds()->toString());
$this->assertSame('30192', $provider->getTime()->getMicroSeconds()->toString());
$this->assertSame('30192', $provider->getTime()->getMicroseconds()->toString());
$this->assertNotSame($time, $provider->getTime());
}
+20 -20
View File
@@ -16,18 +16,18 @@ class TimeTest extends TestCase
{
/**
* @param int|float|string $seconds
* @param int|float|string|null $microSeconds
* @param int|float|string|null $microseconds
*
* @dataProvider provideTimeValues
*/
public function testTime($seconds, $microSeconds): void
public function testTime($seconds, $microseconds): void
{
$params = [$seconds];
$timeString = (string) $seconds;
if ($microSeconds !== null) {
$params[] = $microSeconds;
$timeString .= ".{$microSeconds}";
if ($microseconds !== null) {
$params[] = $microseconds;
$timeString .= ".{$microseconds}";
} else {
$timeString .= '.0';
}
@@ -37,8 +37,8 @@ class TimeTest extends TestCase
$this->assertSame((string) $seconds, $time->getSeconds()->toString());
$this->assertSame(
(string) $microSeconds ?: '0',
$time->getMicroSeconds()->toString()
(string) $microseconds ?: '0',
$time->getMicroseconds()->toString()
);
$this->assertSame($timeString, (string) $time);
@@ -52,26 +52,26 @@ class TimeTest extends TestCase
return [
[
'seconds' => 103072857659,
'microSeconds' => null,
'microseconds' => null,
],
[
'seconds' => -12219292800,
'microSeconds' => 1234,
'microseconds' => 1234,
],
];
}
/**
* @param int|float|string $seconds
* @param int|float|string|null $microSeconds
* @param int|float|string|null $microseconds
*
* @dataProvider provideTimeValues
*/
public function testSerializeUnserializeTime($seconds, $microSeconds): void
public function testSerializeUnserializeTime($seconds, $microseconds): void
{
$params = [$seconds];
if ($microSeconds !== null) {
$params[] = $microSeconds;
if ($microseconds !== null) {
$params[] = $microseconds;
}
$time = new Time(...$params);
@@ -81,8 +81,8 @@ class TimeTest extends TestCase
$this->assertSame((string) $seconds, $unserializedTime->getSeconds()->toString());
$this->assertSame(
(string) $microSeconds ?: '0',
$unserializedTime->getMicroSeconds()->toString()
(string) $microseconds ?: '0',
$unserializedTime->getMicroseconds()->toString()
);
}
@@ -98,22 +98,22 @@ class TimeTest extends TestCase
/**
* @param int|float|string $seconds
* @param int|float|string|null $microSeconds
* @param int|float|string|null $microseconds
*
* @dataProvider provideTimeValues
*/
public function testJsonSerialize($seconds, $microSeconds): void
public function testJsonSerialize($seconds, $microseconds): void
{
$time = [
'seconds' => (string) $seconds,
'microseconds' => (string) $microSeconds ?: '0',
'microseconds' => (string) $microseconds ?: '0',
];
$expectedJson = json_encode($time);
$params = [$seconds];
if ($microSeconds !== null) {
$params[] = $microSeconds;
if ($microseconds !== null) {
$params[] = $microseconds;
}
$time = new Time(...$params);