libcommonc++  0.7
TimeSpan.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_TimeSpan_hxx
24 #define __ccxx_TimeSpan_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/String.h++>
28 
29 namespace ccxx {
30 
37 {
38  public:
39 
45  TimeSpan(int64_t span = INT64_CONST(0));
46 
51  TimeSpan(int days, int hours, int minutes, int seconds, int msec = 0);
52 
54  TimeSpan(const TimeSpan& other);
55 
57  ~TimeSpan();
58 
60  void setSpan(int days, int hours, int minutes, int seconds, int msec = 0);
61 
63  void setSpan(int64_t seconds);
64 
66  inline int64_t getSpan() const
67  { return(_span); }
68 
70  void setDays(int days);
71 
73  void addDays(int days);
74 
76  void subtractDays(int days);
77 
79  inline int getDays() const
80  { return(_days); }
81 
83  void setHours(int hours);
84 
86  void addHours(int hours);
87 
89  void subtractHours(int hours);
90 
92  inline int getHours() const
93  { return(_hours); }
94 
96  void setMinutes(int minutes);
97 
99  void addMinutes(int minutes);
100 
102  void subtractMinutes(int minutes);
103 
105  inline int getMinutes() const
106  { return(_minutes); }
107 
109  void setSeconds(int seconds);
110 
112  void addSeconds(int seconds);
113 
115  void subtractSeconds(int seconds);
116 
118  inline int getSeconds() const
119  { return(_seconds); }
120 
122  void setMilliseconds(int msec);
123 
125  void addMilliseconds(int msec);
126 
128  void subtractMilliseconds(int msec);
129 
131  inline int getMilliseconds() const
132  { return(_msec); }
133 
134  TimeSpan& operator=(const TimeSpan& other);
135 
136  inline bool operator==(const TimeSpan& other) const
137  { return(_span == other._span); }
138 
139  inline bool operator<(const TimeSpan& other) const
140  { return(_span < other._span); }
141 
142  inline bool operator<=(const TimeSpan& other) const
143  { return(_span <= other._span); }
144 
145  inline bool operator!=(const TimeSpan& other) const
146  { return(_span != other._span); }
147 
148  inline bool operator>(const TimeSpan& other) const
149  { return(_span > other._span); }
150 
151  inline bool operator>=(const TimeSpan& other) const
152  { return(_span >= other._span); }
153 
154  TimeSpan& operator+=(const TimeSpan& span);
155  TimeSpan& operator+=(int seconds);
156  TimeSpan& operator-=(const TimeSpan& span);
157  TimeSpan& operator-=(int seconds);
158 
159  TimeSpan operator-() const;
160 
162  inline int64_t toSeconds() const
163  { return(_span / msInSecond); }
164 
166  inline operator int64_t() const
167  { return(_span); }
168 
170  inline int64_t toMilliseconds() const
171  { return(_span); }
172 
174  String toString() const;
175 
177  static const int msInSecond;
178 
180  static const int msInMinute;
181 
183  static const int msInHour;
184 
186  static const int msInDay;
187 
188  private:
189 
190  void _computeFields(int days, int hours, int minutes, int seconds, int msec);
191  void _computeFields(int64_t span);
192 
193  int _days;
194  int _hours;
195  int _minutes;
196  int _seconds;
197  int _msec;
198  int64_t _span;
199 };
200 
201 } // namespace ccxx
202 
203 #endif // __ccxx_TimeSpan_hxx
const TimeSpan operator-(const TimeSpan &s1, const TimeSpan &s2)
Definition: TimeSpan.c++:386
A span of time (a delta), represented in milliseconds.
Definition: TimeSpan.h++:36
static const int msInSecond
The number of milliseconds in a second.
Definition: TimeSpan.h++:177
int getMilliseconds() const
Get the milliseconds component of the timespan.
Definition: TimeSpan.h++:131
bool operator<=(const TimeSpan &other) const
Definition: TimeSpan.h++:142
bool operator==(const TimeSpan &other) const
Definition: TimeSpan.h++:136
bool operator>(const TimeSpan &other) const
Definition: TimeSpan.h++:148
int64_t toMilliseconds() const
Get the timespan in milliseconds.
Definition: TimeSpan.h++:170
int getSeconds() const
Get the seconds component of the timespan.
Definition: TimeSpan.h++:118
bool operator!=(const TimeSpan &other) const
Definition: TimeSpan.h++:145
int getMinutes() const
Get the minutes component of the timespan.
Definition: TimeSpan.h++:105
int64_t getSpan() const
Get the timespan, in milliseconds.
Definition: TimeSpan.h++:66
#define COMMONCPP_API
Definition: Common.h++:126
#define INT64_CONST(I)
Declare a signed 64-bit integer constant I.
Definition: Integers.h++:122
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
static const int msInDay
The number of milliseconds in a day.
Definition: TimeSpan.h++:186
int64_t toSeconds() const
Get the timespan in seconds.
Definition: TimeSpan.h++:162
static const int msInHour
The number of milliseconds in an hour.
Definition: TimeSpan.h++:183
int getDays() const
Get the days component of the timespan.
Definition: TimeSpan.h++:79
static const int msInMinute
The number of milliseconds in a minute.
Definition: TimeSpan.h++:180
int getHours() const
Get the hours component of the timespan.
Definition: TimeSpan.h++:92
Definition: AllocationMap.c++:25
bool operator<(const TimeSpan &other) const
Definition: TimeSpan.h++:139
bool operator>=(const TimeSpan &other) const
Definition: TimeSpan.h++:151