Debian Codegraph @

lib/ansible/module_utils/facts/system/date_time.py

raw
1 # Data and time related facts collection for ansible.
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import datetime
21 import time
22 import typing as t
23
24 from ansible.module_utils.facts.collector import BaseFactCollector
25
26
27 class DateTimeFactCollector(BaseFactCollector):
28 name = 'date_time'
29 _fact_ids = set() # type: t.Set[str]
30
31 def collect(self, module=None, collected_facts=None):
32 facts_dict = {}
33 date_time_facts = {}
34
35 # Store the timestamp once, then get local and UTC versions from that
36 epoch_ts = time.time()
37 now = datetime.datetime.fromtimestamp(epoch_ts)
38 utcnow = datetime.datetime.fromtimestamp(
39 epoch_ts,
40 tz=datetime.timezone.utc,
41 )
42
43 date_time_facts['year'] = now.strftime('%Y')
44 date_time_facts['month'] = now.strftime('%m')
45 date_time_facts['weekday'] = now.strftime('%A')
46 date_time_facts['weekday_number'] = now.strftime('%w')
47 date_time_facts['weeknumber'] = now.strftime('%W')
48 date_time_facts['day'] = now.strftime('%d')
49 date_time_facts['hour'] = now.strftime('%H')
50 date_time_facts['minute'] = now.strftime('%M')
51 date_time_facts['second'] = now.strftime('%S')
52 date_time_facts['epoch'] = now.strftime('%s')
53 # epoch returns float or string in some non-linux environments
54 if date_time_facts['epoch'] == '' or date_time_facts['epoch'][0] == '%':
55 date_time_facts['epoch'] = str(int(epoch_ts))
56 # epoch_int always returns integer format of epoch
57 date_time_facts['epoch_int'] = str(int(now.strftime('%s')))
58 if date_time_facts['epoch_int'] == '' or date_time_facts['epoch_int'][0] == '%':
59 date_time_facts['epoch_int'] = str(int(epoch_ts))
60 date_time_facts['date'] = now.strftime('%Y-%m-%d')
61 date_time_facts['time'] = now.strftime('%H:%M:%S')
62 date_time_facts['iso8601_micro'] = utcnow.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
63 date_time_facts['iso8601'] = utcnow.strftime("%Y-%m-%dT%H:%M:%SZ")
64 date_time_facts['iso8601_basic'] = now.strftime("%Y%m%dT%H%M%S%f")
65 date_time_facts['iso8601_basic_short'] = now.strftime("%Y%m%dT%H%M%S")
66 date_time_facts['tz'] = time.strftime("%Z")
67 date_time_facts['tz_dst'] = time.tzname[1]
68 date_time_facts['tz_offset'] = time.strftime("%z")
69
70 facts_dict['date_time'] = date_time_facts
71 return facts_dict