Add epoch_int in date_time facts (#73822)

* Add unit test

* Add changelog
pull/74317/head
Amin Vakil 4 years ago committed by GitHub
parent 8d027436d4
commit 0c101f3f76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
---
minor_changes:
- setup - add ``epoch_int`` option to date_time facts (https://github.com/ansible/ansible/pull/73822).

@ -47,8 +47,13 @@ class DateTimeFactCollector(BaseFactCollector):
date_time_facts['minute'] = now.strftime('%M') date_time_facts['minute'] = now.strftime('%M')
date_time_facts['second'] = now.strftime('%S') date_time_facts['second'] = now.strftime('%S')
date_time_facts['epoch'] = now.strftime('%s') date_time_facts['epoch'] = now.strftime('%s')
# epoch returns float or string in some non-linux environments
if date_time_facts['epoch'] == '' or date_time_facts['epoch'][0] == '%': if date_time_facts['epoch'] == '' or date_time_facts['epoch'][0] == '%':
date_time_facts['epoch'] = str(int(epoch_ts)) date_time_facts['epoch'] = str(int(epoch_ts))
# epoch_int always returns integer format of epoch
date_time_facts['epoch_int'] = str(int(now.strftime('%s')))
if date_time_facts['epoch_int'] == '' or date_time_facts['epoch_int'][0] == '%':
date_time_facts['epoch_int'] = str(int(epoch_ts))
date_time_facts['date'] = now.strftime('%Y-%m-%d') date_time_facts['date'] = now.strftime('%Y-%m-%d')
date_time_facts['time'] = now.strftime('%H:%M:%S') date_time_facts['time'] = now.strftime('%H:%M:%S')
date_time_facts['iso8601_micro'] = utcnow.strftime("%Y-%m-%dT%H:%M:%S.%fZ") date_time_facts['iso8601_micro'] = utcnow.strftime("%Y-%m-%dT%H:%M:%S.%fZ")

@ -79,6 +79,8 @@ def test_date_time_epoch(fake_date_facts):
assert fake_date_facts['date_time']['epoch'].isdigit() assert fake_date_facts['date_time']['epoch'].isdigit()
assert len(fake_date_facts['date_time']['epoch']) == 10 # This length will not change any time soon assert len(fake_date_facts['date_time']['epoch']) == 10 # This length will not change any time soon
assert fake_date_facts['date_time']['epoch_int'].isdigit()
assert len(fake_date_facts['date_time']['epoch_int']) == 10 # This length will not change any time soon
@pytest.mark.parametrize('fact_name', ('tz', 'tz_dst')) @pytest.mark.parametrize('fact_name', ('tz', 'tz_dst'))

Loading…
Cancel
Save