Android release v6.7_preview1
[xcsoar.git] / src / Time / BrokenTime.hpp
blob9ab1caad8270131d379393adc0a2655842908cf1
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #ifndef XCSOAR_BROKEN_TIME_HPP
25 #define XCSOAR_BROKEN_TIME_HPP
27 #include "Compiler.h"
29 #include <type_traits>
31 #include <stdint.h>
33 /**
34 * A broken-down representation of a time.
36 struct BrokenTime {
37 /**
38 * Hour of day, 0-23.
40 uint8_t hour;
42 /**
43 * Minute, 0-59.
45 uint8_t minute;
47 /**
48 * Second, 0-59.
50 uint8_t second;
52 /**
53 * Non-initializing default constructor.
55 BrokenTime() = default;
57 constexpr
58 BrokenTime(unsigned _hour, unsigned _minute, unsigned _second=0)
59 :hour(_hour), minute(_minute), second(_second) {}
61 constexpr
62 bool operator==(const BrokenTime other) const {
63 return hour == other.hour && minute == other.minute &&
64 second == other.second;
67 constexpr
68 bool operator>(const BrokenTime other) const {
69 return hour > other.hour ||
70 (hour == other.hour && (minute > other.minute ||
71 (minute == other.minute && second > other.second)));
74 /**
75 * Returns an instance that fails the Plausible() check.
77 constexpr
78 static BrokenTime Invalid() {
79 return BrokenTime(24, 60, 60);
82 /**
83 * Does this object contain plausible values?
85 constexpr
86 bool Plausible() const {
87 return hour < 24 && minute < 60 && second < 60;
90 /**
91 * Returns the number of seconds which have passed on this day.
93 constexpr
94 unsigned GetSecondOfDay() const {
95 return hour * 3600u + minute * 60u + second;
98 /**
99 * Construct a BrokenTime object from the specified number of
100 * seconds which have passed on this day.
102 * @param second_of_day 0 .. 3600*24-1
104 gcc_const
105 static BrokenTime FromSecondOfDay(unsigned second_of_day);
108 * A wrapper for FromSecondOfDay() which allows values bigger than
109 * or equal to 3600*24.
111 gcc_const
112 static BrokenTime FromSecondOfDayChecked(unsigned second_of_day);
115 * Returns a BrokenTime that has the specified number of seconds
116 * added to it. It properly wraps around midnight.
118 * @param seconds the number of seconds to add
120 gcc_pure
121 BrokenTime operator+(unsigned seconds) const;
124 * Returns a BrokenTime that has the specified number of seconds
125 * added to it. It properly wraps around midnight.
127 * @param seconds the number of seconds to add; may be negative
129 gcc_pure
130 BrokenTime operator+(int seconds) const;
132 gcc_pure
133 BrokenTime operator-(int seconds) const {
134 return *this + (-seconds);
137 gcc_pure
138 BrokenTime operator-(unsigned seconds) const {
139 return *this - int(seconds);
143 static_assert(std::is_trivial<BrokenTime>::value, "type is not trivial");
145 #endif