From 858a3dbaadcad17b413bec913f589b27ca8364c2 Mon Sep 17 00:00:00 2001 From: Klaas Demter Date: Mon, 9 Oct 2023 17:55:06 +0200 Subject: [PATCH] Fix example in strftime filter plugin help (#81696) * Move the timestamp example to to_datetime.yml and be more verbose in its description --- lib/ansible/plugins/filter/strftime.yml | 8 -------- lib/ansible/plugins/filter/to_datetime.yml | 10 ++++++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/ansible/plugins/filter/strftime.yml b/lib/ansible/plugins/filter/strftime.yml index a1d8b9215d4..8788e359f11 100644 --- a/lib/ansible/plugins/filter/strftime.yml +++ b/lib/ansible/plugins/filter/strftime.yml @@ -41,14 +41,6 @@ EXAMPLES: | {{ '%Y-%m-%d' | strftime(0) }} # => 1970-01-01 {{ '%Y-%m-%d' | strftime(1441357287) }} # => 2015-09-04 - # complex examples - vars: - date1: '2022-11-15T03:23:13.686956868Z' - date2: '2021-12-15T16:06:24.400087Z' - date_short: '{{ date1|regex_replace("([^.]+)(\.\d{6})(\d*)(.+)", "\1\2\4") }}' #shorten microseconds - iso8601format: '%Y-%m-%dT%H:%M:%S.%fZ' - date_diff_isoed: '{{ (date1|to_datetime(isoformat) - date2|to_datetime(isoformat)).total_seconds() }}' - RETURN: _value: description: A formatted date/time string. diff --git a/lib/ansible/plugins/filter/to_datetime.yml b/lib/ansible/plugins/filter/to_datetime.yml index dbd476a1d82..8ed764a2417 100644 --- a/lib/ansible/plugins/filter/to_datetime.yml +++ b/lib/ansible/plugins/filter/to_datetime.yml @@ -29,6 +29,16 @@ EXAMPLES: | # get amount of days between two dates. This returns only number of days and discards remaining hours, minutes, and seconds {{ (("2016-08-14 20:00:12" | to_datetime) - ("2015-12-25" | to_datetime('%Y-%m-%d'))).days }} + # difference between to dotnet (100ns precision) and iso8601 microsecond timestamps + # the date1_short regex replace will work for any timestamp that has a higher than microsecond precision + # by cutting off anything more precise than microseconds + vars: + date1: '2022-11-15T03:23:13.6869568Z' + date2: '2021-12-15T16:06:24.400087Z' + date1_short: '{{ date1|regex_replace("([^.]+)(\.\d{6})(\d*)(.+)", "\1\2\4") }}' # shorten to microseconds + iso8601format: '%Y-%m-%dT%H:%M:%S.%fZ' + date_diff_isoed: '{{ (date1_short|to_datetime(iso8601format) - date2|to_datetime(iso8601format)).total_seconds() }}' + RETURN: _value: description: C(datetime) object from the represented value.