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 version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
23 _strf = "%Y%m%d%H%M%S%f"
24 _reabs = re.compile("^\d{20}$")
25 _rerel = re.compile("^(?P<time>\d+(.\d+)?)(?P<units>h|m|s|ms|us)$")
27 # Work around to fix "ImportError: Failed to import _strptime because the import lock is held by another thread."
28 datetime.datetime.strptime("20120807124732894211", _strf)
31 """ Constructs a datetime object from a string date with
32 format YYYYMMddHHMMSSffff
35 return datetime.datetime.strptime(sdate, _strf).date()
37 def tsformat(date = None):
38 """ Formats a datetime object to a string with format YYYYMMddHHMMSSffff.
39 If no date is given, the current date is used.
45 return date.strftime(_strf)
48 """ Returns datetime object with the current time """
49 return datetime.datetime.now()
51 def tdiff(date1, date2):
52 """ Returns difference ( date1 - date2 ) as a datetime object,
53 where date1 and date 2 are datetime objects
58 def _get_total_seconds(td):
59 return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
61 def tdiffsec(date1, date2):
62 """ Returns the date difference ( date1 - date2 ) in seconds,
63 where date1 and date 2 are datetime objects
66 diff = tdiff(date1, date2)
67 return _get_total_seconds(diff)
69 def stabsformat(sdate, dbase = None):
70 """ Constructs a datetime object from a string date.
71 The string date can be expressed as an absolute date
72 ( i.e. format YYYYMMddHHMMSSffff ) or as a relative time
73 ( e.g. format '5m' or '10s').
74 If the date is a relative time and the dbase parameter
75 is given (dbase must be datetime object), the returned
76 date will be dbase + sdate. If dbase is None,
77 current time will be used instead as base time.
79 :param date : string date
84 # No date given, return current datetime
88 # Absolute date is given
89 if _reabs.match(sdate):
90 return stformat(sdate)
92 # Relative time is given
93 m = _rerel.match(sdate)
95 time = float(m.groupdict()['time'])
96 units = m.groupdict()['units']
98 delta = datetime.timedelta(hours = time)
100 delta = datetime.timedelta(minutes = time)
102 delta = datetime.timedelta(seconds = time)
104 delta = datetime.timedelta(microseconds = (time*1000))
106 delta = datetime.timedelta(microseconds = time)
115 def compute_delay_ms(timestamp2, timestamp1):
116 d1 = datetime.datetime.fromtimestamp(float(timestamp1))
117 d2 = datetime.datetime.fromtimestamp(float(timestamp2))
120 # round up resolution - round up to miliseconds
121 return delay.total_seconds() * 1000