From f66e67b665ff580fcd876fc4aca1e4d242412328 Mon Sep 17 00:00:00 2001 From: Jeel Gajera Date: Sat, 21 Oct 2023 13:30:31 +0530 Subject: [PATCH 1/8] Add: Time Conversion Function --- DIRECTORY.md | 1 + conversions/time_conversions.py | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 conversions/time_conversions.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e0166ad80c5..894ad9b62ad7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -174,6 +174,7 @@ * [Roman Numerals](conversions/roman_numerals.py) * [Speed Conversions](conversions/speed_conversions.py) * [Temperature Conversions](conversions/temperature_conversions.py) + * [Time Conversions](conversions/time_conversions.py) * [Volume Conversions](conversions/volume_conversions.py) * [Weight Conversion](conversions/weight_conversion.py) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py new file mode 100644 index 000000000000..613f4db600df --- /dev/null +++ b/conversions/time_conversions.py @@ -0,0 +1,54 @@ +""" +A unit of time is any particular time interval, used as a standard +way of measuring or expressing duration. +The base unit of time in the International System of Units (SI), +and by extension most of the Western world, is the second, +defined as about 9 billion oscillations of the caesium atom. + +WIKI: https://en.wikipedia.org/wiki/Unit_of_time +""" + +time_chart: dict[str, float] = { + "seconds": 1.0, + "minutes": 60.0, # 1 minute = 60 sec + "hours": 3600.0, # 1 hour = 60 minutes = 3600 seconds + "days": 86400.0, # 1 day = 24 hours = 1440 min = 86400 sec + "weeks": 604800.0, # 1 week=7d=168hr=10080min = 604800 sec +} + +time_chart_inverse: dict[str, float] = { + "seconds": 1.0, + "minutes": 1 / 60.0, # 1 minute = 1/60 hours + "hours": 1 / 3600.0, # 1 hour = 1/3600 days + "days": 1 / 86400.0, # 1 day = 1/86400 weeks + "weeks": 1 / 604800.0, # 1 week = 1/604800 seconds +} + +def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: + """ + Convert time from one unit to another using the time_chart above. + + >>> convert_time(3600, "seconds", "hours") + 1.0 + >>> convert_time(1, "days", "hours") + 24.0 + >>> convert_time(120, "minutes", "seconds") + 7200.0 + >>> convert_time(2, "weeks", "days") + 14.0 + >>> convert_time(0.5, "hours", "minutes") + 30.0 + """ + if unit_to not in time_chart or unit_from not in time_chart_inverse: + msg = ( + f"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n" + f"Valid values are: {', '.join(time_chart_inverse)}" + ) + raise ValueError(msg) + return round(time_value * time_chart[unit_from] * time_chart_inverse[unit_to], 3) + +if __name__ == "__main__": + import doctest + doctest.testmod() + + print(convert_time(3600, "seconds", "hours")) From b516a4b9a9bf95e5f08d03e3560c5e3018d2bf98 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Oct 2023 08:03:18 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/time_conversions.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index 613f4db600df..2be70ad3e162 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -1,9 +1,9 @@ """ -A unit of time is any particular time interval, used as a standard -way of measuring or expressing duration. +A unit of time is any particular time interval, used as a standard +way of measuring or expressing duration. The base unit of time in the International System of Units (SI), and by extension most of the Western world, is the second, -defined as about 9 billion oscillations of the caesium atom. +defined as about 9 billion oscillations of the caesium atom. WIKI: https://en.wikipedia.org/wiki/Unit_of_time """ @@ -24,6 +24,7 @@ "weeks": 1 / 604800.0, # 1 week = 1/604800 seconds } + def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: """ Convert time from one unit to another using the time_chart above. @@ -47,8 +48,10 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: raise ValueError(msg) return round(time_value * time_chart[unit_from] * time_chart_inverse[unit_to], 3) + if __name__ == "__main__": import doctest + doctest.testmod() print(convert_time(3600, "seconds", "hours")) From 2bc12893c0b9bd5455cea051770c33f6256789fb Mon Sep 17 00:00:00 2001 From: Jeel Gajera <83470656+JeelGajera@users.noreply.github.com> Date: Sun, 22 Oct 2023 13:25:37 +0530 Subject: [PATCH 3/8] Update conversions/time_conversions.py Co-authored-by: Christian Clauss --- conversions/time_conversions.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index 2be70ad3e162..19492a9bc671 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -1,11 +1,10 @@ """ -A unit of time is any particular time interval, used as a standard -way of measuring or expressing duration. -The base unit of time in the International System of Units (SI), -and by extension most of the Western world, is the second, -defined as about 9 billion oscillations of the caesium atom. +A unit of time is any particular time interval, used as a standard way of measuring or +expressing duration. The base unit of time in the International System of Units (SI), +and by extension most of the Western world, is the second, defined as about 9 billion +oscillations of the caesium atom. -WIKI: https://en.wikipedia.org/wiki/Unit_of_time +https://en.wikipedia.org/wiki/Unit_of_time """ time_chart: dict[str, float] = { From dc5f3ca94abeb775fd3863ec781d0e3ad5f8173e Mon Sep 17 00:00:00 2001 From: Jeel Gajera Date: Sun, 22 Oct 2023 14:05:13 +0530 Subject: [PATCH 4/8] fix: required changes --- conversions/time_conversions.py | 47 ++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index 19492a9bc671..c975f6d67719 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -13,15 +13,12 @@ "hours": 3600.0, # 1 hour = 60 minutes = 3600 seconds "days": 86400.0, # 1 day = 24 hours = 1440 min = 86400 sec "weeks": 604800.0, # 1 week=7d=168hr=10080min = 604800 sec + "months": 2629800.0, # Approximate value for a month in seconds + "years": 31557600.0, # Approximate value for a year in seconds } -time_chart_inverse: dict[str, float] = { - "seconds": 1.0, - "minutes": 1 / 60.0, # 1 minute = 1/60 hours - "hours": 1 / 3600.0, # 1 hour = 1/3600 days - "days": 1 / 86400.0, # 1 day = 1/86400 weeks - "weeks": 1 / 604800.0, # 1 week = 1/604800 seconds -} +time_chart_inverse: dict[str, float] = {key: 1 / value for key, + value in time_chart.items()} def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: @@ -38,14 +35,30 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: 14.0 >>> convert_time(0.5, "hours", "minutes") 30.0 + >>> convert_time(-3600, "seconds", "hours") + Error: 'time_value' must be a non-negative number. + >>> convert_time("Hello", "hours", "minutes") + Error: 'time_value' must be a non-negative number. + >>> convert_time([0, 1, 2], "weeks", "days") + Error: 'time_value' must be a non-negative number. + >>> convert_time(50, "HOURS", "days") + 2.083 + >>> convert_time(50, "hours", "YEARS") + 0.006 """ - if unit_to not in time_chart or unit_from not in time_chart_inverse: - msg = ( - f"Incorrect 'from_type' or 'to_type' value: {unit_from!r}, {unit_to!r}\n" - f"Valid values are: {', '.join(time_chart_inverse)}" - ) - raise ValueError(msg) - return round(time_value * time_chart[unit_from] * time_chart_inverse[unit_to], 3) + if unit_to.lower() not in time_chart or unit_from.lower() not in time_chart_inverse: + valid_units = ", ".join(time_chart_inverse) + print( + f"Error: Invalid unit: {unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." + f" Valid units are {valid_units}.") + + return None + + if not isinstance(time_value, (int, float)) or time_value < 0: + print("Error: 'time_value' must be a non-negative number.") + return None + return round(time_value * time_chart[unit_from.lower()] * + time_chart_inverse[unit_to.lower()], 3) if __name__ == "__main__": @@ -53,4 +66,8 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: doctest.testmod() - print(convert_time(3600, "seconds", "hours")) + print(f"{convert_time(3600,'seconds', 'hours') = }") + print(f"{convert_time(360, 'days', 'months') = }") + print(f"{convert_time(360, 'months', 'years') = }") + print(f"{convert_time(360, 'cool', 'months') = }") + \ No newline at end of file From 3b52b58d2b6274d15bab9152ceab1d2cada2cd0e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 08:35:55 +0000 Subject: [PATCH 5/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/time_conversions.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index c975f6d67719..7eecbcbf70dd 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -17,8 +17,9 @@ "years": 31557600.0, # Approximate value for a year in seconds } -time_chart_inverse: dict[str, float] = {key: 1 / value for key, - value in time_chart.items()} +time_chart_inverse: dict[str, float] = { + key: 1 / value for key, value in time_chart.items() +} def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: @@ -49,16 +50,21 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: if unit_to.lower() not in time_chart or unit_from.lower() not in time_chart_inverse: valid_units = ", ".join(time_chart_inverse) print( - f"Error: Invalid unit: {unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." - f" Valid units are {valid_units}.") + f"Error: Invalid unit: {unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." + f" Valid units are {valid_units}." + ) return None if not isinstance(time_value, (int, float)) or time_value < 0: print("Error: 'time_value' must be a non-negative number.") return None - return round(time_value * time_chart[unit_from.lower()] * - time_chart_inverse[unit_to.lower()], 3) + return round( + time_value + * time_chart[unit_from.lower()] + * time_chart_inverse[unit_to.lower()], + 3, + ) if __name__ == "__main__": @@ -70,4 +76,3 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: print(f"{convert_time(360, 'days', 'months') = }") print(f"{convert_time(360, 'months', 'years') = }") print(f"{convert_time(360, 'cool', 'months') = }") - \ No newline at end of file From c83d39b2258edcdafc1aa51a2bebeb75fae46e99 Mon Sep 17 00:00:00 2001 From: Jeel Gajera Date: Sun, 22 Oct 2023 14:09:48 +0530 Subject: [PATCH 6/8] fix: err --- conversions/time_conversions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index 7eecbcbf70dd..bb101d2c9c17 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -50,9 +50,9 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: if unit_to.lower() not in time_chart or unit_from.lower() not in time_chart_inverse: valid_units = ", ".join(time_chart_inverse) print( - f"Error: Invalid unit: {unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." - f" Valid units are {valid_units}." - ) + f"Error: Invalid unit: " + f"{unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." + f" Valid units are {valid_units}.") return None From 0c6465d13eebf7839313bfae5983df16ad96790d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Oct 2023 08:40:30 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/time_conversions.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index bb101d2c9c17..74037293c3b9 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -50,9 +50,10 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: if unit_to.lower() not in time_chart or unit_from.lower() not in time_chart_inverse: valid_units = ", ".join(time_chart_inverse) print( - f"Error: Invalid unit: " - f"{unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." - f" Valid units are {valid_units}.") + f"Error: Invalid unit: " + f"{unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." + f" Valid units are {valid_units}." + ) return None From 9a7ce0eafb61e598b9725227696abf1ff1e5ac1d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 15:47:56 +0200 Subject: [PATCH 8/8] Update time_conversions.py --- conversions/time_conversions.py | 67 ++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/conversions/time_conversions.py b/conversions/time_conversions.py index 74037293c3b9..8c30f5bc4a45 100644 --- a/conversions/time_conversions.py +++ b/conversions/time_conversions.py @@ -28,42 +28,50 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: >>> convert_time(3600, "seconds", "hours") 1.0 - >>> convert_time(1, "days", "hours") + >>> convert_time(3500, "Seconds", "Hours") + 0.972 + >>> convert_time(1, "DaYs", "hours") 24.0 - >>> convert_time(120, "minutes", "seconds") + >>> convert_time(120, "minutes", "SeCoNdS") 7200.0 - >>> convert_time(2, "weeks", "days") + >>> convert_time(2, "WEEKS", "days") 14.0 - >>> convert_time(0.5, "hours", "minutes") + >>> convert_time(0.5, "hours", "MINUTES") 30.0 >>> convert_time(-3600, "seconds", "hours") - Error: 'time_value' must be a non-negative number. + Traceback (most recent call last): + ... + ValueError: 'time_value' must be a non-negative number. >>> convert_time("Hello", "hours", "minutes") - Error: 'time_value' must be a non-negative number. + Traceback (most recent call last): + ... + ValueError: 'time_value' must be a non-negative number. >>> convert_time([0, 1, 2], "weeks", "days") - Error: 'time_value' must be a non-negative number. - >>> convert_time(50, "HOURS", "days") - 2.083 - >>> convert_time(50, "hours", "YEARS") - 0.006 + Traceback (most recent call last): + ... + ValueError: 'time_value' must be a non-negative number. + >>> convert_time(1, "cool", "century") # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: Invalid unit cool is not in seconds, minutes, hours, days, weeks, ... + >>> convert_time(1, "seconds", "hot") # doctest: +ELLIPSIS + Traceback (most recent call last): + ... + ValueError: Invalid unit hot is not in seconds, minutes, hours, days, weeks, ... """ - if unit_to.lower() not in time_chart or unit_from.lower() not in time_chart_inverse: - valid_units = ", ".join(time_chart_inverse) - print( - f"Error: Invalid unit: " - f"{unit_from if unit_from.lower() not in time_chart_inverse else unit_to}." - f" Valid units are {valid_units}." - ) + if not isinstance(time_value, (int, float)) or time_value < 0: + msg = "'time_value' must be a non-negative number." + raise ValueError(msg) - return None + unit_from = unit_from.lower() + unit_to = unit_to.lower() + if unit_from not in time_chart or unit_to not in time_chart: + invalid_unit = unit_from if unit_from not in time_chart else unit_to + msg = f"Invalid unit {invalid_unit} is not in {', '.join(time_chart)}." + raise ValueError(msg) - if not isinstance(time_value, (int, float)) or time_value < 0: - print("Error: 'time_value' must be a non-negative number.") - return None return round( - time_value - * time_chart[unit_from.lower()] - * time_chart_inverse[unit_to.lower()], + time_value * time_chart[unit_from] * time_chart_inverse[unit_to], 3, ) @@ -72,8 +80,7 @@ def convert_time(time_value: float, unit_from: str, unit_to: str) -> float: import doctest doctest.testmod() - - print(f"{convert_time(3600,'seconds', 'hours') = }") - print(f"{convert_time(360, 'days', 'months') = }") - print(f"{convert_time(360, 'months', 'years') = }") - print(f"{convert_time(360, 'cool', 'months') = }") + print(f"{convert_time(3600,'seconds', 'hours') = :,}") + print(f"{convert_time(360, 'days', 'months') = :,}") + print(f"{convert_time(360, 'months', 'years') = :,}") + print(f"{convert_time(1, 'years', 'seconds') = :,}")