00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import OpenRTM
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class TimeValue:
00035 """
00036 """
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 def __init__(self, sec=None, usec=None):
00056 if sec is None:
00057 self.tv_sec = 0
00058 else:
00059 self.tv_sec = float(sec)
00060
00061 if usec is None:
00062 self.tv_usec = 0
00063 else:
00064 self.tv_usec = float(usec)
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 def __sub__(self, tm):
00083 try:
00084 res = TimeValue()
00085 except:
00086 res = OpenRTM.TimeValue()
00087
00088 if self.tv_sec >= tm.tv_sec:
00089 if self.tv_usec >= tm.tv_usec:
00090 res.tv_sec = self.tv_sec - tm.tv_sec
00091 res.tv_usec = self.tv_usec - tm.tv_usec
00092 else:
00093 res.tv_sec = self.tv_sec - tm.tv_sec - 1
00094 res.tv_usec = (self.tv_usec + 1000000) - tm.tv_usec
00095 else:
00096 if tm.tv_usec >= self.tv_usec:
00097 res.tv_sec = -(tm.tv_sec - self.tv_sec)
00098 res.tv_usec = -(tm.tv_usec - self.tv_usec)
00099 else:
00100 res.tv_sec = -(tm.tv_sec - self.tv_sec - 1)
00101 res.tv_usec = -(tm.tv_usec + 1000000) + self.tv_usec
00102 return res
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 def __add__(self, tm):
00121 res = TimeValue()
00122 res.tv_sec = self.tv_sec + tm.tv_sec
00123 res.tv_usec = self.tv_usec + tm.tv_usec
00124 if res.tv_usec > 1000000:
00125 res.tv_sec += 1
00126 res.tv_usec -= 1000000
00127 return res
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 def set_time(self, time):
00145 self.tv_sec = long(time)
00146 self.tv_usec = long((time - float(self.tv_sec))*1000000)
00147 return self
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 def toDouble(self):
00163 return float(self.tv_sec) + float(self.tv_usec/1000000.0)
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 def __str__(self):
00180 return str(self.tv_sec + self.tv_usec/1000000.0)
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 def sign(self):
00196 if self.tv_sec > 0:
00197 return 1
00198 if self.tv_sec < 0:
00199 return -1
00200 if self.tv_usec > 0:
00201 return 1
00202 if self.tv_usec < 0:
00203 return -1
00204 return 0