From dd9afc09a8bab0b8482a71104954182897ef0218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Wed, 4 May 2022 07:38:16 +0200 Subject: [PATCH] time_filter: allow 0 to return 0 (#4612) * time_filter: allow 0 to return 0 * add changelog * Update changelogs/fragments/4612-time_filter_zero.yml Co-authored-by: Felix Fontein Co-authored-by: Felix Fontein --- changelogs/fragments/4612-time_filter_zero.yml | 2 ++ plugins/filter/time.py | 5 +++++ tests/integration/targets/filter_time/tasks/main.yml | 7 +++++++ 3 files changed, 14 insertions(+) create mode 100644 changelogs/fragments/4612-time_filter_zero.yml diff --git a/changelogs/fragments/4612-time_filter_zero.yml b/changelogs/fragments/4612-time_filter_zero.yml new file mode 100644 index 0000000000..8129725055 --- /dev/null +++ b/changelogs/fragments/4612-time_filter_zero.yml @@ -0,0 +1,2 @@ +minor_changes: + - to_time_unit filter plugins - the time filters has been extended to also allow ``0`` as input (https://github.com/ansible-collections/community.general/pull/4612). diff --git a/plugins/filter/time.py b/plugins/filter/time.py index 3b44ad0e49..f069780fe7 100644 --- a/plugins/filter/time.py +++ b/plugins/filter/time.py @@ -46,6 +46,11 @@ def multiply(factors): def to_time_unit(human_time, unit='ms', **kwargs): ''' Return a time unit from a human readable string ''' + + # No need to handle 0 + if human_time == "0": + return 0 + unit_to_short_form = UNIT_TO_SHORT_FORM unit_factors = UNIT_FACTORS diff --git a/tests/integration/targets/filter_time/tasks/main.yml b/tests/integration/targets/filter_time/tasks/main.yml index b3e8a55d14..7f0d092f34 100644 --- a/tests/integration/targets/filter_time/tasks/main.yml +++ b/tests/integration/targets/filter_time/tasks/main.yml @@ -4,6 +4,13 @@ # and should not be used as examples of how to write Ansible roles # #################################################################### +- name: test zero is 0 + assert: + that: + - "('0' | community.general.to_milliseconds) == 0" + - "('0' | community.general.to_seconds) == 0" + - "('0' | community.general.to_minutes) == 0" + - name: test to_milliseconds filter assert: that: