69869d0efbe8f6ebc3b3435eec400c60c4a1027b
[libasyncio] / utils / asyncio_time_tools.hpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /** 
21  * @file
22  * @brief
23  *
24  * @author Reinhard Pfau \<reinhard.pfau@intra2net.com\>
25  *
26  * @copyright &copy; Copyright 2008 Intra2Net AG
27  */
28
29 #ifndef __ASYNCIO__TIME_TOOLS_HPP__
30 #define __ASYNCIO__TIME_TOOLS_HPP__
31
32 namespace AsyncIo
33 {
34 namespace Utils
35 {
36
37 /**
38  * @brief structure for storing (a point in time as) seconds and milliseconds.
39  */
40 struct MilliTime
41 {
42     long mt_sec;
43     long mt_msec;
44
45     MilliTime(long sec=0, long msec=0);
46
47     void set(long sec, long msec=0);
48
49     inline long long get_milliseconds() const
50     {
51         return ((long long)mt_sec * 1000L + mt_msec);
52     } // eo get_milliseconds
53
54     void normalize();
55
56     bool operator < (MilliTime& other);
57     bool operator == (MilliTime& other);
58
59     MilliTime& operator -= (const MilliTime& lhs);
60     MilliTime& operator += (const MilliTime& lhs);
61
62 }; // eo struct MilliTime
63
64
65 inline MilliTime operator + (const MilliTime& rhs, const MilliTime& lhs)
66 {
67     MilliTime t(rhs);
68     return t+= lhs;
69 } // eo operator + (const MilliTime& rhs, const MilliTime lhs)
70
71 inline MilliTime operator - (const MilliTime& rhs, const MilliTime& lhs)
72 {
73     MilliTime t(rhs);
74     return t-= lhs;
75 } // eo operator - (const MilliTime& rhs, const MilliTime lhs)
76
77
78 inline bool operator <= (MilliTime& rhs, MilliTime& lhs)
79 {
80     return (rhs<lhs) || (rhs==lhs);
81 } // eo operator <= (MilliTime& rhs, MilliTime& lhs)
82
83
84
85 /*
86 ** tool functions:
87 */
88
89
90 void get_current_real_time(MilliTime& mt);
91 void get_current_monotonic_time(MilliTime& mt);
92
93
94 } // eo namespace Utils
95 } // eo namespace AsyncIo
96
97
98
99 #endif