From 91197f3bb26d818b8bac52102a79ea4769cc7e0c Mon Sep 17 00:00:00 2001 From: Arafat Hossain Ar Date: Wed, 30 Oct 2024 22:58:33 +0600 Subject: [PATCH 1/5] feat: file created --- Conversions/ConvertTime.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Conversions/ConvertTime.php diff --git a/Conversions/ConvertTime.php b/Conversions/ConvertTime.php new file mode 100644 index 00000000..e69de29b From 2a1a2808062f74d7fb19b7d46d4764e0ba9f0c44 Mon Sep 17 00:00:00 2001 From: Arafat Hossain Ar Date: Wed, 30 Oct 2024 23:02:08 +0600 Subject: [PATCH 2/5] add: added converter with example --- Conversions/ConvertTime.php | 91 +++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/Conversions/ConvertTime.php b/Conversions/ConvertTime.php index e69de29b..d68323f9 100644 --- a/Conversions/ConvertTime.php +++ b/Conversions/ConvertTime.php @@ -0,0 +1,91 @@ + [ + 'minutes' => 1 / 60, + 'hours' => 1 / 3600, + 'days' => 1 / 86400, + 'weeks' => 1 / 604800, + 'months' => 1 / 2592000, + 'years' => 1 / 31536000 + ], + 'minutes' => [ + 'seconds' => 60, + 'hours' => 1 / 60, + 'days' => 1 / 1440, + 'weeks' => 1 / 10080, + 'months' => 1 / 43200, + 'years' => 1 / 525600 + ], + 'hours' => [ + 'seconds' => 3600, + 'minutes' => 60, + 'days' => 1 / 24, + 'weeks' => 1 / 168, + 'months' => 1 / 720, + 'years' => 1 / 8760 + ], + 'days' => [ + 'seconds' => 86400, + 'minutes' => 1440, + 'hours' => 24, + 'weeks' => 1 / 7, + 'months' => 1 / 30.4, + 'years' => 1 / 365.25 + ], + 'weeks' => [ + 'seconds' => 604800, + 'minutes' => 10080, + 'hours' => 168, + 'days' => 7, + 'months' => 1 / 4.3, + 'years' => 1 / 52.17 + ], + 'months' => [ + 'seconds' => 2592000, + 'minutes' => 43200, + 'hours' => 720, + 'days' => 30.4, + 'weeks' => 4.3, + 'years' => 1 / 12 + ], + 'years' => [ + 'seconds' => 31536000, + 'minutes' => 525600, + 'hours' => 8760, + 'days' => 365.25, + 'weeks' => 52.17, + 'months' => 12 + ] + ]; + + if (!isset($conversionFactors[$fromUnit], $conversionFactors[$fromUnit][$toUnit])) { + throw new InvalidArgumentException("Invalid time unit(s) specified: $fromUnit to $toUnit."); + } + + $conversionFactor = $conversionFactors[$fromUnit][$toUnit]; + $result = $value * $conversionFactor; + + return round($result, 2); +} + +// Example usage +try { + $timeResult = convertTime(120, 'minutes', 'seconds'); + echo "120 minutes = $timeResult seconds\n"; +} catch (InvalidArgumentException $e) { + echo "Error: " . $e->getMessage() . "\n"; +} From fcd7dc73e52504e52488ee4421414fcce0c3844e Mon Sep 17 00:00:00 2001 From: Arafat Hossain Ar Date: Wed, 30 Oct 2024 23:18:50 +0600 Subject: [PATCH 3/5] add: text case added for ConvertTime classs --- tests/Conversions/ConversionsTest.php | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/Conversions/ConversionsTest.php b/tests/Conversions/ConversionsTest.php index 93893b4e..b39dcebc 100644 --- a/tests/Conversions/ConversionsTest.php +++ b/tests/Conversions/ConversionsTest.php @@ -10,6 +10,7 @@ require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php'; require_once __DIR__ . '/../../Conversions/SpeedConversion.php'; require_once __DIR__ . '/../../Conversions/TemperatureConversions.php'; +require_once __DIR__ . '/../../Conversions/ConvertTime.php'; class ConversionsTest extends TestCase { @@ -142,4 +143,41 @@ public function testFahrenheitToKelvin() $this->expectExceptionMessage('Temperature (Fahrenheit) must be a number'); FahrenheitToKelvin("non-numeric"); } + + public function testConvertTime() + { + // Test basic conversions + $this->assertEquals(2.0, convertTime(120, 'seconds', 'minutes'), "Failed converting 120 seconds to minutes"); + $this->assertEquals(2.0, convertTime(120, 'minutes', 'hours'), "Failed converting 120 minutes to hours"); + $this->assertEquals(2.0, convertTime(48, 'hours', 'days'), "Failed converting 48 hours to days"); + $this->assertEquals(2.0, convertTime(14, 'days', 'weeks'), "Failed converting 14 days to weeks"); + + // Test with different units and values + $this->assertEqualsWithDelta(2.0, convertTime(8.6, 'weeks', 'months'), 0.01, "Failed converting 8.6 weeks to months"); + $this->assertEquals(2.0, convertTime(24, 'months', 'years'), "Failed converting 24 months to years"); + + // Test edge cases + $this->assertEquals(0.0, convertTime(0, 'seconds', 'hours'), "Failed converting 0 seconds to hours"); + + // Test invalid 'from' and 'to' units + try { + convertTime(120, 'invalidUnit', 'minutes'); + } catch (InvalidArgumentException $e) { + $this->assertEquals("Invalid time unit(s) specified: invalidUnit to minutes", $e->getMessage()); + } + + try { + convertTime(120, 'seconds', 'invalidUnit'); + } catch (InvalidArgumentException $e) { + $this->assertEquals("Invalid time unit(s) specified: seconds to invalidUnit", $e->getMessage()); + } + + try { + convertTime(120, 'invalidFrom', 'invalidTo'); + } catch (InvalidArgumentException $e) { + $this->assertEquals("Invalid time unit(s) specified: invalidFrom to invalidTo", $e->getMessage()); + } + } + + } From ebe917adbd53016192b04efbaa5483d830c9a2ee Mon Sep 17 00:00:00 2001 From: Arafat Hossain Ar Date: Wed, 30 Oct 2024 23:19:37 +0600 Subject: [PATCH 4/5] add: new conversations file added in the directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 30aa2c7f..ecfa0305 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -16,6 +16,7 @@ * [Octaltodecimal](./Conversions/OctalToDecimal.php) * [Speedconversion](./Conversions/SpeedConversion.php) * [Temperatureconversions](./Conversions/TemperatureConversions.php) + * [ConvertTime](./Conversions/ConvertTime.php) ## Datastructures * Avltree From 4f81e1528233a1c8a6f6c96ba79ed587f7873268 Mon Sep 17 00:00:00 2001 From: Arafat Hossain Ar Date: Sat, 16 Nov 2024 11:06:47 +0600 Subject: [PATCH 5/5] :up: Update: Updated test cases --- tests/Conversions/ConversionsTest.php | 42 ++++++++------------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/tests/Conversions/ConversionsTest.php b/tests/Conversions/ConversionsTest.php index b39dcebc..3ba91d0f 100644 --- a/tests/Conversions/ConversionsTest.php +++ b/tests/Conversions/ConversionsTest.php @@ -146,38 +146,20 @@ public function testFahrenheitToKelvin() public function testConvertTime() { - // Test basic conversions - $this->assertEquals(2.0, convertTime(120, 'seconds', 'minutes'), "Failed converting 120 seconds to minutes"); - $this->assertEquals(2.0, convertTime(120, 'minutes', 'hours'), "Failed converting 120 minutes to hours"); - $this->assertEquals(2.0, convertTime(48, 'hours', 'days'), "Failed converting 48 hours to days"); - $this->assertEquals(2.0, convertTime(14, 'days', 'weeks'), "Failed converting 14 days to weeks"); + $examples = [ + [120, 'minutes', 'seconds', 7200], + [48, 'hours', 'days', 2.0], + [14, 'days', 'weeks', 2.0], + [8.6, 'weeks', 'months', 2.0], + [24, 'months', 'years', 2.0], + [3600, 'seconds', 'hours', 1.0], + ]; - // Test with different units and values - $this->assertEqualsWithDelta(2.0, convertTime(8.6, 'weeks', 'months'), 0.01, "Failed converting 8.6 weeks to months"); - $this->assertEquals(2.0, convertTime(24, 'months', 'years'), "Failed converting 24 months to years"); - - // Test edge cases - $this->assertEquals(0.0, convertTime(0, 'seconds', 'hours'), "Failed converting 0 seconds to hours"); - - // Test invalid 'from' and 'to' units - try { - convertTime(120, 'invalidUnit', 'minutes'); - } catch (InvalidArgumentException $e) { - $this->assertEquals("Invalid time unit(s) specified: invalidUnit to minutes", $e->getMessage()); - } - - try { - convertTime(120, 'seconds', 'invalidUnit'); - } catch (InvalidArgumentException $e) { - $this->assertEquals("Invalid time unit(s) specified: seconds to invalidUnit", $e->getMessage()); - } - - try { - convertTime(120, 'invalidFrom', 'invalidTo'); - } catch (InvalidArgumentException $e) { - $this->assertEquals("Invalid time unit(s) specified: invalidFrom to invalidTo", $e->getMessage()); + foreach ($examples as [$value, $from, $to, $expected]) { + $result = convertTime($value, $from, $to); + $this->assertEquals($expected, $result, "Failed converting $value $from to $to"); + echo "Example: $value $from = $result $to\n"; } } - }