Debian Codegraph @

seamicroclient/openstack/common/timeutils.py

raw
1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3 # Copyright 2011 OpenStack Foundation.
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17
18 """
19 Time related utilities and helper functions.
20 """
21
22 import calendar
23 import datetime
24 import time
25
26 import iso8601
27 import six
28
29
30 # ISO 8601 extended time format with microseconds
31 _ISO8601_TIME_FORMAT_SUBSECOND = '%Y-%m-%dT%H:%M:%S.%f'
32 _ISO8601_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
33 PERFECT_TIME_FORMAT = _ISO8601_TIME_FORMAT_SUBSECOND
34
35
36 def isotime(at=None, subsecond=False):
37 """Stringify time in ISO 8601 format."""
38 if not at:
39 at = utcnow()
40 st = at.strftime(_ISO8601_TIME_FORMAT
41 if not subsecond
42 else _ISO8601_TIME_FORMAT_SUBSECOND)
43 tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
44 st += ('Z' if tz == 'UTC' else tz)
45 return st
46
47
48 def parse_isotime(timestr):
49 """Parse time from ISO 8601 format."""
50 try:
51 return iso8601.parse_date(timestr)
52 except iso8601.ParseError as e:
53 raise ValueError(unicode(e))
54 except TypeError as e:
55 raise ValueError(unicode(e))
56
57
58 def strtime(at=None, fmt=PERFECT_TIME_FORMAT):
59 """Returns formatted utcnow."""
60 if not at:
61 at = utcnow()
62 return at.strftime(fmt)
63
64
65 def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT):
66 """Turn a formatted time back into a datetime."""
67 return datetime.datetime.strptime(timestr, fmt)
68
69
70 def normalize_time(timestamp):
71 """Normalize time in arbitrary timezone to UTC naive object."""
72 offset = timestamp.utcoffset()
73 if offset is None:
74 return timestamp
75 return timestamp.replace(tzinfo=None) - offset
76
77
78 def is_older_than(before, seconds):
79 """Return True if before is older than seconds."""
80 if isinstance(before, six.string_types):
81 before = parse_strtime(before).replace(tzinfo=None)
82 return utcnow() - before > datetime.timedelta(seconds=seconds)
83
84
85 def is_newer_than(after, seconds):
86 """Return True if after is newer than seconds."""
87 if isinstance(after, six.string_types):
88 after = parse_strtime(after).replace(tzinfo=None)
89 return after - utcnow() > datetime.timedelta(seconds=seconds)
90
91
92 def utcnow_ts():
93 """Timestamp version of our utcnow function."""
94 if utcnow.override_time is None:
95 # NOTE(kgriffs): This is several times faster
96 # than going through calendar.timegm(...)
97 return int(time.time())
98
99 return calendar.timegm(utcnow().timetuple())
100
101
102 def utcnow():
103 """Overridable version of utils.utcnow."""
104 if utcnow.override_time:
105 try:
106 return utcnow.override_time.pop(0)
107 except AttributeError:
108 return utcnow.override_time
109 return datetime.datetime.utcnow()
110
111
112 def iso8601_from_timestamp(timestamp):
113 """Returns a iso8601 formated date from timestamp."""
114 return isotime(datetime.datetime.utcfromtimestamp(timestamp))
115
116
117 utcnow.override_time = None
118
119
120 def set_time_override(override_time=datetime.datetime.utcnow()):
121 """Overrides utils.utcnow.
122
123 Make it return a constant time or a list thereof, one at a time.
124 """
125 utcnow.override_time = override_time
126
127
128 def advance_time_delta(timedelta):
129 """Advance overridden time using a datetime.timedelta."""
130 assert(not utcnow.override_time is None)
131 try:
132 for dt in utcnow.override_time:
133 dt += timedelta
134 except TypeError:
135 utcnow.override_time += timedelta
136
137
138 def advance_time_seconds(seconds):
139 """Advance overridden time by seconds."""
140 advance_time_delta(datetime.timedelta(0, seconds))
141
142
143 def clear_time_override():
144 """Remove the overridden time."""
145 utcnow.override_time = None
146
147
148 def marshall_now(now=None):
149 """Make an rpc-safe datetime with microseconds.
150
151 Note: tzinfo is stripped, but not required for relative times.
152 """
153 if not now:
154 now = utcnow()
155 return dict(day=now.day, month=now.month, year=now.year, hour=now.hour,
156 minute=now.minute, second=now.second,
157 microsecond=now.microsecond)
158
159
160 def unmarshall_time(tyme):
161 """Unmarshall a datetime dict."""
162 return datetime.datetime(day=tyme['day'],
163 month=tyme['month'],
164 year=tyme['year'],
165 hour=tyme['hour'],
166 minute=tyme['minute'],
167 second=tyme['second'],
168 microsecond=tyme['microsecond'])
169
170
171 def delta_seconds(before, after):
172 """Return the difference between two timing objects.
173
174 Compute the difference in seconds between two date, time, or
175 datetime objects (as a float, to microsecond resolution).
176 """
177 delta = after - before
178 try:
179 return delta.total_seconds()
180 except AttributeError:
181 return ((delta.days * 24 * 3600) + delta.seconds +
182 float(delta.microseconds) / (10 ** 6))
183
184
185 def is_soon(dt, window):
186 """Determines if time is going to happen in the next window seconds.
187
188 :params dt: the time
189 :params window: minimum seconds to remain to consider the time not soon
190
191 :return: True if expiration is within the given duration
192 """
193 soon = (utcnow() + datetime.timedelta(seconds=window))
194 return normalize_time(dt) <= soon