-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.cpp
134 lines (114 loc) · 4.1 KB
/
time_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Avoid warning on the call of _ftime()
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#include "vrpn_Shared.h"
#include <winbase.h>
#include <stdio.h>
#include <math.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include "vrpn_Types.h"
/*
void get_time_using_GetLocalTime(unsigned long &sec, unsigned long &usec)
{
SYSTEMTIME stime; // System time in funky structure
FILETIME ftime; // Time in 100-nsec intervals since Jan 1 1601
LARGE_INTEGER tics; // ftime stored into a 64-bit quantity
GetLocalTime(&stime);
SystemTimeToFileTime(&stime, &ftime);
tics.HighPart = ftime.dwHighDateTime;
tics.LowPart = ftime.dwLowDateTime;
sec = (long)( tics.QuadPart / 10000000L );
usec = (long)( ( tics.QuadPart - ( ((LONGLONG)(sec)) * 10000000L ) ) / 10 );
}
*/
static void get_time_using_GetLocalTime(unsigned long &sec, unsigned long &usec)
{
SYSTEMTIME stime; // System time in funky structure
FILETIME ftime; // Time in 100-nsec intervals since Jan 1 1601
LARGE_INTEGER tics; // ftime stored into a 64-bit quantity
GetLocalTime(&stime);
SystemTimeToFileTime(&stime, &ftime);
// Copy the data into a structure that can be treated as a 64-bit integer
tics.HighPart = ftime.dwHighDateTime;
tics.LowPart = ftime.dwLowDateTime;
// Convert the 64-bit time into seconds and microseconds since July 1 1601
sec = (long)( tics.QuadPart / 10000000L );
usec = (long)( ( tics.QuadPart - ( ((LONGLONG)(sec)) * 10000000L ) ) / 10 );
// Translate the time to be based on January 1, 1970 (_ftime base)
// The offset here is gotten by using the "time_test" program to report the
// difference in seconds between the two clocks.
sec -= 3054524608L;
}
void get_time_using_ftime(unsigned long &sec, unsigned long &usec)
{
struct _timeb t;
_ftime(&t);
sec = static_cast<unsigned long>(t.time);
usec = (long)t. millitm * 1000;
}
int main(int argc, char *argv[]) {
/* XXX Checking how well the two clocks track each other
unsigned long lsec, lusec;
unsigned long fsec, fusec;
long dsec, dusec;
int i;
for (i = 0; i < 10; i++) {
get_time_using_GetLocalTime(lsec, lusec);
get_time_using_ftime(fsec, fusec);
dsec = lsec - fsec;
dusec = lusec - fusec;
printf("L: %u:%u, F: %u:%u, Difference: %u:%ld\n",
lsec, lusec, fsec, fusec, dsec, dusec);
Sleep(1000);
}
*/
/* Check the behavior of the clock */
SYSTEMTIME sstart, stime; // System time in funky structure
FILETIME ftime; // Time in 100-nsec intervals since Jan 1 1601
LARGE_INTEGER tics; // ftime stored into a 64-bit quantity
GetLocalTime(&sstart);
stime = sstart;
while (stime.wSecond - sstart.wSecond < 2) {
GetLocalTime(&stime);
printf("Seconds %2d, Milliseconds %4d\n", static_cast<int>(stime.wSecond),
static_cast<int>(stime.wMilliseconds));
SystemTimeToFileTime(&stime, &ftime);
// Copy the data into a structure that can be treated as a 64-bit
// integer
tics.HighPart = ftime.dwHighDateTime;
tics.LowPart = ftime.dwLowDateTime;
printf(" Converted to 64-bit: %llu\n", tics.QuadPart);
// Convert the 64-bit time into seconds and microseconds since July 1
// 1601
unsigned long sec, usec;
sec = (long)(tics.QuadPart / 10000000L);
usec = (long)((tics.QuadPart - (((LONGLONG)(sec)) * 10000000L)) / 10);
printf(" Converted to secs and usecs: %6lu:%6lu\n", sec, usec);
}
/* Checking the vrpn_gettimeofday() function for monotonicity and step size */
struct timeval last_time, this_time;
double skip;
vrpn_gettimeofday(&last_time, NULL);
printf("Should be no further output if things are working\n");
while (true) {
vrpn_gettimeofday(&this_time, NULL);
skip = vrpn_TimevalDurationSeconds(this_time, last_time);
if (skip > 200e-6) {
printf("Skipped forward %lg microseconds\n", skip*1e6);
}
if (skip < 0) {
printf("** Backwards %lg microseconds\n", skip*1e6);
}
last_time = this_time;
}
return 0;
}
#else
#include <stdio.h>
int main(void)
{
fprintf(stderr,"Program not ported to other than Windows\n");
return -1;
}
#endif