-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeInterval.h
91 lines (82 loc) · 2.76 KB
/
TimeInterval.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
/**
* \class TimeInterval
*
* \brief Storage and manipulation of time intervals
*
* This class stores time intervals that may be as long as 10^16 years
* (when sizeof(long) == 8) while having microsecond resolution.
* Basic arithmetic for these intervals is supported for the following
* operations:
* - addition
* - subtraction
* - multiplication of an interval by an integer
* - comparison (==, !=, <, <=, >, >=)
*
* Printing of a TimeInterval is supported through the extraction
* operator <<, which causes the interval to be printed as [X days, X.XXXXXX s].
*
* The value of a time interval is specified as a sum of days, seconds,
* and microseconds. Negative values for any of these components are
* supported, e.g., -1 day plus 500000 microseconds is equivalent to
* an interval of -86399.5 seconds.
*
* Examples:
*
* TimeInterval a(1, 0, 0); // Interval of 1 day
* TimeInterval b(0, 1, 500000); // Interval of 1.5 seconds
* TimeInterval zero; // Intervals default to 0 days 0.0 s
*
* while (a > zero) {
* a -= b;
* b *= 2;
* std::cout << a << std::endl;
* }
*
* \author Michael G. Duda, NCAR/MMM
*
* \date 8 July 2018
*
*/
#ifndef TIMEINTERVAL_H
#define TIMEINTERVAL_H
#include <iostream>
class TimeInterval
{
public:
//
// Constructors and destructors
//
TimeInterval();
TimeInterval(long days, long seconds, long u_seconds);
~TimeInterval();
//
// Set an interval to a specified length of time
//
void setInterval(long days, long seconds, long u_seconds);
//
// Operators
//
TimeInterval operator+(const TimeInterval &inc) const;
TimeInterval& operator+=(const TimeInterval &inc);
TimeInterval operator-(const TimeInterval &dec) const;
TimeInterval& operator-=(const TimeInterval &dec);
TimeInterval operator*(long n) const;
TimeInterval& operator*=(long n);
friend bool operator==(const TimeInterval &t1, const TimeInterval &t2);
friend bool operator!=(const TimeInterval &t1, const TimeInterval &t2);
friend bool operator<(const TimeInterval &t1, const TimeInterval &t2);
friend bool operator<=(const TimeInterval &t1, const TimeInterval &t2);
friend bool operator>(const TimeInterval &t1, const TimeInterval &t2);
friend bool operator>=(const TimeInterval &t1, const TimeInterval &t2);
friend std::ostream& operator<<(std::ostream& output, const TimeInterval& p);
friend TimeInterval operator*(long n, const TimeInterval &t);
private:
long intervalDays; // Number of days in the interval
long intervalSeconds; // Number of seconds in the interval
long intervalMicroSeconds; // Number of microseconds in the interval
//
// Private methods
//
void normalize(); // Keep microseconds in the range [0,999999], keep seconds in the range [0,86399]
};
#endif