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(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`` or ``float`` |
30
|
|
|
|
31
|
|
|
:rtype: ``str`` |
32
|
|
|
""" |
33
|
|
|
assert (isinstance(seconds, int) or isinstance(seconds, long) or |
|
|
|
|
34
|
|
|
isinstance(seconds, float)) |
35
|
|
|
|
36
|
|
|
return _get_human_time(seconds) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def _get_human_time(seconds): |
40
|
|
|
""" |
41
|
|
|
Takes number of seconds as input and returns a string of form '1h3m5s'. |
42
|
|
|
|
43
|
|
|
:param seconds: Number of seconds. |
44
|
|
|
:type seconds: ``int`` or ``long`` or ``float`` |
45
|
|
|
|
46
|
|
|
:rtype: ``str`` |
47
|
|
|
""" |
48
|
|
|
|
49
|
|
|
if seconds is None: |
50
|
|
|
return None |
51
|
|
|
|
52
|
|
|
if seconds == 0: |
53
|
|
|
return '0s' |
54
|
|
|
|
55
|
|
|
if seconds < 1: |
56
|
|
|
return '%s\u03BCs' % seconds # Microseconds |
57
|
|
|
|
58
|
|
|
if isinstance(seconds, float): |
59
|
|
|
seconds = long(round(seconds)) # Let's lose microseconds. |
|
|
|
|
60
|
|
|
|
61
|
|
|
timedelta = datetime.timedelta(seconds=seconds) |
62
|
|
|
offset_date = datetime.datetime(1, 1, 1) + timedelta |
63
|
|
|
|
64
|
|
|
years = offset_date.year - 1 |
65
|
|
|
days = offset_date.day - 1 |
66
|
|
|
hours = offset_date.hour |
67
|
|
|
mins = offset_date.minute |
68
|
|
|
secs = offset_date.second |
69
|
|
|
|
70
|
|
|
time_parts = [years, days, hours, mins, secs] |
71
|
|
|
|
72
|
|
|
first_non_zero_pos = next((i for i, x in enumerate(time_parts) if x), None) |
73
|
|
|
|
74
|
|
|
if first_non_zero_pos is None: |
75
|
|
|
return '0s' |
76
|
|
|
else: |
77
|
|
|
time_parts = time_parts[first_non_zero_pos:] |
78
|
|
|
|
79
|
|
|
if len(time_parts) == 1: |
80
|
|
|
return '%ss' % tuple(time_parts) |
81
|
|
|
elif len(time_parts) == 2: |
82
|
|
|
return '%sm%ss' % tuple(time_parts) |
83
|
|
|
elif len(time_parts) == 3: |
84
|
|
|
return '%sh%sm%ss' % tuple(time_parts) |
85
|
|
|
elif len(time_parts) == 4: |
86
|
|
|
return '%sd%sh%sm%ss' % tuple(time_parts) |
87
|
|
|
elif len(time_parts) == 5: |
88
|
|
|
return '%sy%sd%sh%sm%ss' % tuple(time_parts) |
89
|
|
|
|