forked from ManageIQ/integration_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeutil.py
258 lines (194 loc) · 7.97 KB
/
timeutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-
""" This module should contain all things associated with time or date that can be shared.
"""
from datetime import datetime as _datetime
import time
import tzlocal
local_tz = tzlocal.get_localzone()
class parsetime(_datetime): # NOQA
""" Modified class with loaders for our datetime formats.
"""
american_with_utc_format = "%m/%d/%y %H:%M:%S UTC"
iso_with_utc_format = "%Y-%m-%d %H:%M:%S UTC"
american_minutes = "%m/%d/%y %H:%M"
american_minutes_with_utc = "%m/%d/%y %H:%M UTC"
american_date_only_format = "%m/%d/%y"
iso_date_only_format = "%Y-%m-%d"
request_format = "%Y-%m-%d-%H-%M-%S"
long_date_format = "%B %d, %Y %H:%M"
saved_report_title_format = "%a, %d %b %Y %H:%M:%S +0000"
@classmethod
def _parse(cls, fmt, time_string):
return cls.fromtimestamp(
time.mktime(
time.strptime(
time_string,
fmt
)
)
)
@classmethod
def from_american_with_utc(cls, time_string):
""" Convert the string representation of the time into parsetime()
CFME's format here is 'mm/dd/yy hh:mm:ss UTC'
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.american_with_utc_format, time_string)
def to_american_with_utc(self):
""" Convert the this object to string representation in american with UTC.
CFME's format here is 'mm/dd/yy hh:mm:ss UTC'
Returns: :py:class`str` object
"""
return self.strftime(self.american_with_utc_format)
@classmethod
def from_iso_with_utc(cls, time_string):
""" Convert the string representation of the time into parsetime()
CFME's format here is 'mm-dd-yy hh:mm:ss UTC'
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.iso_with_utc_format, time_string)
def to_iso_with_utc(self):
""" Convert the this object to string representation in american with UTC.
CFME's format here is 'mm-dd-yy hh:mm:ss UTC'
Returns: :py:class`str` object
"""
return self.strftime(self.iso_with_utc_format)
@classmethod
def from_american_minutes(cls, time_string):
""" Convert the string representation of the time into parsetime()
CFME's format here is 'mm/dd/yy hh:mm'
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.american_minutes, time_string)
def to_american_minutes(self):
""" Convert the this object to string representation in american with just minutes.
CFME's format here is 'mm/dd/yy hh:mm'
Returns: :py:class`str` object
"""
return self.strftime(self.american_minutes)
@classmethod
def from_american_minutes_with_utc(cls, time_string):
""" Convert the string representation of the time into parsetime()
CFME's format here is 'mm/dd/yy hh:mm UTC'
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.american_minutes_with_utc, time_string)
def to_american_minutes_with_utc(self):
""" Convert the this object to string representation in american with just minutes.
CFME's format here is 'mm/dd/yy hh:mm'
Returns: :py:class`str` object
"""
return self.strftime(self.american_minutes_with_utc)
@classmethod
def from_american_date_only(cls, time_string):
""" Convert the string representation of the time into parsetime()
CFME's format here is 'mm/dd/yy'
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.american_date_only_format, time_string)
def to_american_date_only(self):
""" Convert the this object to string representation in american date only format.
CFME's format here is 'mm/dd/yy'
Returns: :py:class`str` object
"""
return self.strftime(self.american_date_only_format)
@classmethod
def from_iso_date(cls, time_string):
""" Convert the string representation of the time into parsetime()
Format here is 'YYYY-MM-DD'
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.iso_date_only_format, time_string)
def to_iso_date(self):
""" Convert the this object to string representation in ISO format.
Format here is 'YYYY-MM-DD'
Returns: :py:class`str` object
"""
return self.strftime(self.iso_date_only_format)
@classmethod
def from_request_format(cls, time_string):
""" Convert the string representation of the time into parsetime()
Format here is 'YYYY-MM-DD-HH-MM-SS'. Used for transmitting data over http
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.request_format, time_string)
def to_request_format(self):
""" Convert the this object to string representation in http request.
Format here is 'YYYY-MM-DD-HH-MM-SS'
Returns: :py:class`str` object
"""
return self.strftime(self.request_format)
@classmethod
def from_long_date_format(cls, time_string):
""" Convert the string representation of the time into parsetime()
Format here is '%B %d, %Y %H:%M'.
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.long_date_format, time_string)
def to_long_date_format(self):
""" Convert the this object to string representation in http request.
Format here is '%B %d, %Y %H:%M'
Returns: :py:class`str` object
"""
return self.strftime(self.long_date_format)
@classmethod
def from_saved_report_title_format(cls, time_string):
""" Convert the string representation of the time into parsetime()
Format here is '%a, %d %b %Y %H:%M:%S +0000'.
Args:
time_string: String with time to parse
Returns: :py:class`utils.timeutil.datetime()` object
"""
return cls._parse(cls.saved_report_title_format, time_string)
def to_saved_report_title_format(self):
""" Convert the this object to string representation in Saved Report title.
Format here is '%a, %d %b %Y %H:%M:%S +0000'
Returns: :py:class`str` object
"""
return self.strftime(self.saved_report_title_format)
def nice_seconds(t_s):
"""Return nicer representation of seconds"""
if t_s < 60.0:
return "{0:.2f}s".format(t_s)
minutes = 1
while t_s - (minutes * 60.0) >= 60.0:
minutes += 1
seconds = t_s - (minutes * 60)
if minutes < 60.0:
return "{0}m{1:.2f}s".format(minutes, seconds)
# Hours
hours = 1
while minutes - (hours * 60.0) >= 60.0:
hours += 1
minutes = minutes - (hours * 60)
if hours < 24.0:
return "{0}h{1}m{2:.2f}s".format(hours, minutes, seconds)
# Days
days = 1
while hours - (days * 24.0) >= 24.0:
days += 1
hours = hours - (days * 24)
if days < 7.0:
return "{0}d{1}h{2}m{3:.2f}s".format(days, hours, minutes, seconds)
# Weeks
weeks = 1
while days - (weeks * 7.0) >= 7.0:
weeks += 1
days = days - (weeks * 7)
return "{0}w{1}d{2}h{3}m{4:.2f}s".format(weeks, days, hours, minutes, seconds)