The Battle for Wesnoth  1.15.3+dev
time_span.hpp
Go to the documentation of this file.
1 //
2 // M A R I A D B + +
3 //
4 // Copyright Sylvain Rochette Langlois 2013,
5 // The ViaDuck Project 2016 - 2018.
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 #ifndef _MARIADB_TIME_SPAN_HPP_
11 #define _MARIADB_TIME_SPAN_HPP_
12 
13 #include <sstream>
14 #include <mariadb++/types.hpp>
15 
16 namespace mariadb {
17 class time_span {
18  public:
19  /**
20  * Construct time_span from given data. Note that a time_span can span one day at most.
21  *
22  * @param hours Number of hours 0-23
23  * @param minutes Number of minutes 0-59
24  * @param seconds Number of seconds 0-59
25  * @param milliseconds Number of milliseconds 0-999
26  * @param negative Indicates negative timespan
27  */
28  time_span(u32 days = 0, u8 hours = 0, u8 minutes = 0, u8 seconds = 0, u16 milliseconds = 0,
29  bool negative = false);
30 
31  /**
32  * Copy constructor
33  *
34  * @param dur Timespan to copy from
35  */
36  time_span(const time_span& dur);
37 
38  /**
39  * Compares this timespan with the given timespan.
40  *
41  * @param rhs Timespan to compare this to
42  * @return 1 if this is greater, 0 if equal, -1 if this is smaller
43  */
44  int compare(const time_span& rhs) const;
45 
46  time_span& operator=(const time_span& rhs);
47  bool operator==(const time_span& rhs) const;
48  bool operator!=(const time_span& rhs) const;
49  bool operator<(const time_span& rhs) const;
50  bool operator<=(const time_span& rhs) const;
51  bool operator>(const time_span& rhs) const;
52  bool operator>=(const time_span& rhs) const;
53 
54  /**
55  * Set the value of this time_span to given values
56  *
57  * @param days Any number of days
58  * @param hours Number of hours 0-23
59  * @param minutes Number of minutes 0-59
60  * @param seconds Number of seconds 0-59
61  * @param milliseconds Number of milliseconds 0-999
62  * @param negative Indicates negative timespan
63  */
64  void set(u32 days = 0, u8 hours = 0, u8 minutes = 0, u8 seconds = 0, u16 milliseconds = 0,
65  bool negative = false);
66 
67  /**
68  * Indicates whether this time_span is zero. That is only true if all components are zero
69  *
70  * @return True if zero
71  */
72  bool zero() const;
73 
74  /**
75  * Indicates whether this time_span is negative.
76  *
77  * @return True if negative
78  */
79  bool negative() const;
80 
81  /**
82  * Sets negative flag on this time_span
83  *
84  * @param negative Value to set
85  * @return Newly set value
86  */
87  bool negative(bool negative);
88 
89  /**
90  * Get number of days
91  *
92  * @return Number of days
93  */
94  u32 days() const;
95 
96  /**
97  * Sets the number of days
98  *
99  * @param hour Number of days to set
100  * @return Newly set value
101  */
102  u32 days(u32 day);
103 
104  /**
105  * Get number of hours
106  *
107  * @return Number of hours
108  */
109  u8 hours() const;
110 
111  /**
112  * Sets the number of hours
113  *
114  * @param hour Number of hours to set
115  * @return Newly set value
116  */
117  u8 hours(u8 hour);
118 
119  /**
120  * Get number of hours
121  *
122  * @return Number of hours
123  */
124  u8 minutes() const;
125 
126  /**
127  * Sets the number of minutes
128  *
129  * @param minute Number of minutes to set
130  * @return Newly set value
131  */
132  u8 minutes(u8 minute);
133 
134  /**
135  * Get number of seconds
136  *
137  * @return Number of seconds
138  */
139  u8 seconds() const;
140 
141  /**
142  * Sets the number of seconds
143  *
144  * @param second Number of seconds to set
145  * @return Newly set value
146  */
147  u8 seconds(u8 second);
148 
149  /**
150  * Get number of milliseconds
151  *
152  * @return Number of milliseconds
153  */
154  u16 milliseconds() const;
155 
156  /**
157  * Sets the number of milliseconds
158  *
159  * @param millisecond Number of milliseconds to set
160  * @return Newly set value
161  */
162  u16 milliseconds(u16 millisecond);
163 
164  /**
165  * Converts the time_span to minutes
166  *
167  * @return Total number of minutes in this time_span
168  */
169  u64 total_hours() const;
170 
171  /**
172  * Converts the time_span to minutes
173  *
174  * @return Total number of minutes in this time_span
175  */
176  u64 total_minutes() const;
177 
178  /**
179  * Converts the time_span to seconds
180  *
181  * @return Total number of seconds in this time_span
182  */
183  u64 total_seconds() const;
184 
185  /**
186  * Converts the time_span to milliseconds
187  *
188  * @return Total number of milliseconds in this time_span
189  */
190  u64 total_milliseconds() const;
191 
192  private:
193  bool m_negative = false;
194  u32 m_days = 0;
195  u8 m_hours = 0;
199 };
200 
201 std::ostream& operator<<(std::ostream& os, const time_span& ts);
202 }
203 
204 #endif
bool operator>(const time_span &rhs) const
Definition: time_span.cpp:64
u8 seconds() const
Get number of seconds.
Definition: time_span.cpp:102
bool operator==(const time_span &rhs) const
Definition: time_span.cpp:56
bool operator<(const time_span &rhs) const
Definition: time_span.cpp:60
u64 total_hours() const
Converts the time_span to minutes.
Definition: time_span.cpp:122
std::ostream & operator<<(std::ostream &os, const date_time &ddt)
Definition: date_time.cpp:587
u32 days() const
Get number of days.
Definition: time_span.cpp:82
u16 milliseconds() const
Get number of milliseconds.
Definition: time_span.cpp:110
time_span & operator=(const time_span &rhs)
Definition: time_span.cpp:50
u64 total_milliseconds() const
Converts the time_span to milliseconds.
Definition: time_span.cpp:128
unsigned char u8
Definition: types.hpp:20
unsigned short u16
Definition: types.hpp:21
bool operator!=(const time_span &rhs) const
Definition: time_span.cpp:58
u64 total_minutes() const
Converts the time_span to minutes.
Definition: time_span.cpp:124
time_span(u32 days=0, u8 hours=0, u8 minutes=0, u8 seconds=0, u16 milliseconds=0, bool negative=false)
Construct time_span from given data.
Definition: time_span.cpp:14
int compare(const time_span &rhs) const
Compares this timespan with the given timespan.
Definition: time_span.cpp:22
bool operator<=(const time_span &rhs) const
Definition: time_span.cpp:62
bool negative() const
Indicates whether this time_span is negative.
Definition: time_span.cpp:118
bool operator>=(const time_span &rhs) const
Definition: time_span.cpp:66
u8 minutes() const
Get number of hours.
Definition: time_span.cpp:94
unsigned long long u64
Definition: types.hpp:34
u64 total_seconds() const
Converts the time_span to seconds.
Definition: time_span.cpp:126
unsigned int u32
Definition: types.hpp:22
u8 hours() const
Get number of hours.
Definition: time_span.cpp:86
bool zero() const
Indicates whether this time_span is zero.
Definition: time_span.cpp:78