PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timeint.h
Go to the documentation of this file.
1 /*
2  * timeint.h
3  *
4  * Millisecond resolution time interval class (uses 64 bit integers).
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 #ifndef PTLIB_TIMEINTERVAL_H
31 #define PTLIB_TIMEINTERVAL_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 
39 // Difference between two system times
40 
51 class PTimeInterval : public PObject
52 {
53  PCLASSINFO(PTimeInterval, PObject);
54 
55  public:
70  PInt64 millisecs = 0
71  );
73  PInt64 millisecs,
74  long seconds,
75  long minutes = 0,
76  long hours = 0,
77  int days = 0
78  );
80  const PString & str
81  );
82 
83  PTimeInterval(const PTimeInterval & other);
84  PTimeInterval & operator=(const PTimeInterval & other);
85 
88  int64_t nsecs,
89  int secs = 0
90  );
91 
94  int64_t usecs,
95  int secs = 0
96  );
97 
99  static PTimeInterval Seconds(
100  double secs
101  );
102 
104  static PTimeInterval Frequency(
105  double frequency
106  );
108 
117  PObject * Clone() const;
118 
126  virtual Comparison Compare(
127  const PObject & obj
128  ) const;
129 
139  virtual void PrintOn(
140  ostream & strm
141  ) const;
142 
146  virtual void ReadFrom(
147  istream & strm
148  );
150 
153  enum Formats {
158  };
159 
166  int decimals = 3,
167  Formats format = NormalFormat,
168  int width = 1
169  ) const;
171 
179  PInt64 GetNanoSeconds() const;
180 
183  void SetNanoSeconds(
184  int64_t nsecs,
185  int secs = 0
186  );
187 
193  PInt64 GetMicroSeconds() const;
194 
197  void SetMicroSeconds(
198  int64_t usecs,
199  int secs = 0
200  );
201 
204  double GetFrequency() const;
205 
208  void SetFrequency(double frequency);
209 
215  PInt64 GetMilliSeconds() const;
216 
219  void SetMilliSeconds(PInt64 msecs);
220 
226  long GetSeconds() const;
227 
233  double GetSecondsAsDouble() const;
234 
237  void SetSeconds(
238  double secs
239  );
240 
246  long GetMinutes() const;
247 
253  int GetHours() const;
254 
260  int GetDays() const;
261 
262 #ifdef _WIN32
263  typedef DWORD IntervalType;
264 #else
265  typedef int IntervalType;
266 #endif
267 
277  IntervalType GetInterval() const;
278 
290  void SetInterval(
291  PInt64 milliseconds = 0,
292  long seconds = 0,
293  long minutes = 0,
294  long hours = 0,
295  int days = 0
296  );
298 
306  PTimeInterval operator-() const;
307 
314  const PTimeInterval & interval
315  ) const;
316  friend PTimeInterval operator+(int64_t left, const PTimeInterval & right);
317 
324  const PTimeInterval & interval
325  );
326 
333  const PTimeInterval & interval
334  ) const;
335  friend PTimeInterval operator-(int64_t left, const PTimeInterval & right);
336 
343  const PTimeInterval & interval
344  );
345 
352  int factor
353  ) const;
354  friend PTimeInterval operator*(int left, const PTimeInterval & right);
355 
362  int factor
363  );
364 
370  int operator/(
371  const PTimeInterval & smaller
372  ) const;
373 
380  int factor
381  ) const;
382  friend PTimeInterval operator/(int64_t left, const PTimeInterval & right);
383 
390  int factor
391  );
393 
403  bool operator==(
404  const PTimeInterval & interval
405  ) const;
406  bool operator==(
407  long msecs
408  ) const;
409 
417  bool operator!=(
418  const PTimeInterval & interval
419  ) const;
420  bool operator!=(
421  long msecs
422  ) const;
423 
431  bool operator> (
432  const PTimeInterval & interval
433  ) const;
434  bool operator> (
435  long msecs
436  ) const;
437 
445  bool operator>=(
446  const PTimeInterval & interval
447  ) const;
448  bool operator>=(
449  long msecs
450  ) const;
451 
459  bool operator< (
460  const PTimeInterval & interval
461  ) const;
462  bool operator< (
463  long msecs
464  ) const;
465 
473  bool operator<=(
474  const PTimeInterval & interval
475  ) const;
476  bool operator<=(
477  long msecs
478  ) const;
480 
481  static int64_t const MicroToNano = 1000;
482  static int64_t const MilliToNano = MicroToNano*1000;
483  static int64_t const SecsToNano = MilliToNano*1000;
484  static int64_t const MinsToNano = SecsToNano*60;
485  static int64_t const HoursToNano = MinsToNano*60;
486  static int64_t const DaysToNano = HoursToNano*24;
487 
488  protected:
489  virtual int64_t InternalGet() const;
490  virtual void InternalSet(int64_t t);
491 
492  private:
493  atomic<int64_t> m_nanoseconds;
494 
495 
496 // Include platform dependent part of class
497 #ifdef _WIN32
498 #include "msos/ptlib/timeint.h"
499 #else
500 #include "unix/ptlib/timeint.h"
501 #endif
502 };
503 
504 
507 {
508  PNanoSeconds(int64_t nanoseconds) { SetNanoSeconds(nanoseconds); }
509 };
510 
513 {
514  PMicroSeconds(int64_t microseconds) { SetMicroSeconds(microseconds); }
515 };
516 
519 
522 {
523  PFrequencyTime(double frequency) { SetFrequency(frequency); }
524 };
525 
526 
527 namespace std
528 {
530  template<> class numeric_limits<PTimeInterval>
531  {
532  public:
533  static PTimeInterval min() { return -PMaxTimeInterval; }
534  static PTimeInterval max() { return PMaxTimeInterval; }
537  static PTimeInterval denorm_min() { return PTimeInterval::NanoSeconds(numeric_limits<int64_t>::denorm_min()); }
538  static PTimeInterval infinity() { return PTimeInterval::NanoSeconds(numeric_limits<int64_t>::infinity()); }
539  static PTimeInterval quiet_NaN() { return PTimeInterval::NanoSeconds(numeric_limits<int64_t>::quiet_NaN()); }
540  static PTimeInterval signaling_NaN() { return PTimeInterval::NanoSeconds(numeric_limits<int64_t>::signaling_NaN()); }
541  };
542 };
543 
544 
545 #endif // PTLIB_TIMEINTERVAL_H
546 
547 
548 // End Of File ///////////////////////////////////////////////////////////////
PFrequencyTime(double frequency)
Definition: timeint.h:523
double GetSecondsAsDouble() const
Get the number of seconds, and partial seconds, as floating point, for the time interval.
#define PMaxTimeInterval
Definition: timeint.h:31
Output as &quot;hh:mm:ss.uuu&quot;.
Definition: timeint.h:154
Class wrapper of PTimeInterval to initialise to a frequency.
Definition: timeint.h:521
void SetMilliSeconds(PInt64 msecs)
Set the number of milliseconds for the time interval.
PTimeInterval operator-() const
Unary minus, get negative of time interval.
virtual void InternalSet(int64_t t)
static int64_t const MilliToNano
Definition: timeint.h:482
Ouptut using SI units, ns, us, ms, s, though not ks, Ms etc.
Definition: timeint.h:157
PTimeInterval operator+(const PTimeInterval &interval) const
Add the two time intervals yielding a third time interval.
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
static int64_t const SecsToNano
Definition: timeint.h:483
static PTimeInterval quiet_NaN()
Definition: timeint.h:539
PTimeInterval operator*(int factor) const
Multiply the time interval by a factor yielding a third time interval.
virtual void ReadFrom(istream &strm)
Input the time interval from the I/O stream.
PInt64 GetMicroSeconds() const
Get the number of microseconds for the time interval.
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
static PTimeInterval denorm_min()
Definition: timeint.h:537
static int64_t const MicroToNano
Definition: timeint.h:481
int IntervalType
Definition: timeint.h:265
bool operator<=(const PTimeInterval &interval) const
Compare to the two time intervals.
PInt64 GetNanoSeconds() const
Get the number of nanoseconds for the time interval.
virtual Comparison Compare(const PObject &obj) const
Rank the two time intervals.
PTimeInterval & operator-=(const PTimeInterval &interval)
Subtract the second time interval from the first time interval.
bool operator!=(const PTimeInterval &interval) const
Compare to the two time intervals.
Formats
Definition: timeint.h:153
void SetFrequency(double frequency)
Set time interval from a frequency.
Class wrapper of PTimeInterval to initialise to microseconds.
Definition: timeint.h:512
IntervalType GetInterval() const
Get the &quot;clamped&quot; number of milliseconds for the time interval.
bool operator==(const PTimeInterval &interval) const
Compare to the two time intervals.
double GetFrequency() const
Get time interval as a frequency.
static PTimeInterval round_error()
Definition: timeint.h:536
PTimeInterval(PInt64 millisecs=0)
Create a new time interval object.
static PTimeInterval signaling_NaN()
Definition: timeint.h:540
void SetMicroSeconds(int64_t usecs, int secs=0)
Set the number of microseconds for the time interval.
PTimeInterval & operator/=(int factor)
Divide the time interval by a factor.
static PTimeInterval Frequency(double frequency)
Return a PTimeInterval from a frequency.
PTimeInterval & operator=(const PTimeInterval &other)
static PTimeInterval min()
Definition: timeint.h:533
static PTimeInterval Seconds(double secs)
Return a PTimeInterval in seconds.
void SetInterval(PInt64 milliseconds=0, long seconds=0, long minutes=0, long hours=0, int days=0)
Set the value of the time interval.
Class wrapper of PTimeInterval to initialise to nanoseconds.
Definition: timeint.h:506
PTimeInterval PMilliSeconds
As we have PNanoSeconds &amp; PMicroSeconds, this is for completeness.
Definition: timeint.h:518
PTimeInterval & operator+=(const PTimeInterval &interval)
Add the second time interval to the first time interval.
The character string class.
Definition: pstring.h:108
void SetSeconds(double secs)
Set the number of seconds for the time interval.
static int64_t const DaysToNano
Definition: timeint.h:486
PMicroSeconds(int64_t microseconds)
Definition: timeint.h:514
Output as &quot;s.uuu&quot;.
Definition: timeint.h:156
virtual void PrintOn(ostream &strm) const
Output the time interval to the I/O stream.
static PTimeInterval max()
Definition: timeint.h:534
PNanoSeconds(int64_t nanoseconds)
Definition: timeint.h:508
static int64_t const HoursToNano
Definition: timeint.h:485
static PTimeInterval epsilon()
Definition: timeint.h:535
bool operator>(const PTimeInterval &interval) const
Compare to the two time intervals.
Output as &quot;d:hh:mm:ss.uuu&quot;.
Definition: timeint.h:155
virtual int64_t InternalGet() const
long GetMinutes() const
Get the number of whole minutes for the time interval.
bool operator>=(const PTimeInterval &interval) const
Compare to the two time intervals.
static PTimeInterval NanoSeconds(int64_t nsecs, int secs=0)
Return a PTimeInterval in nanoseconds.
void SetNanoSeconds(int64_t nsecs, int secs=0)
Set the number of nanoseconds for the time interval.
long GetSeconds() const
Get the number of whole seconds for the time interval.
bool operator<(const PTimeInterval &interval) const
Compare to the two time intervals.
int operator/(const PTimeInterval &smaller) const
Divide the time interval by another interval yielding a count.
int GetDays() const
Get the number of whole days for the time interval.
PInt64 GetMilliSeconds() const
Get the number of milliseconds for the time interval.
int GetHours() const
Get the number of whole hours for the time interval.
PObject * Clone() const
Create a new copy of the time interval.
PTimeInterval & operator*=(int factor)
Multiply the time interval by a factor.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
static PTimeInterval MicroSeconds(int64_t usecs, int secs=0)
Return a PTimeInterval in microseconds.
static PTimeInterval infinity()
Definition: timeint.h:538
PString AsString(int decimals=3, Formats format=NormalFormat, int width=1) const
Output the time interval as a string.
static int64_t const MinsToNano
Definition: timeint.h:484