Test Failed
Push — master ( f06717...a2792c )
by Tomaz
01:48 queued 10s
created

st2common/st2common/expressions/functions/time.py (1 issue)

1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
from __future__ import absolute_import
17
18
import six
19
20
import datetime
21
22
__all__ = [
23
    'to_human_time_from_seconds'
24
]
25
26
if six.PY3:
27
    long_int = int
28
else:
29
    long_int = long  # NOQA
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'long'
Loading history...
30
31
32
def to_human_time_from_seconds(seconds):
33
    """
34
    Given a time value in seconds, this function returns
35
    a fuzzy version like 3m5s.
36
37
    :param time_seconds: Time specified in seconds.
38
    :type time_seconds: ``int`` or ``long`` or ``float``
39
40
    :rtype: ``str``
41
    """
42
    assert (isinstance(seconds, int) or isinstance(seconds, int) or
43
            isinstance(seconds, float))
44
45
    return _get_human_time(seconds)
46
47
48
def _get_human_time(seconds):
49
    """
50
    Takes number of seconds as input and returns a string of form '1h3m5s'.
51
52
    :param seconds: Number of seconds.
53
    :type seconds: ``int`` or ``long`` or ``float``
54
55
    :rtype: ``str``
56
    """
57
58
    if seconds is None:
59
        return None
60
61
    if seconds == 0:
62
        return '0s'
63
64
    if seconds < 1:
65
        return '%s\u03BCs' % seconds  # Microseconds
66
67
    if isinstance(seconds, float):
68
        seconds = long_int(round(seconds))  # Let's lose microseconds.
69
70
    timedelta = datetime.timedelta(seconds=seconds)
71
    offset_date = datetime.datetime(1, 1, 1) + timedelta
72
73
    years = offset_date.year - 1
74
    days = offset_date.day - 1
75
    hours = offset_date.hour
76
    mins = offset_date.minute
77
    secs = offset_date.second
78
79
    time_parts = [years, days, hours, mins, secs]
80
81
    first_non_zero_pos = next((i for i, x in enumerate(time_parts) if x), None)
82
83
    if first_non_zero_pos is None:
84
        return '0s'
85
    else:
86
        time_parts = time_parts[first_non_zero_pos:]
87
88
    if len(time_parts) == 1:
89
        return '%ss' % tuple(time_parts)
90
    elif len(time_parts) == 2:
91
        return '%sm%ss' % tuple(time_parts)
92
    elif len(time_parts) == 3:
93
        return '%sh%sm%ss' % tuple(time_parts)
94
    elif len(time_parts) == 4:
95
        return '%sd%sh%sm%ss' % tuple(time_parts)
96
    elif len(time_parts) == 5:
97
        return '%sy%sd%sh%sm%ss' % tuple(time_parts)
98