Adding trace Collector RM
[nepi.git] / src / nepi / util / timefuncs.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20
21 import datetime
22 import re
23
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)$")
27
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)
30
31 def stformat(sdate):
32     """ Constructs a datetime object from a string date with
33     format YYYYMMddHHMMSSffff 
34
35     """
36     return datetime.datetime.strptime(sdate, _strf).date()
37
38 def tsformat(date = None):
39     """ Formats a datetime object to a string with format YYYYMMddHHMMSSffff.
40     If no date is given, the current date is used.
41     
42     """
43     if not date:
44         date = tnow()
45
46     return date.strftime(_strf)
47
48 def tnow():
49     """ Returns datetime object with the current time """
50     return datetime.datetime.now()
51
52 def tdiff(date1, date2):
53     """ Returns difference ( date1 - date2 ) as a datetime object,
54     where date1 and date 2 are datetime objects 
55     
56     """
57     return date1 - date2
58
59 def tdiffsec(date1, date2):
60     """ Returns the date difference ( date1 - date2 ) in seconds,
61     where date1 and date 2 are datetime objects 
62     
63     """
64     diff = tdiff(date1, date2)
65     return diff.total_seconds()
66
67 def stabsformat(sdate, dbase = None):
68     """ Constructs a datetime object from a string date.
69     The string date can be expressed as an absolute date
70     ( i.e. format YYYYMMddHHMMSSffff ) or as a relative time
71     ( e.g. format '5m' or '10s'). 
72     If the date is a relative time and the dbase parameter 
73     is given (dbase must be datetime object), the returned
74     date will be dbase + sdate. If dbase is None, 
75     current time will be used instead as base time.
76
77     :param date : string date  
78     :type date : date 
79
80     """
81
82     # No date given, return current datetime
83     if not sdate:
84         return tnow()
85
86     # Absolute date is given
87     if _reabs.match(sdate):
88         return stformat(sdate)
89
90     # Relative time is given
91     m = _rerel.match(sdate)
92     if m:
93         time = float(m.groupdict()['time'])
94         units = m.groupdict()['units']
95         if units == 'h':
96             delta = datetime.timedelta(hours = time) 
97         elif units == 'm':
98             delta = datetime.timedelta(minutes = time) 
99         elif units == 's':
100             delta = datetime.timedelta(seconds = time) 
101         elif units == 'ms':
102             delta = datetime.timedelta(microseconds = (time*1000)) 
103         else:
104             delta = datetime.timedelta(microseconds = time)
105         
106         if not dbase:
107             dbase = tnow()
108
109         return dbase + delta
110
111     return None
112