po: update French translation
[xcsoar.git] / src / DateTime.hpp
blob8f5d366008b1b9fafc3a2f4ebc8f0e07a72d3137
1 /*
2 Copyright_License {
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"
28 #include "Compiler.h"
30 #include <stdint.h>
32 /**
33 * A broken-down representation of a date.
35 struct BrokenDate {
36 /**
37 * Absolute year, e.g. "2010".
39 uint16_t year;
41 /**
42 * Month number, 1-12.
44 uint8_t month;
46 /**
47 * Day of month, 1-31.
49 uint8_t day;
51 /**
52 * Day of the week (0-6, 0: sunday)
54 unsigned char day_of_week;
56 /**
57 * Non-initializing default constructor.
59 BrokenDate() = default;
61 gcc_constexpr_ctor
62 BrokenDate(unsigned _year, unsigned _month, unsigned _day)
63 :year(_year), month(_month), day(_day), day_of_week(-1) {}
65 gcc_constexpr_method
66 bool operator==(const BrokenDate other) const {
67 return year == other.year && month == other.month && day == other.day;
70 gcc_constexpr_method
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)));
77 /**
78 * Clears the object, to make the Plausible() check returns false.
80 void Clear() {
81 year = 0;
84 /**
85 * Does this object contain plausible values?
87 gcc_constexpr_method
88 bool Plausible() const {
89 return year >= 1800 && year <= 2500 &&
90 month >= 1 && month <= 12 &&
91 day >= 1 && day <= 31;
95 /**
96 * A broken-down representation of a time.
98 struct BrokenTime {
99 /**
100 * Hour of day, 0-23.
102 uint8_t hour;
105 * Minute, 0-59.
107 uint8_t minute;
110 * Second, 0-59.
112 uint8_t second;
115 * Non-initializing default constructor.
117 BrokenTime() = default;
119 gcc_constexpr_ctor
120 BrokenTime(unsigned _hour, unsigned _minute, unsigned _second=0)
121 :hour(_hour), minute(_minute), second(_second) {}
123 gcc_constexpr_method
124 bool operator==(const BrokenTime other) const {
125 return hour == other.hour && minute == other.minute &&
126 second == other.second;
129 gcc_constexpr_method
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?
139 gcc_constexpr_method
140 bool Plausible() const {
141 return hour < 24 && minute < 60 && second < 60;
145 * Returns the number of seconds which have passed on this day.
147 gcc_constexpr_method
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
158 gcc_const
159 static BrokenTime FromSecondOfDay(unsigned second_of_day);
162 * A wrapper for FromSecondOfDay() which allows values bigger than
163 * or equal to 3600*24.
165 gcc_const
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;
178 gcc_constexpr_ctor
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) {}
183 gcc_constexpr_ctor
184 BrokenDateTime(unsigned _year, unsigned _month, unsigned _day)
185 :BrokenDate(_year, _month, _day), BrokenTime(0, 0) {}
187 gcc_constexpr_method
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?
196 gcc_constexpr_method
197 bool Plausible() const {
198 return BrokenDate::Plausible() && BrokenTime::Plausible();
201 #ifdef HAVE_POSIX
203 * Convert a UNIX UTC time stamp (seconds since epoch) to a
204 * BrokenDateTime object.
206 gcc_const
207 static BrokenDateTime FromUnixTimeUTC(int64_t t);
208 #endif
210 gcc_pure
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
225 * added to it.
227 * @param seconds the number of seconds to add; may be negative
229 gcc_pure
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");
247 #endif