summaryrefslogtreecommitdiff
path: root/utils/Time.cc
blob: ced0df5d35af76e1e15b7614d4c996040feac9c0 (plain)
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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	time management
 *
 *	$Id$
 *	$CurLog$
 */

#include "Time.h"
#include <sys/time.h>
//extern "C" int gettimeofday (struct timeval *, struct timezone *);

/*?class IvlTimeStamp
The class \var{IvlTimeStamp} provides variables that can be manipulated as
integers, but whose initial value is set according to the current time.
The following example should be self-explanatory:
\begin{ccode}
    IvlTimeStamp before;
    long_task ();
    IvlTimeStamp after;
    Millisecond delay = after - before;
\end{ccode}
The values of \typ{IvlTimeStamp}s are expressed in milliseconds.
The type \typ{Millisecond} is currently defined as \typ{long int}.
?*/

/*?
Create a time stamp. Its value is set according to the current time.
?*/
IvlTimeStamp :: IvlTimeStamp ()
{
	struct timeval tv;
	gettimeofday (&tv, 0);
	Value = 1000 * tv.tv_sec + tv.tv_usec / 1000;
}

#ifdef DOC
/*?
Get the value of a time stamp.
?*/
IvlTimeStamp :: operator Millisecond () const
{
}
#endif	/* DOC */

const char*
IvlTimeStamp :: AsString () const
{
	time_t t = Value / 1000;
	return ctime (&t);
}

/*?class IvlTime
The class \typ{IvlTime} provides a simple way of manipulating real-time. Objects
of this class are used like integer variables whose value would change with time.
That behavior is obtained through the redefinition of the operator yielding the
integer value.

Time values are expressed with the type \typ{Millisecond}, which is currently implemented
as \typ{long int}. You may initialize a \typ{IvlTime} with a negative value.
?*/

/*?
Initialize a time variable with initial value \var{t}.
?*/
IvlTime :: IvlTime (Millisecond t)
{
	IvlTimeStamp now;
	Offset = now - t;
}

/*?
Get the current value of a time variable.
?*/
IvlTime :: operator Millisecond () const
{
	IvlTimeStamp now;
	return now - Offset;
}

/*?
Reinitialize a time variable to the value \var{t}.
?*/
IvlTime&
IvlTime :: operator = (Millisecond t)
{
	IvlTimeStamp now;
	Offset = now - t;
	return *this;
}