Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* args.py: arguments with characters outside of ASCII arrive intact when the Icinga 2 agent for Windows starts the consumer
* base.py: on Windows, what argparse writes to stderr no longer arrives with an empty line after every line
* disk.py: `is_within()` with the filesystem root as a root
* human.py: `human2seconds()` ([#409](https://github.com/Linuxfabrik/lib/issues/409))
* logsource.py: `read()` names missing journal rights instead of passing on journalctl's hint
* net.py: `FAMILIYSTR` names an IPv6 address "6" instead of "irda" on Windows
* powershell.py, shell.py: `run_ps()` and `shell_exec()` return on time on Windows after a timeout, even when a child process keeps the output open
Expand Down
3 changes: 2 additions & 1 deletion human.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@ def human2seconds(string):
except (ValueError, TypeError):
return 0

return int(value * _UNIT_TO_SECONDS[unit])
seconds = value * _UNIT_TO_SECONDS[unit]
return int(seconds) if math.isfinite(seconds) else 0


def humanduration2seconds(text):
Expand Down
10 changes: 10 additions & 0 deletions tests/human/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ class TestHuman2Bytes(unittest.TestCase):


class TestHuman2Seconds(unittest.TestCase):
def test_nonfinite_durations_return_zero(self):
for value in ('nans', 'NaNs', '+infs', '-infs', '1e309s', '1e308Y'):
with self.subTest(value=value):
self.assertEqual(human.human2seconds(value), 0)

def test_finite_durations_keep_truncation(self):
self.assertEqual(human.human2seconds('-2.5h'), -9000)
self.assertEqual(human.human2seconds('0.5s'), 0)
self.assertEqual(human.human2seconds('1e300s'), int(1e300))

def test_units(self):
self.assertEqual(human.human2seconds('1h'), 3600)
self.assertEqual(human.human2seconds('1d'), 86400)
Expand Down
Loading