[go: up one dir, main page]

File: postime.h

package info (click to toggle)
dibbler 1.0.1-1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 13,308 kB
  • ctags: 10,470
  • sloc: cpp: 60,323; ansic: 12,233; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (239 lines) | stat: -rw-r--r-- 6,904 bytes parent folder | download | duplicates (6)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
    Posadis - A DNS Server
    Posadis time functions
    Copyright (C) 2002  Meilof Veeningen <meilof@users.sourceforge.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __POSLIB_POSTIME_H
#define __POSLIB_POSTIME_H

#ifndef WIN32
#include <pthread.h>
#endif
#include "types.h"

#ifdef WIN32
#include <time.h>
struct timespec
  {
    time_t tv_sec;            /* Seconds.  */
    long int tv_nsec;           /* Nanoseconds.  */
  };
#endif


/*! \file poslib/postime.h
 * \brief Posadis time functions
 *
 * The functions in this source file, as well as the postime_t class defined
 * here, provide easy access to times in a system-independent way.
 */

/*!
 * \brief Time with millisecond precision
 *
 * The postime_t class offers system-independent time handling with
 * millisecond precision. Using its member functions and operators, one can
 * manipulate times in an inituive way.
 */
class postime_t {
  public:

    /*!
     * \brief postime_t constructor
     *
     * This constructor sets the number of seconds and the number of
     * milliseconds to zero.
     */
    postime_t();

    /*!
     * \brief constructor with a number of milliseconds
     *
     * This constructor takes a number of milliseconds. If this number is
     * greater than 1000, the number is split in a number of seconds and
     * a number of milliseconds. This function is mainly useful for offsets.
     * \param msecs Number of milliseconds
     * \sa postime_t()
     */
    postime_t(int msecs);

    /*!
     * \brief time assignment
     *
     * This assignment operator copies the given time to the current time.
     * \param t The time to assign
     * \return The assigned time
     */
    postime_t& operator=(const postime_t &t);

    /*!
     * \brief number of seconds
     *
     * This is the number of seconds of the time. For absolute times, this
     * is the number of seconds since the UNIX epoch as returned by the
     * gettimeofday() function.
     */
    long sec;

    /*!
     * \brief number of milliseconds
     *
     * This is the number of milliseconds. This value can range from 0 to
     * 999. If you assign a value greater than 999 to this, results are
     * undefined.
     */
    long msec;

    /*!
     * \brief equality check
     *
     * This operator checks whether two times are equal, that is, both the
     * number of seconds and the number of milliseconds are the same
     * \param t The time to compare with
     */
    bool operator==(const postime_t &t);

    /*!
     * \brief less than or equal
     *
     * This operator checks whether this time is before or the same as the
     * argument, t.
     * \param t The time to compare with
     */
    bool operator<=(const postime_t &t);

    /*!
     * \brief less than
     *
     * This operator checks whether this time is before the argument, t.
     * \param t The time to compare with
     */
    bool operator<(const postime_t &t);

    /*!
     * \brief greater than or equal

     *
     * This operator checks whether this time is after or the same as the
     * argument, t.
     * \param t The time to compare with
     */
    bool operator>=(const postime_t &t);

    /*!

     * \brief greater than
     *
     * This operator checks whether the time is after the argument, t.
     * \param t The time to compare with
     */
    bool operator>(const postime_t &t);

    /*!
     * \brief greater than timespec
     *
     * This operator checks whether the time is after the argument, t.
     * t is a timespec, as returned by the postimespec() function, and as
     * required by pthread_cond_timedwait function.
     * \param t The time to compare with
     * \sa postimespec()
     */
    bool operator>(const timespec &t);


    /*!
     * \brief Number of milliseconds after t
     *
     * This function returns the amount of time between this time and the time
     * specified by t. If this time is after t, this value is positive, if
     * this time is before t, the return value is negative.
     * \param t The time to compare with
     * \return The number of milliseconds the time is after t
     */
    int after(const postime_t &t);

    /*!
     * \brief time substraction
     *
     * This function substracts t from the given time and returns the result.
     * \param t The time to substract
     * \return The result of the substraction
     */
    postime_t operator-(const postime_t &t);

    /*!
     * \brief time addition
     *
     * This function adds t to the given time and returns the result. This is
     * probably only useful if at least one of the given times is an offset.
     * \param t The time to add
     * \return The result of the addition
     */
    postime_t operator+(const postime_t &t);

    /*!
     * \brief time millisecond addition
     *
     * This function adds the given amount of milliseconds to the time and
     * returns the result.
     * \param t The number of milliseconds to add
     * \return The result of the addition
     */
    postime_t operator+(int t);

    /*!
     * \brief time addition
     *
     * This function adds t to the given time and returns the result. This is
     * probably only useful if at least one of the given times is an offset.
     * \param t The time to add
     * \return This
     */
    postime_t& operator+=(const postime_t &t);

    /*!
     * \brief time millisecond addition
     *
     * This function adds the given amount of milliseconds to the time and
     * returns the result.
     * \param t The number of milliseconds to add
     * \return This
     */
    postime_t& operator+=(int t);
};

/*!
 * \brief get the current time
 *
 * Retrieves the current time with millisecond precision and stores it in a
 * postime_t structure.
 * \return The current time
 */
postime_t getcurtime();

/*!
 * \brief timespec for given millisecond timeout
 *
 * Returns a timespec structure that can be used, among other things, for the
 * pthread_cond_timedwait function (which is what it was designed for).
 * \param timeout Number of milliseconds from now the timespec is set to
 * \return A timespec structure containing an absolute time.
 */
timespec postimespec(int timeout);

#endif /* __POSLIB_POSTIME_H */