-
Notifications
You must be signed in to change notification settings - Fork 1
/
utime.h
241 lines (234 loc) · 7.05 KB
/
utime.h
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
240
241
/* #***************************************************************************
#* Copyright (C) 2006-2023 by DTU
#*
#*
#* The MIT License (MIT) https://mit-license.org/
#*
#* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
#* and associated documentation files (the “Software”), to deal in the Software without restriction,
#* including without limitation the rights to use, copy, modify, merge, publish, distribute,
#* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
#* is furnished to do so, subject to the following conditions:
#*
#* The above copyright notice and this permission notice shall be included in all copies
#* or substantial portions of the Software.
#*
#* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
#* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
#* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
#* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#* THE SOFTWARE. */
#ifndef UTIME_H
#define UTIME_H
#include <sys/time.h>
/**
Class encapsulation the time structure used by 'gettimeofday'
with resolution in years down to micro-seconds.
The class has functions to make simple time calculations and
conversion to and from string in localized format. */
class UTime
{
public:
/**
Constructor */
UTime();
/**
* Constructor that init to now */
UTime(const char*);
/**
Destructor */
~UTime();
/**
Clear to 0.0 */
void clear();
/**
Get time value in seconds (since 1970) */
unsigned long getSec();
/**
Get milisecond value within second in range 0..999 */
long getMilisec();
/**
Get microsecond value within second in range 0..999999 */
unsigned long getMicrosec();
/**
Get second value with microsecond as decimals */
float getDecSec();
/**
Get time since t1 as decimal seconds. */
float getDecSec(UTime t1);
/**
Get time past since this time in seconds */
float getTimePassed();
/**
Set time value to system time now using gettimeofday() */
inline void now()
{ gettimeofday(&time, nullptr); valid = true; }
/**
Set time from a timeval structure */
void setTime(timeval iTime);
/**
Set time using seconds and microseconds. */
void setTime(long sec, long uSec);
/**
* Writes time to INFO in format "hh:mm:ss.msec"
* \param info destination buffer, must be at least 13 characters long
* \param local converts time to local time (is system time is UTM or somthing)
*/
int getTimeAsString(char * info, bool local = true);
/**
Writes time to INFO in format "yyyyMMdd_hhmmss.msec"
* \param info is a bugger for the string, must be at least 19 characters long.
* \param local should time be in local time (else UTM if set on computer)
* \returns pointer to the info buffer */
char * getForFilename(char * info, bool local = true);
/**
* Same as above, but just returns a std::string
* */
std::string getForFilename();
/**
Writes time to INFO in format "yyyy-MM-dd hh:mm:ss.msec"
* \param info is a bugger for the string, must be at least 24 characters long.
* \param local should time be in local time (else UTM if set on computer)
* \returns pointer to the info buffer */
char * getDateTimeAsString(char * info, bool local = true);
/**
* Compare two times */
inline UTime operator=(timeval newTime)
{
time = newTime;
valid = true;
return *this;
};
/**
Compare two times */
inline bool operator==(UTime other)
{
bool result;
if ((time.tv_sec == other.time.tv_sec) and (time.tv_usec == other.time.tv_usec))
result = true;
else
result = false;
return result;
};
/**
Compare two times */
inline bool operator> (UTime other)
{
bool result;
if ((time.tv_sec > other.time.tv_sec) or
((time.tv_sec == other.time.tv_sec) and (time.tv_usec > other.time.tv_usec)))
result = true;
else
result = false;
return result;
};
/**
Compare two times */
inline bool operator>= (UTime other)
{ return not (*this < other); };
/**
Compare two times */
inline bool operator< (UTime other)
{
bool result;
if ((time.tv_sec < other.time.tv_sec) or
((time.tv_sec == other.time.tv_sec) and (time.tv_usec < other.time.tv_usec)))
result = true;
else
result = false;
return result;
};
/**
Compare two times, where other is a float float */
inline bool operator< (float other)
{
return ((getDecSec() - other) < 0.0);
};
/**
Compare two times, where other is a float float */
inline bool operator> (float other)
{
return ((getDecSec() - other) > 0.0);
};
/**
Compare two times, where other is a float float */
inline bool operator<= (float other)
{
return ((getDecSec() - other) <= 0.0);
};
/**
Compare two times, where other is a float float */
inline bool operator>= (float other)
{
return ((getDecSec() - other) >= 0.0);
};
/**
Compare two times */
inline bool operator<= (UTime other)
{ return not (*this > other); };
/**
Compare two times */
inline bool operator!=(UTime other)
{
return not (*this == other);
};
/**
Subtract two UTime values and get result in decimal seconds */
inline float operator- (UTime old)
{ return getDecSec(old);};
/**
Add a number of seconds to this time */
UTime operator+ (float seconds);
/**
sub a number of seconds to this time */
UTime operator- (float seconds);
/**
Add a number of decimal seconds to this time. */
inline void operator+= (float seconds)
{ add(seconds); };
/**
Add a number of decimal seconds to this time. */
inline void operator-= (float seconds)
{ sub(seconds); };
/**
Add this number of seconds to the current value */
void add(float seconds);
/**
Subtract a number of seconds from this time.
Can not handle negative time, and seconds must be positive. */
void sub(float seconds);
/**
Convert seconds to time_tm strucure.
\param when 'local' is true the local time is returned, else GMT.
\return the structure with year (year 1900 == 0), month, day, hour, min and sec. */
struct tm getTimeTm(bool local = true);
/**
Get copy of timevalue structure */
inline struct timeval getTimeval()
{
return time;
}
/**
Get month number form 3 character string.
String value must match one of:
Jan Feb Mas Apr May Jun Jul Aug Sep Oct Nov Dec.
Returns 0 if no match were found. */
int getMdrFromString(const char * month3char);
/**
Show date and time on console */
void show(const char * prestring = nullptr);
/**
print date and time on console */
inline void print(const char * prestring = nullptr)
{ show(prestring); };
public:
/**
Time as 'timeval' - i.e. same format as in 'timeofday' call. */
timeval time;
/**
A valid flag, that are used when setting the time */
bool valid;
};
#endif