Skip to content

Commit 3b544af

Browse files
replaced bcmath functions with different logic
1 parent 47938ce commit 3b544af

File tree

9 files changed

+35
-20
lines changed

9 files changed

+35
-20
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"minimum-stability": "stable",
1010
"require": {
1111
"php": ">=7.1.0",
12-
"ext-mbstring": "*",
13-
"ext-bcmath": "*"
12+
"ext-mbstring": "*"
1413
},
1514
"require-dev": {
1615
"phpunit/phpunit": ">=7.5.0"

src/structures/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public function days(): int
4040

4141
public function __toString(): string
4242
{
43-
return gmdate('Y-m-d', strtotime('+' . $this->days . ' days +0000', 0));
43+
return gmdate('Y-m-d', strtotime(sprintf("%+d", $this->days) . ' days +0000', 0));
4444
}
4545
}

src/structures/DateTime.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public function tz_offset_seconds(): int
7676

7777
public function __toString(): string
7878
{
79-
$ts = bcadd($this->seconds - $this->tz_offset_seconds, bcdiv($this->nanoseconds, 1e9, 6), 6);
80-
return \DateTime::createFromFormat('U.u', $ts, new \DateTimeZone('UTC'))
79+
$datetime = sprintf("%d", $this->seconds - $this->tz_offset_seconds)
80+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6);
81+
return \DateTime::createFromFormat('U.u', $datetime, new \DateTimeZone('UTC'))
8182
->setTimezone(new \DateTimeZone(sprintf("%+'05d", $this->tz_offset_seconds / 3600 * 100)))
8283
->format('Y-m-d\TH:i:s.uP');
8384
}

src/structures/DateTimeZoneId.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public function tz_id(): string
7676

7777
public function __toString(): string
7878
{
79-
$timestamp = bcadd($this->seconds, bcdiv($this->nanoseconds, 1e9, 6), 6);
80-
return \DateTime::createFromFormat('U.u', $timestamp, new \DateTimeZone($this->tz_id))
79+
$datetime = sprintf("%d", $this->seconds)
80+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6);
81+
return \DateTime::createFromFormat('U.u', $datetime, new \DateTimeZone($this->tz_id))
8182
->format('Y-m-d\TH:i:s.u') . '[' . $this->tz_id . ']';
8283
}
8384
}

src/structures/Duration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public function __toString(): string
102102
if (!empty($minutes))
103103
$time .= $minutes . 'M';
104104

105-
$seconds = rtrim(bcadd($this->seconds % 3600 % 60, bcdiv($this->nanoseconds, 1e9, 6), 6), '0.');
105+
$seconds = rtrim(sprintf("%d", $this->seconds % 3600 % 60)
106+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6), '0.');
106107
if (!empty($seconds))
107108
$time .= $seconds . 'S';
108109

src/structures/LocalDateTime.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public function nanoseconds(): int
5656

5757
public function __toString(): string
5858
{
59-
return \DateTime::createFromFormat('U.u', bcadd($this->seconds, bcdiv($this->nanoseconds, 1e9, 6), 6))
59+
$datetime = sprintf("%d", $this->seconds)
60+
. '.' . substr(sprintf("%09d", $this->nanoseconds), 0, 6);
61+
return \DateTime::createFromFormat('U.u', $datetime)
6062
->format('Y-m-d\TH:i:s.u');
6163
}
6264

src/structures/LocalTime.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ public function nanoseconds(): int
4040

4141
public function __toString(): string
4242
{
43-
return \DateTime::createFromFormat('U.u', bcdiv($this->nanoseconds, 1e9, 6))
43+
$value = sprintf("%09d", $this->nanoseconds);
44+
$seconds = substr($value, 0, -9);
45+
if (empty($seconds))
46+
$seconds = '0';
47+
$fraction = substr($value, -9, 6);
48+
49+
return \DateTime::createFromFormat('U.u', $seconds . '.' . $fraction)
4450
->format('H:i:s.u');
4551
}
4652
}

src/structures/Time.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ public function tz_offset_seconds(): int
5959

