Completed
Pull Request — master (#2849)
by Lakshmi
06:22
created

_get_human_time()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
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
import datetime
17
18
__all__ = [
19
    'to_human_time_from_seconds'
20
]
21
22
23
def to_human_time_from_seconds(time_seconds):
24
    """
25
    Given a time value in seconds, this function returns
26
    a fuzzy version like 3m5s.
27
28
    :param time_seconds: Time specified in seconds.
29
    :type time_seconds: ``int`` or ``long``
30
31
    :rtype: ``str``
32
    """
33
    assert time_seconds is int or time_seconds is long
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'long'
Loading history...
34
    timedelta_str = datetime.timedelta(seconds=time_seconds)  # Returns 'hh:mm:ss'
35
    return _get_human_time(timedelta_str)
36
37
38
def _get_human_time(timedelta_str):
39
    """
40
    Takes a timedelta string of form '01:03:05' and returns a string
41
    of form '1h3m5s'.
42
43
    :param timedelta_str: Timedelta in string format.
44
    :type timedelta_str: ``str``
45
46
    :rtype: ``str``
47
    """
48
    time_parts = timedelta_str.split(':')
49
    time_parts = [part.lstrip("0") for part in time_parts]
50
    return '%sh%sm%ss' % (tuple(time_parts))
51