summaryrefslogtreecommitdiffstats
path: root/lib/ansible/module_utils/compat/datetime.py
blob: d3cdc0d3d383e19ba47a0ee0ae52b2ee506b81dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Copyright (c) 2023 Ansible
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

from __future__ import annotations

from ansible.module_utils.six import PY3

import datetime


if PY3:
    UTC = datetime.timezone.utc
else:
    _ZERO = datetime.timedelta(0)

    class _UTC(datetime.tzinfo):
        __slots__ = ()

        def utcoffset(self, dt):
            return _ZERO

        def dst(self, dt):
            return _ZERO

        def tzname(self, dt):
            return "UTC"

    UTC = _UTC()


def utcfromtimestamp(timestamp):  # type: (float) -> datetime.datetime
    """Construct an aware UTC datetime from a POSIX timestamp."""
    return datetime.datetime.fromtimestamp(timestamp, UTC)


def utcnow():  # type: () -> datetime.datetime
    """Construct an aware UTC datetime from time.time()."""
    return datetime.datetime.now(UTC)