Add a new filter: strftime. Use the well known function to format a date output. (#23832)

(cherry picked from commit 3f5b304fc28b6c34df9ea6a4c3531dc422ce198b)
rebased for @yannig
This commit is contained in:
Brian Coca 2017-04-21 14:48:45 -04:00 committed by scottb
commit 3358abcf49
2 changed files with 33 additions and 1 deletions

View file

@ -30,13 +30,15 @@ import os.path
import re
import string
import sys
import time
import uuid
import yaml
from collections import MutableMapping, MutableSequence
from datetime import datetime
from functools import partial
from random import Random, SystemRandom, shuffle
import yaml
from jinja2.filters import environmentfilter, do_groupby as _do_groupby
try:
@ -121,6 +123,15 @@ def to_datetime(string, format="%Y-%d-%m %H:%M:%S"):
return datetime.strptime(string, format)
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)
except:
raise errors.AnsibleFilterError('Invalid value for epoch value (%s)' % second)
return time.strftime(string_format, time.localtime(second))
def quote(a):
''' return its argument quoted for shell usage '''
return shlex_quote(a)
@ -510,6 +521,9 @@ class FilterModule(object):
# value as boolean
'bool': to_bool,
# date formating
'strftime': strftime,
# quote string for shell usage
'quote': quote,