Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import OpenRTM_aist
00020
00021 TIMEVALUE_ONE_SECOND_IN_USECS = 1000000
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 class TimeValue:
00037 """
00038 """
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 def __init__(self, sec=None, usec=None):
00058 global TIMEVALUE_ONE_SECOND_IN_USECS
00059
00060 if type(sec) == str:
00061 sec = float(sec)
00062 if type(usec) == str:
00063 usec = float(usec)
00064
00065
00066 if sec and usec is None:
00067 if sec >= 0.0:
00068 dbHalfAdj_ = 0.5
00069 else:
00070 dbHalfAdj_ = -0.5
00071
00072 self.tv_sec = long(sec)
00073 self.tv_usec = long((sec - float(self.tv_sec)) *
00074 float(TIMEVALUE_ONE_SECOND_IN_USECS) + dbHalfAdj_)
00075 self.normalize()
00076 return
00077
00078 if sec is None:
00079 self.tv_sec = long(0)
00080 else:
00081 self.tv_sec = long(sec)
00082
00083 if usec is None:
00084 self.tv_usec = long(0)
00085 else:
00086 self.tv_usec = long(usec)
00087 self.normalize()
00088 return
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 def __sub__(self, tm):
00107 global TIMEVALUE_ONE_SECOND_IN_USECS
00108 try:
00109 res = TimeValue()
00110 except:
00111 res = OpenRTM_aist.TimeValue()
00112
00113 if self.tv_sec >= tm.tv_sec:
00114
00115 if self.tv_usec >= tm.tv_usec:
00116
00117 res.tv_sec = self.tv_sec - tm.tv_sec
00118 res.tv_usec = self.tv_usec - tm.tv_usec
00119 else:
00120
00121 res.tv_sec = self.tv_sec - tm.tv_sec - 1
00122 res.tv_usec = (self.tv_usec + TIMEVALUE_ONE_SECOND_IN_USECS) - tm.tv_usec
00123 else:
00124
00125 if tm.tv_usec >= self.tv_usec:
00126
00127 res.tv_sec = -(tm.tv_sec - self.tv_sec)
00128 res.tv_usec = -(tm.tv_usec - self.tv_usec)
00129 else:
00130
00131 res.tv_sec = -(tm.tv_sec - self.tv_sec - 1)
00132 res.tv_usec = -(tm.tv_usec + TIMEVALUE_ONE_SECOND_IN_USECS) + self.tv_usec
00133
00134 self.normalize()
00135 return res
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 def __add__(self, tm):
00154 global TIMEVALUE_ONE_SECOND_IN_USECS
00155 res = TimeValue()
00156 res.tv_sec = self.tv_sec + tm.tv_sec
00157 res.tv_usec = self.tv_usec + tm.tv_usec
00158 if res.tv_usec > TIMEVALUE_ONE_SECOND_IN_USECS:
00159 res.tv_sec += 1
00160 res.tv_usec -= TIMEVALUE_ONE_SECOND_IN_USECS
00161
00162 self.normalize()
00163 return res
00164
00165
00166 def sec(self):
00167 return self.tv_sec
00168
00169
00170 def usec(self):
00171 return self.tv_usec
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 def set_time(self, time):
00190 global TIMEVALUE_ONE_SECOND_IN_USECS
00191
00192 self.tv_sec = long(time)
00193 self.tv_usec = long((time - float(self.tv_sec)) * float(TIMEVALUE_ONE_SECOND_IN_USECS))
00194 return self
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 def toDouble(self):
00210 global TIMEVALUE_ONE_SECOND_IN_USECS
00211 return float(self.tv_sec) + float(self.tv_usec / float(TIMEVALUE_ONE_SECOND_IN_USECS))
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 def __str__(self):
00228 global TIMEVALUE_ONE_SECOND_IN_USECS
00229 return str(self.tv_sec + self.tv_usec / float(TIMEVALUE_ONE_SECOND_IN_USECS))
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 def sign(self):
00245 if self.tv_sec > 0:
00246 return 1
00247 if self.tv_sec < 0:
00248 return -1
00249 if self.tv_usec > 0:
00250 return 1
00251 if self.tv_usec < 0:
00252 return -1
00253 return 0
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 def normalize(self):
00264 global TIMEVALUE_ONE_SECOND_IN_USECS
00265 if self.tv_usec >= TIMEVALUE_ONE_SECOND_IN_USECS:
00266 self.tv_sec += 1
00267 self.tv_usec -= TIMEVALUE_ONE_SECOND_IN_USECS
00268
00269 while self.tv_usec >= TIMEVALUE_ONE_SECOND_IN_USECS:
00270 self.tv_sec += 1
00271 self.tv_usec -= TIMEVALUE_ONE_SECOND_IN_USECS
00272
00273 elif self.tv_usec <= -TIMEVALUE_ONE_SECOND_IN_USECS:
00274 self.tv_sec -= 1
00275 self.tv_usec += TIMEVALUE_ONE_SECOND_IN_USECS
00276
00277 while self.tv_usec <= -TIMEVALUE_ONE_SECOND_IN_USECS:
00278 self.tv_sec -= 1
00279 self.tv_usec += TIMEVALUE_ONE_SECOND_IN_USECS
00280
00281
00282 if self.tv_sec >= 1 and self.tv_usec < 0:
00283 self.tv_sec -= 1
00284 self.tv_usec += TIMEVALUE_ONE_SECOND_IN_USECS
00285
00286 elif self.tv_sec < 0 and self.tv_usec > 0:
00287 self.tv_sec += 1
00288 self.tv_usec -= TIMEVALUE_ONE_SECOND_IN_USECS