libDwm-0.9.45
DwmTimeValue.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2005-2007, 2016, 2024
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
32// PURPOSE, OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMTIMEVALUE_HH_
42#define _DWMTIMEVALUE_HH_
43
44extern "C" {
45 #include <sys/types.h>
46 #include <sys/time.h>
47#if ((! __APPLE__) && (! __linux__))
48 #include <sys/timespec.h>
49#endif
50 #include <sys/uio.h>
51 #include <unistd.h>
52 #include <bzlib.h>
53 #include <zlib.h>
54}
55
56#include <cstdint>
57#include <cstdio>
58#include <ctime>
59#include <iostream>
60
61namespace Dwm {
62
63 //--------------------------------------------------------------------------
66 //--------------------------------------------------------------------------
68 {
69 public:
70 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
74 TimeValue(bool setNow = false);
75
76 //------------------------------------------------------------------------
78 //------------------------------------------------------------------------
79 TimeValue(const struct timeval & tv);
80
81 TimeValue(const struct timespec & ts);
82
83 //------------------------------------------------------------------------
85 //------------------------------------------------------------------------
86 TimeValue(uint32_t secs, uint32_t usecs);
87
88 //------------------------------------------------------------------------
90 //------------------------------------------------------------------------
91 void SetNow();
92
93 //------------------------------------------------------------------------
95 //------------------------------------------------------------------------
96 void Set(const struct timeval & tv);
97
98 //------------------------------------------------------------------------
100 //------------------------------------------------------------------------
101 void Set(uint32_t secs, uint32_t usecs);
102
103 //------------------------------------------------------------------------
105 //------------------------------------------------------------------------
106 uint32_t Secs() const;
107
108 //------------------------------------------------------------------------
110 //------------------------------------------------------------------------
111 uint32_t Usecs() const;
112
113 struct tm LocalTime() const;
114
115 //------------------------------------------------------------------------
117 //------------------------------------------------------------------------
118 bool operator < (const TimeValue & tv) const;
119
120 //------------------------------------------------------------------------
122 //------------------------------------------------------------------------
123 bool operator <= (const TimeValue & tv) const;
124
125 //------------------------------------------------------------------------
127 //------------------------------------------------------------------------
128 bool operator > (const TimeValue & tv) const;
129
130 //------------------------------------------------------------------------
132 //------------------------------------------------------------------------
133 bool operator >= (const TimeValue & tv) const;
134
135 //------------------------------------------------------------------------
137 //------------------------------------------------------------------------
138 bool operator == (const TimeValue & tv) const;
139
140 TimeValue & operator += (const TimeValue & tv);
141 TimeValue & operator -= (const TimeValue & tv);
142 TimeValue & operator *= (uint32_t x);
143 TimeValue & operator /= (uint32_t x);
144
145 //------------------------------------------------------------------------
148 //------------------------------------------------------------------------
149 uint64_t StreamedLength() const;
150
151 //------------------------------------------------------------------------
153 //------------------------------------------------------------------------
154 std::istream & Read(std::istream & is);
155
156 //------------------------------------------------------------------------
158 //------------------------------------------------------------------------
159 std::ostream & Write(std::ostream & os) const;
160
161 //------------------------------------------------------------------------
164 //------------------------------------------------------------------------
165 size_t Read(FILE *f);
166
167 //------------------------------------------------------------------------
170 //------------------------------------------------------------------------
171 size_t Write(FILE *f) const;
172
173 //------------------------------------------------------------------------
176 //------------------------------------------------------------------------
177 ssize_t Read(int fd);
178
179 //------------------------------------------------------------------------
182 //------------------------------------------------------------------------
183 ssize_t Write(int fd) const;
184
185 //------------------------------------------------------------------------
188 //------------------------------------------------------------------------
189 int Read(gzFile gzf);
190
191 //------------------------------------------------------------------------
194 //------------------------------------------------------------------------
195 int Write(gzFile gzf) const;
196
197 //------------------------------------------------------------------------
200 //------------------------------------------------------------------------
201 int BZRead(BZFILE *bzf);
202
203 //------------------------------------------------------------------------
206 //------------------------------------------------------------------------
207 int BZWrite(BZFILE *bzf) const;
208
209 //--------------------------------------------------------------------
211 //--------------------------------------------------------------------
212 operator double () const;
213
214 private:
215 uint32_t _secs;
216 uint32_t _usecs;
217 };
218
219} // namespace Dwm
220
221Dwm::TimeValue operator + (const Dwm::TimeValue & tv1,
222 const Dwm::TimeValue & tv2);
223Dwm::TimeValue operator - (const Dwm::TimeValue & tv1,
224 const Dwm::TimeValue & tv2);
225
226#endif // _DWMTIMEVALUE_HH_
227
228//---------------------------- emacs settings -----------------------------
229// Local Variables:
230// mode: C++
231// tab-width: 2
232// indent-tabs-mode: nil
233// c-basic-offset: 2
234// End:
235//-------------------------------------------------------------------------
This class encapsulates a time value beyond the UNIX epoch, with microsecond granularity.
Definition DwmTimeValue.hh:68
bool operator<(const TimeValue &tv) const
Less-than operator.
TimeValue(const struct timeval &tv)
Construct from a struct timeval.
uint64_t StreamedLength() const
Returns the number of bytes that would be written if one of the Write() members were called.
int Write(gzFile gzf) const
Writes the TimeValue to a gzFile.
bool operator==(const TimeValue &tv) const
Equal-to operator.
int BZRead(BZFILE *bzf)
Reads the TimeValue from a BZFILE pointer.
uint32_t Secs() const
Returns the seconds.
TimeValue(bool setNow=false)
Constructor.
std::istream & Read(std::istream &is)
Reads the TimeValue from an istream. Returns the istream.
void Set(uint32_t secs, uint32_t usecs)
Sets the TimeValue from the given secs and usecs.
size_t Write(FILE *f) const
Writes the TimeValue to a FILE pointer.
bool operator>(const TimeValue &tv) const
Greater-than operator.
std::ostream & Write(std::ostream &os) const
Writes the TimeValue to an ostream. Returns the ostream.
TimeValue(uint32_t secs, uint32_t usecs)
Construct from the given secs and usecs.
ssize_t Read(int fd)
Reads the TimeValue from a file descriptor.
void Set(const struct timeval &tv)
Sets the TimeValue from the given struct timeval tv.
uint32_t Usecs() const
Returns the residual microseconds.
ssize_t Write(int fd) const
Writes the TimeValue to a file descriptor.
bool operator>=(const TimeValue &tv) const
operator >=
int BZWrite(BZFILE *bzf) const
Writes the TimeValue to a BZFILE pointer.
void SetNow()
Sets the TimeValue to the current time.
int Read(gzFile gzf)
Reads the TimeValue from a gzFile.
size_t Read(FILE *f)
Reads the TimeValue from a FILE pointer.
bool operator<=(const TimeValue &tv) const
operator <=