1"""
2Convenience functions for dealing with datetime classes
3"""
4
5
6import datetime
7
8import salt.utils.stringutils
9from salt.utils.decorators.jinja import jinja_filter
10
11try:
12    import timelib
13
14    HAS_TIMELIB = True
15except ImportError:
16    HAS_TIMELIB = False
17
18
19def date_cast(date):
20    """
21    Casts any object into a datetime.datetime object
22
23    date
24      any datetime, time string representation...
25    """
26    if date is None:
27        return datetime.datetime.now()
28    elif isinstance(date, datetime.datetime):
29        return date
30
31    # fuzzy date
32    try:
33        if isinstance(date, str):
34            try:
35                if HAS_TIMELIB:
36                    # py3: yes, timelib.strtodatetime wants bytes, not str :/
37                    return timelib.strtodatetime(salt.utils.stringutils.to_bytes(date))
38            except ValueError:
39                pass
40
41            # not parsed yet, obviously a timestamp?
42            if date.isdigit():
43                date = int(date)
44            else:
45                date = float(date)
46
47        return datetime.datetime.fromtimestamp(date)
48    except Exception:  # pylint: disable=broad-except
49        if HAS_TIMELIB:
50            raise ValueError("Unable to parse {}".format(date))
51
52        raise RuntimeError(
53            "Unable to parse {}. Consider installing timelib".format(date)
54        )
55
56
57@jinja_filter("date_format")
58@jinja_filter("strftime")
59def strftime(date=None, format="%Y-%m-%d"):
60    """
61    Converts date into a time-based string
62
63    date
64      any datetime, time string representation...
65
66    format
67       :ref:`strftime<http://docs.python.org/2/library/datetime.html#datetime.datetime.strftime>` format
68
69    >>> import datetime
70    >>> src = datetime.datetime(2002, 12, 25, 12, 00, 00, 00)
71    >>> strftime(src)
72    '2002-12-25'
73    >>> src = '2002/12/25'
74    >>> strftime(src)
75    '2002-12-25'
76    >>> src = 1040814000
77    >>> strftime(src)
78    '2002-12-25'
79    >>> src = '1040814000'
80    >>> strftime(src)
81    '2002-12-25'
82    """
83    return date_cast(date).strftime(format)
84
85
86def total_seconds(td):
87    """
88    Takes a timedelta and returns the total number of seconds
89    represented by the object. Wrapper for the total_seconds()
90    method which does not exist in versions of Python < 2.7.
91    """
92    return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 10 ** 6
93