Allow strftime to display time in UTC (#70351)

pull/77544/head
Will Thames 4 years ago committed by GitHub
parent a0dede5458
commit c3a3bc1d0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- Provide a `utc` option for strftime to show time in UTC rather than local time

@ -2117,6 +2117,13 @@ To format a date using a string (like with the shell date command), use the "str
{{ '%Y-%m-%d' | strftime(0) }} # => 1970-01-01
{{ '%Y-%m-%d' | strftime(1441357287) }} # => 2015-09-04
.. versionadded:: 2.13
strftime takes an optional utc argument, defaulting to False, meaning times are in the local timezone::
{{ '%H:%M:%S' | strftime }} # time now in local timezone
{{ '%H:%M:%S' | strftime(utc=True) }} # time now in UTC
.. note:: To get all string possibilities, check https://docs.python.org/3/library/time.html#time.strftime
Getting Kubernetes resource names

@ -95,14 +95,18 @@ def to_datetime(string, format="%Y-%m-%d %H:%M:%S"):
return datetime.datetime.strptime(string, format)
def strftime(string_format, second=None):
def strftime(string_format, second=None, utc=False):
''' return a date string using string. See https://docs.python.org/3/library/time.html#time.strftime for format '''
if utc:
timefn = time.gmtime
else:
timefn = time.localtime
if second is not None:
try:
second = float(second)
except Exception:
raise AnsibleFilterError('Invalid value for epoch value (%s)' % second)
return time.strftime(string_format, time.localtime(second))
return time.strftime(string_format, timefn(second))
def quote(a):

Loading…
Cancel
Save