Skip to content
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

Add epoch_int in date_time facts #73822

Merged
merged 4 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/73822-date_time_facts_add_epoch_int.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- setup - add ``epoch_int`` option to date_time facts (https://github.com/ansible/ansible/pull/73822).
5 changes: 5 additions & 0 deletions lib/ansible/module_utils/facts/system/date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ def collect(self, module=None, collected_facts=None):
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')))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment explaining why this is different from the existing epoch value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR and ansible-collections/ansible.windows#123 were created because of this issue: #72479

Current epoch value returns float and string in some circumstances, epoch_int is guaranteed to be integer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I should have been more specific. Can you add a comment to the code explaining the possibility of the current epoch being a float? That will help anyone in the future looking at the code, since it's otherwise not obvious why we need both epoch and epoch_int.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d2c161d.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This PR is assigned to the 2.12 milestone, which we'll be going through after creating the stable-2.11 branch (which will be done when 2.11 RC1 comes out).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing!

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")
Expand Down
2 changes: 2 additions & 0 deletions test/units/module_utils/facts/test_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down