diff --git a/changelogs/fragments/73822-date_time_facts_add_epoch_int.yaml b/changelogs/fragments/73822-date_time_facts_add_epoch_int.yaml new file mode 100644 index 00000000000..65d13b600a5 --- /dev/null +++ b/changelogs/fragments/73822-date_time_facts_add_epoch_int.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - setup - add ``epoch_int`` option to date_time facts (https://github.com/ansible/ansible/pull/73822). diff --git a/lib/ansible/module_utils/facts/system/date_time.py b/lib/ansible/module_utils/facts/system/date_time.py index e42ced97fa9..1661042456a 100644 --- a/lib/ansible/module_utils/facts/system/date_time.py +++ b/lib/ansible/module_utils/facts/system/date_time.py @@ -47,8 +47,13 @@ class DateTimeFactCollector(BaseFactCollector): date_time_facts['minute'] = now.strftime('%M') date_time_facts['second'] = 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] == '%': 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['time'] = now.strftime('%H:%M:%S') date_time_facts['iso8601_micro'] = utcnow.strftime("%Y-%m-%dT%H:%M:%S.%fZ") diff --git a/test/units/module_utils/facts/test_date_time.py b/test/units/module_utils/facts/test_date_time.py index b57f0efaa9f..6abc36a7ecf 100644 --- a/test/units/module_utils/facts/test_date_time.py +++ b/test/units/module_utils/facts/test_date_time.py @@ -79,6 +79,8 @@ def test_date_time_epoch(fake_date_facts): 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 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'))