jrtlib 时间测试
代码:
#include "rtptimeutilities.h" #include <iostream> #include <stdio.h> using namespace std; using namespace jrtplib; int main(void) { double times[] = { 1.2, -2.3, 0, 3.999999 }; for (int i = 0 ; i < sizeof(times)/sizeof(double) ; i++) { RTPTime t(times[i]); cout << "Double: " << times[i] << endl; cout << "Seconds: " << t.GetSeconds() << endl; cout << "Microseconds: " << t.GetMicroSeconds() << endl; cout << endl; } int32_t secsAndMuSecs[][2] = { { 1, 3000}, {-2, 200}, {0, 0}, {-2, 999999} }; for (int i = 0 ; i < sizeof(secsAndMuSecs)/(2*sizeof(int32_t)) ; i++) { RTPTime t(secsAndMuSecs[i][0], secsAndMuSecs[i][1]); cout << "Input Seconds: " << secsAndMuSecs[i][0] << endl; cout << "Input Microseconds: " << secsAndMuSecs[i][1] << endl; cout << "Output Seconds: " << t.GetSeconds() << endl; cout << "Output Microseconds: " << t.GetMicroSeconds() << endl; cout << "Output Double: " << t.GetDouble() << endl; cout << endl; } for (int i = 0 ; i < 10 ; i++) { RTPTime now = RTPTime::CurrentTime(); fprintf(stderr, "%10u.%06d\n", (uint32_t)now.GetSeconds(), now.GetMicroSeconds()); RTPTime::Wait(RTPTime(1)); } return 0; }
运行结果:
/timetest
Double: 1.2
Seconds: 1
Microseconds: 200000
Double: -2.3
Seconds: -2
Microseconds: 300000
Double: 0
Seconds: 0
Microseconds: 0
Double: 4
Seconds: 3
Microseconds: 999999
Input Seconds: 1
Input Microseconds: 3000
Output Seconds: 1
Output Microseconds: 3000
Output Double: 1.003
Input Seconds: -2
Input Microseconds: 200
Output Seconds: -2
Output Microseconds: 200
Output Double: -2.0002
Input Seconds: 0
Input Microseconds: 0
Output Seconds: 0
Output Microseconds: 0
Output Double: 0
Input Seconds: -2
Input Microseconds: 999999
Output Seconds: -2
Output Microseconds: 999999
Output Double: -3
1541561350.472532
1541561351.473606
1541561352.474346
1541561353.474663
1541561354.475759
1541561355.476676
1541561356.477701
1541561357.478725
1541561358.479587
1541561359.480622
double GetDouble() const { return (((double)sec)+(((double)microsec)/1000000.0)); } inline RTPTime::RTPTime(double t) { sec = (uint32_t)t; double t2 = t-((double)sec); t2 *= 1000000.0; microsec = (uint32_t)t2; } RTPTime(uint32_t seconds, uint32_t microseconds) { sec = seconds; microsec = microseconds; } uint32_t GetSeconds() const { return sec; } uint32_t GetMicroSeconds() const { return microsec; }