|
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' |
|
20
|
|
|
] |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def to_human_time(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 |
|
|
|
|
|
|
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
|
|
|
|