4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2012 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_DATE_TIME_HPP
25 #define XCSOAR_DATE_TIME_HPP
27 #include "Util/TypeTraits.hpp"
33 * A broken-down representation of a date.
37 * Absolute year, e.g. "2010".
52 * Day of the week (0-6, 0: sunday)
54 unsigned char day_of_week
;
57 * Non-initializing default constructor.
59 BrokenDate() = default;
62 BrokenDate(unsigned _year
, unsigned _month
, unsigned _day
)
63 :year(_year
), month(_month
), day(_day
), day_of_week(-1) {}
66 bool operator==(const BrokenDate other
) const {
67 return year
== other
.year
&& month
== other
.month
&& day
== other
.day
;
71 bool operator>(const BrokenDate other
) const {
72 return year
> other
.year
||
73 (year
== other
.year
&& (month
> other
.month
||
74 (month
== other
.month
&& day
> other
.day
)));
78 * Clears the object, to make the Plausible() check returns false.
85 * Does this object contain plausible values?
88 bool Plausible() const {
89 return year
>= 1800 && year
<= 2500 &&
90 month
>= 1 && month
<= 12 &&
91 day
>= 1 && day
<= 31;
96 * A broken-down representation of a time.
115 * Non-initializing default constructor.
117 BrokenTime() = default;
120 BrokenTime(unsigned _hour
, unsigned _minute
, unsigned _second
=0)
121 :hour(_hour
), minute(_minute
), second(_second
) {}
124 bool operator==(const BrokenTime other
) const {
125 return hour
== other
.hour
&& minute
== other
.minute
&&
126 second
== other
.second
;
130 bool operator>(const BrokenTime other
) const {
131 return hour
> other
.hour
||
132 (hour
== other
.hour
&& (minute
> other
.minute
||
133 (minute
== other
.minute
&& second
> other
.second
)));
137 * Does this object contain plausible values?
140 bool Plausible() const {
141 return hour
< 24 && minute
< 60 && second
< 60;
145 * Returns the number of seconds which have passed on this day.
148 unsigned GetSecondOfDay() const {
149 return hour
* 3600u + minute
* 60u + second
;
153 * Construct a BrokenTime object from the specified number of
154 * seconds which have passed on this day.
156 * @param second_of_day 0 .. 3600*24-1
159 static BrokenTime
FromSecondOfDay(unsigned second_of_day
);
162 * A wrapper for FromSecondOfDay() which allows values bigger than
163 * or equal to 3600*24.
166 static BrokenTime
FromSecondOfDayChecked(unsigned second_of_day
);
170 * A broken-down representation of date and time.
172 struct BrokenDateTime
: public BrokenDate
, public BrokenTime
{
174 * Non-initializing default constructor.
176 BrokenDateTime() = default;
179 BrokenDateTime(unsigned _year
, unsigned _month
, unsigned _day
,
180 unsigned _hour
, unsigned _minute
, unsigned _second
=0)
181 :BrokenDate(_year
, _month
, _day
), BrokenTime(_hour
, _minute
, _second
) {}
184 BrokenDateTime(unsigned _year
, unsigned _month
, unsigned _day
)
185 :BrokenDate(_year
, _month
, _day
), BrokenTime(0, 0) {}
188 bool operator==(const BrokenDateTime other
) const {
189 return (const BrokenDate
&)*this == (const BrokenDate
&)other
&&
190 (const BrokenTime
&)*this == (const BrokenTime
&)other
;
194 * Does this object contain plausible values?
197 bool Plausible() const {
198 return BrokenDate::Plausible() && BrokenTime::Plausible();
203 * Convert a UNIX UTC time stamp (seconds since epoch) to a
204 * BrokenDateTime object.
207 static BrokenDateTime
FromUnixTimeUTC(int64_t t
);
211 int64_t ToUnixTimeUTC() const;
214 * Returns the current system date and time, in UTC.
216 static const BrokenDateTime
NowUTC();
219 * Returns the current system date and time, in the current time zone.
221 static const BrokenDateTime
NowLocal();
224 * Returns a BrokenDateTime that has the specified number of seconds
227 * @param seconds the number of seconds to add; may be negative
230 BrokenDateTime
operator+(int seconds
) const;
232 BrokenDateTime
operator-(int seconds
) const {
233 return *this + (-seconds
);
237 * Returns the number of seconds between the two BrokenDateTime structs.
238 * The second one is subtracted from the first one.
240 * <now> - <old> = positive timespan since <old> in seconds
242 int operator-(const BrokenDateTime
&other
) const;
245 static_assert(is_trivial
<BrokenDateTime
>::value
, "type is not trivial");