Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConvertTime Class with Time Conversion Functionality and Unit Tests #177

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions Conversions/ConvertTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* Converts a value from one time unit to another.
*
* @author Arafat Hossain
* @link https://github.com/arafat-web
* @param float $value The value to convert.
* @param string $fromUnit The current unit of the value.
* @param string $toUnit The target unit to convert the value to.
* @throws InvalidArgumentException If the fromUnit or toUnit is invalid.
* @return float The converted value, rounded to two decimal places.
*/
function convertTime(float $value, string $fromUnit, string $toUnit): float
{
$conversionFactors = [
'seconds' => [
'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";
}
Comment on lines +85 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep examples like this in the test code, with assertions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [Octaltodecimal](./Conversions/OctalToDecimal.php)
* [Speedconversion](./Conversions/SpeedConversion.php)
* [Temperatureconversions](./Conversions/TemperatureConversions.php)
* [ConvertTime](./Conversions/ConvertTime.php)

## Datastructures
* Avltree
Expand Down
38 changes: 38 additions & 0 deletions tests/Conversions/ConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}


}
Loading