The Battle for Wesnoth  1.15.3+dev
time_span.cpp
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 #include <mariadb++/time_span.hpp>
11 
12 using namespace mariadb;
13 
14 time_span::time_span(u32 days, u8 hours, u8 minutes, u8 seconds, u16 milliseconds, bool negative) {
16 }
17 
19  set(dur.days(), dur.hours(), dur.minutes(), dur.seconds(), dur.milliseconds(), dur.negative());
20 }
21 
22 int time_span::compare(const time_span& ts) const {
23  if (negative() && !ts.negative()) return -1;
24 
25  if (!negative() && ts.negative()) return 1;
26 
27  if (zero() && ts.zero()) return 0;
28 
29  if (days() < ts.days()) return -1;
30 
31  if (days() > ts.days()) return 1;
32 
33  if (hours() < ts.hours()) return -1;
34 
35  if (hours() > ts.hours()) return 1;
36 
37  if (minutes() < ts.minutes()) return -1;
38 
39  if (minutes() > ts.minutes()) return 1;
40 
41  if (seconds() < ts.seconds()) return -1;
42 
43  if (seconds() > ts.seconds()) return 1;
44 
45  if (milliseconds() < ts.milliseconds()) return -1;
46 
47  return milliseconds() == ts.milliseconds() ? 0 : 1;
48 }
49 
51  set(ts.days(), ts.hours(), ts.minutes(), ts.seconds(), ts.milliseconds(), ts.negative());
52 
53  return *this;
54 }
55 
56 bool time_span::operator==(const time_span& ts) const { return compare(ts) == 0; }
57 
58 bool time_span::operator!=(const time_span& ts) const { return compare(ts) != 0; }
59 
60 bool time_span::operator<(const time_span& ts) const { return compare(ts) < 0; }
61 
62 bool time_span::operator<=(const time_span& ts) const { return compare(ts) <= 0; }
63 
64 bool time_span::operator>(const time_span& ts) const { return compare(ts) > 0; }
65 
66 bool time_span::operator>=(const time_span& ts) const { return compare(ts) >= 0; }
67 
68 void time_span::set(u32 _days, u8 _hours, u8 _minutes, u8 _seconds, u16 _milliseconds,
69  bool _negative) {
70  negative(_negative);
71  days(_days);
72  hours(_hours);
73  minutes(_minutes);
74  seconds(_seconds);
75  milliseconds(_milliseconds);
76 }
77 
78 bool time_span::zero() const {
79  return m_days == 0 && m_hours == 0 && m_minutes == 0 && m_seconds == 0 && m_milliseconds == 0;
80 }
81 
82 u32 time_span::days() const { return m_days; }
83 
84 u32 time_span::days(u32 days) { return (m_days = days); }
85 
86 u8 time_span::hours() const { return m_hours; }
87 
89  if (hours > 23) throw std::invalid_argument("Hours must be < 24");
90 
91  return (m_hours = hours);
92 }
93 
94 u8 time_span::minutes() const { return m_minutes; }
95 
97  if (minutes > 59) throw std::invalid_argument("Hours must be < 60");
98 
99  return (m_minutes = minutes);
100 }
101 
102 u8 time_span::seconds() const { return m_seconds; }
103 
105  if (seconds > 60) throw std::invalid_argument("Hours must be < 61");
106 
107  return (m_seconds = seconds);
108 }
109 
111 
113  if (milliseconds > 999) throw std::invalid_argument("Hours must be < 1000");
114 
115  return (m_milliseconds = milliseconds);
116 }
117 
118 bool time_span::negative() const { return m_negative; }
119 
120 bool time_span::negative(bool negative) { return (m_negative = negative); }
121 
122 u64 time_span::total_hours() const { return m_days * 24u + m_hours; }
123 
124 u64 time_span::total_minutes() const { return total_hours() * 60u + m_minutes; }
125 
127 
129 
130 std::ostream& mariadb::operator<<(std::ostream& os, const time_span& ts) {
131  if (ts.negative()) os << "negative ";
132 
133  os << ts.days() << " days, " << +ts.hours() << " hours, " << +ts.minutes() << " minutes, "
134  << +ts.seconds() << " seconds, " << ts.milliseconds() << " milliseconds";
135 
136  return os;
137 }
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
void set(u32 days=0, u8 hours=0, u8 minutes=0, u8 seconds=0, u16 milliseconds=0, bool negative=false)
Set the value of this time_span to given values.
Definition: time_span.cpp:68
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