-
Notifications
You must be signed in to change notification settings - Fork 1.6k
filter: add new time_delta filter #9831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
9700872
to
7b5c05f
Compare
This comment was marked as outdated.
This comment was marked as outdated.
nice |
* Calculate datetime string based upon time delta provided by user Signed-off-by: Abhijeet Kasurde <[email protected]>
Whilst I appreciate the idea very much, the name Additionally, I think forcing this to use string representation is limiting. We can have datetime objects and time delta objects, as shown in: Therefore, I reckon it would be interesting to:
I mean those in addition to the current code, which is clearly quite useful. WDYT? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! I agree with @russoz that the name should be adjusted; I was thinking as well that this filter allows to compute the difference between two timestamps, and not to modify a timestamp.
raise AnsibleFilterError('time_delta accepts datetime in string format') | ||
date_format = "%Y-%m-%d %H:%M:%S" | ||
if 'date_format' in kwargs: | ||
date_format = kwargs.get('date_format') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be validated to be a string.
'minutes': kwargs.get('minutes', 0), | ||
'hours': kwargs.get('hours', 0), | ||
'weeks': kwargs.get('weeks', 0), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to validate the above to be integers if supplied as options. Also you should check whether kwargs
has more arguments than the ones we use (and fail if there are more).
f'Failed to parse provided string into datetime format "{date_format}" provided.' | ||
) | ||
|
||
return str(source_date + timedelta(**delta)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't date_format
be used to format the timestamp?
A few thought on this: ---
- gather_facts: no
hosts: localhost
connection: local
vars:
date1: '00:00'
date2: '01:00'
fmt: '%H:%M'
timedelta: "{{ (date2 | to_datetime(fmt)) - (date1 | to_datetime(fmt)) }}" # this is coerced to string, you can't "store" your timedelta
tasks:
- debug:
var: now() + ((date2 | to_datetime(fmt)) - (date1 | to_datetime(fmt))) IMO, instead of a filter applying a computed timedelta on a datetime, it would be better to have a way to directly construct a timedelta. This allows more things, like comparing timedeltas, doing arithemic on it etc (example usecase: checking if remaining validity time of a certificate is greater than 20% of it's total duration, that sort of thing) I don't think there is a way to add jinja functions as extension (is there ?), but maybe a filter passing a dict to the timedelta constructor would do it. ---
- gather_facts: no
hosts: localhost
connection: local
tasks:
- debug:
var: "now() + ({'hours': 1} | community.general.timedelta)" # NOT IMPLEMENTED |
SUMMARY
provided by user
Signed-off-by: Abhijeet Kasurde [email protected]
ISSUE TYPE