6060
public function __toString(): string
6161
{
62-
$ts = bcsub(bcdiv($this->nanoseconds, 1e9, 6), $this->tz_offset_seconds, 6);
63-
return \DateTime::createFromFormat('U.u', $ts, new \DateTimeZone('UTC'))
62+
$value = sprintf("%09d", $this->nanoseconds - $this->tz_offset_seconds * 1e9);
63+
$seconds = substr($value, 0, -9);
64+
if (empty($seconds))
65+
$seconds = '0';
66+
$fraction = substr($value, -9, 6);
67+
68+
return \DateTime::createFromFormat('U.u', $seconds . '.' . $fraction, new \DateTimeZone('UTC'))
6469
->setTimezone(new \DateTimeZone(sprintf("%+'05d", $this->tz_offset_seconds / 3600 * 100)))
6570
->format('H:i:s.uP');
6671
}

tests/StructuresTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class StructuresTest extends TestCase
5454
* How many iterations do for each date/time test
5555
* @var int
5656
*/
57-
public static $iterations = 10;
57+
public static $iterations = 50;
5858

5959
public function testInit(): AProtocol
6060
{
@@ -111,7 +111,7 @@ public function testDate(int $timestamp, AProtocol $protocol)
111111
public function testDateTime(int $timestamp, string $timezone, AProtocol $protocol)
112112
{
113113
try {
114-
$timestamp = bcadd($timestamp, sprintf('%.6f', fmod(microtime(true), 1)), 6);
114+
$timestamp .= '.' . rand(0, 9e5);
115115
$datetime = \DateTime::createFromFormat('U.u', $timestamp, new \DateTimeZone($timezone))
116116
->format('Y-m-d\TH:i:s.uP');
117117

@@ -143,7 +143,7 @@ public function testDateTime(int $timestamp, string $timezone, AProtocol $protoc
143143
public function testDateTimeZoneId(int $timestamp, string $timezone, AProtocol $protocol)
144144
{
145145
try {
146-
$timestamp = bcadd($timestamp, sprintf('%.6f', fmod(microtime(true), 1)), 6);
146+
$timestamp .= '.' . rand(0, 9e5);
147147
$datetime = \DateTime::createFromFormat('U.u', $timestamp, new \DateTimeZone($timezone))
148148
->format('Y-m-d\TH:i:s.u') . '[' . $timezone . ']';
149149

@@ -214,7 +214,7 @@ public function testDuration(AProtocol $protocol)
214214
public function testLocalDateTime(int $timestamp, AProtocol $protocol)
215215
{
216216
try {
217-
$timestamp = bcadd($timestamp, sprintf('%.6f', fmod(microtime(true), 1)), 6);
217+
$timestamp .= '.' . rand(0, 9e5);
218218
$datetime = \DateTime::createFromFormat('U.u', $timestamp)
219219
->format('Y-m-d\TH:i:s.u');
220220

@@ -245,7 +245,7 @@ public function testLocalDateTime(int $timestamp, AProtocol $protocol)
245245
public function testLocalTime(int $timestamp, AProtocol $protocol)
246246
{
247247
try {
248-
$timestamp = bcadd($timestamp, sprintf('%.6f', fmod(microtime(true), 1)), 6);
248+
$timestamp .= '.' . rand(0, 9e5);
249249
$time = \DateTime::createFromFormat('U.u', $timestamp)
250250
->format('H:i:s.u');
251251

@@ -386,7 +386,7 @@ public function testRelationship(AProtocol $protocol)
386386
public function testTime(int $timestamp, string $timezone, AProtocol $protocol)
387387
{
388388
try {
389-
$timestamp = bcadd($timestamp, sprintf('%.6f', fmod(microtime(true), 1)), 6);
389+
$timestamp .= '.' . rand(0, 9e5);
390390
$time = \DateTime::createFromFormat('U.u', $timestamp, new \DateTimeZone($timezone))
391391
->format('H:i:s.uP');
392392

@@ -436,8 +436,8 @@ private function randomTimestamp(string $timezone = '+0000'): int
436436
{
437437
try {
438438
$zone = new \DateTimeZone($timezone);
439-
$start = new \DateTime('-3 years', $zone);
440-
$end = new \DateTime('+3 years', $zone);
439+
$start = new \DateTime(date('Y-m-d H:i:s', strtotime('-10 years', 0)), $zone);
440+
$end = new \DateTime(date('Y-m-d H:i:s', strtotime('+10 years', 0)), $zone);
441441
return rand($start->getTimestamp(), $end->getTimestamp());
442442
} catch (Exception $e) {
443443
return strtotime('now ' . $timezone);

0 commit comments

Comments
 (0)