summaryrefslogtreecommitdiff
path: root/utils/Time.cc
blob: 3e57a3ccf1be1ef084848f2c0c941eff242e4856 (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 CcuTimeStamp
The class \var{CcuTimeStamp} 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}
    CcuTimeStamp before;
    long_task ();
    CcuTimeStamp after;
    Millisecond delay = after - before;
\end{ccode}
The values of \typ{CcuTimeStamp}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.
?*/
CcuTimeStamp :: CcuTimeStamp ()
{
	struct timeval tv;
	gettimeofday (&tv, 0);
	Value = 1000 * tv.tv_sec + tv.tv_usec / 1000;
}

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

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

/*?class CcuTime
The class \typ{CcuTime} 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{CcuTime} with a negative value.
?*/

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

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

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