From 6d1cc0dede726a9c61af6939f70b132741bf0d74 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 27 Aug 2020 12:35:21 -0500 Subject: [PATCH] [stable-2.10] epoch can be a float with strftime filter. Fixes #71257 (#71314) (#71319) (cherry picked from commit 6289570) Co-authored-by: Matt Martz --- changelogs/fragments/71257-strftime-float.yml | 3 +++ lib/ansible/plugins/filter/core.py | 2 +- test/integration/targets/filter_core/tasks/main.yml | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/71257-strftime-float.yml diff --git a/changelogs/fragments/71257-strftime-float.yml b/changelogs/fragments/71257-strftime-float.yml new file mode 100644 index 00000000000..d5e3bf46faf --- /dev/null +++ b/changelogs/fragments/71257-strftime-float.yml @@ -0,0 +1,3 @@ +bugfixes: +- strftime filter - Input epoch is allowed to be a float + (https://github.com/ansible/ansible/issues/71257) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 59a5df79122..99e9e0e3f59 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -102,7 +102,7 @@ def strftime(string_format, second=None): ''' return a date string using string. See https://docs.python.org/2/library/time.html#time.strftime for format ''' if second is not None: try: - second = int(second) + second = float(second) except Exception: raise AnsibleFilterError('Invalid value for epoch value (%s)' % second) return time.strftime(string_format, time.localtime(second)) diff --git a/test/integration/targets/filter_core/tasks/main.yml b/test/integration/targets/filter_core/tasks/main.yml index 791542816e2..2197febde91 100644 --- a/test/integration/targets/filter_core/tasks/main.yml +++ b/test/integration/targets/filter_core/tasks/main.yml @@ -298,6 +298,7 @@ assert: that: - '"%Y-%m-%d"|strftime(1585247522) == "2020-03-26"' + - '"%Y-%m-%d"|strftime("1585247522.0") == "2020-03-26"' - '("%Y"|strftime(None)).startswith("20")' # Current date, can't check much there. - strftime_fail is failed - '"Invalid value for epoch value" in strftime_fail.msg'