blob: 98d877d59a511a38b3657538ae6dc05119be1e5f [file] [log] [blame]
Matthew Treinish0db53772013-07-26 10:39:35 -04001# 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"""
19Time related utilities and helper functions.
20"""
21
22import calendar
23import datetime
Matthew Treinishffa94d62013-09-11 18:09:17 +000024import time
Matthew Treinish0db53772013-07-26 10:39:35 -040025
26import iso8601
27import 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'
33PERFECT_TIME_FORMAT = _ISO8601_TIME_FORMAT_SUBSECOND
34
35
36def 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
48def parse_isotime(timestr):
49 """Parse time from ISO 8601 format."""
50 try:
51 return iso8601.parse_date(timestr)
52 except iso8601.ParseError as e:
Matthew Treinishffa94d62013-09-11 18:09:17 +000053 raise ValueError(unicode(e))
Matthew Treinish0db53772013-07-26 10:39:35 -040054 except TypeError as e:
Matthew Treinishffa94d62013-09-11 18:09:17 +000055 raise ValueError(unicode(e))
Matthew Treinish0db53772013-07-26 10:39:35 -040056
57
58def 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
65def 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
70def 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
78def 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
85def 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
92def utcnow_ts():
93 """Timestamp version of our utcnow function."""
Matthew Treinishffa94d62013-09-11 18:09:17 +000094 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
Matthew Treinish0db53772013-07-26 10:39:35 -040099 return calendar.timegm(utcnow().timetuple())
100
101
102def 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
112def iso8601_from_timestamp(timestamp):
113 """Returns a iso8601 formated date from timestamp."""
114 return isotime(datetime.datetime.utcfromtimestamp(timestamp))
115
116
117utcnow.override_time = None
118
119
Matthew Treinishf45528a2013-10-24 20:12:28 +0000120def set_time_override(override_time=None):
Matthew Treinish0db53772013-07-26 10:39:35 -0400121 """Overrides utils.utcnow.
122
123 Make it return a constant time or a list thereof, one at a time.
Matthew Treinishf45528a2013-10-24 20:12:28 +0000124
125 :param override_time: datetime instance or list thereof. If not
126 given, defaults to the current UTC time.
Matthew Treinish0db53772013-07-26 10:39:35 -0400127 """
Matthew Treinishf45528a2013-10-24 20:12:28 +0000128 utcnow.override_time = override_time or datetime.datetime.utcnow()
Matthew Treinish0db53772013-07-26 10:39:35 -0400129
130
131def advance_time_delta(timedelta):
132 """Advance overridden time using a datetime.timedelta."""
133 assert(not utcnow.override_time is None)
134 try:
135 for dt in utcnow.override_time:
136 dt += timedelta
137 except TypeError:
138 utcnow.override_time += timedelta
139
140
141def advance_time_seconds(seconds):
142 """Advance overridden time by seconds."""
143 advance_time_delta(datetime.timedelta(0, seconds))
144
145
146def clear_time_override():
147 """Remove the overridden time."""
148 utcnow.override_time = None
149
150
151def marshall_now(now=None):
152 """Make an rpc-safe datetime with microseconds.
153
154 Note: tzinfo is stripped, but not required for relative times.
155 """
156 if not now:
157 now = utcnow()
158 return dict(day=now.day, month=now.month, year=now.year, hour=now.hour,
159 minute=now.minute, second=now.second,
160 microsecond=now.microsecond)
161
162
163def unmarshall_time(tyme):
164 """Unmarshall a datetime dict."""
165 return datetime.datetime(day=tyme['day'],
166 month=tyme['month'],
167 year=tyme['year'],
168 hour=tyme['hour'],
169 minute=tyme['minute'],
170 second=tyme['second'],
171 microsecond=tyme['microsecond'])
172
173
174def delta_seconds(before, after):
175 """Return the difference between two timing objects.
176
177 Compute the difference in seconds between two date, time, or
178 datetime objects (as a float, to microsecond resolution).
179 """
180 delta = after - before
181 try:
182 return delta.total_seconds()
183 except AttributeError:
184 return ((delta.days * 24 * 3600) + delta.seconds +
185 float(delta.microseconds) / (10 ** 6))
186
187
188def is_soon(dt, window):
189 """Determines if time is going to happen in the next window seconds.
190
191 :params dt: the time
192 :params window: minimum seconds to remain to consider the time not soon
193
194 :return: True if expiration is within the given duration
195 """
196 soon = (utcnow() + datetime.timedelta(seconds=window))
197 return normalize_time(dt) <= soon