2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
24 _strf = "%Y%m%d%H%M%S%f"
25 _reabs = re.compile("^\d{20}$")
26 _rerel = re.compile("^(?P<time>\d+(.\d+)?)(?P<units>h|m|s|ms|us)$")
28 # Work around to fix "ImportError: Failed to import _strptime because the import lock is held by another thread."
29 datetime.datetime.strptime("20120807124732894211", _strf)
33 return datetime.datetime.now().strftime(_strf)
35 def strfdiff(str1, str2):
36 # Time difference in seconds without ignoring miliseconds
37 d1 = datetime.datetime.strptime(str1, _strf)
38 d2 = datetime.datetime.strptime(str2, _strf)
40 ddays = diff.days * 86400
41 dus = round(diff.microseconds * 1.0e-06, 2)
42 ret = ddays + diff.seconds + dus
47 """ User defined date to scheduler date
49 :param date : user define date matchin the pattern _strf
55 if _reabs.match(date):
57 m = _rerel.match(date)
59 time = float(m.groupdict()['time'])
60 units = m.groupdict()['units']
62 delta = datetime.timedelta(hours = time)
64 delta = datetime.timedelta(minutes = time)
66 delta = datetime.timedelta(seconds = time)
68 delta = datetime.timedelta(microseconds = (time*1000))
70 delta = datetime.timedelta(microseconds = time)
71 now = datetime.datetime.now()
73 return d.strftime(_strf)