1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_DATETIME_HPP
20 #define REALM_DATETIME_HPP
30 OldDateTime() noexcept
35 /// Construct from the number of seconds since Jan 1 00:00:00 UTC
37 /// FIXME: See if we can make this private again. Required by query_expression.hpp
38 OldDateTime(int_fast64_t d) noexcept
43 /// Return the time as seconds since Jan 1 00:00:00 UTC 1970.
44 int_fast64_t get_olddatetime() const noexcept
49 friend bool operator==(const OldDateTime&, const OldDateTime&) noexcept;
50 friend bool operator!=(const OldDateTime&, const OldDateTime&) noexcept;
51 friend bool operator<(const OldDateTime&, const OldDateTime&) noexcept;
52 friend bool operator<=(const OldDateTime&, const OldDateTime&) noexcept;
53 friend bool operator>(const OldDateTime&, const OldDateTime&) noexcept;
54 friend bool operator>=(const OldDateTime&, const OldDateTime&) noexcept;
56 /// Construct from broken down local time.
58 /// \note This constructor uses std::mktime() to convert the
59 /// specified local time to seconds since the Epoch, that is, the
60 /// result depends on the current globally specified time zone
63 /// \param year The year (the minimum valid value is 1970).
65 /// \param month The month in the range [1, 12].
67 /// \param day The day of the month in the range [1, 31].
69 /// \param hours Hours since midnight in the range [0, 23].
71 /// \param minutes Minutes after the hour in the range [0, 59].
73 /// \param seconds Seconds after the minute in the range [0,
74 /// 60]. Note that the range allows for leap seconds.
75 OldDateTime(int year, int month, int day, int hours = 0, int minutes = 0, int seconds = 0);
77 template <class Ch, class Tr>
78 friend std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& out, const OldDateTime&);
80 // This is used by query_expression.hpp to generalize its templates and simplify the code *alot*; it is needed
81 // because OldDateTime is internally stored in an int64_t column.
82 operator int_fast64_t() noexcept;
85 int_fast64_t m_time; // Seconds since Jan 1 00:00:00 UTC 1970.
86 static std::time_t assemble(int year, int month, int day, int hours, int minutes, int seconds);
94 inline bool operator==(const OldDateTime& a, const OldDateTime& b) noexcept
96 return a.m_time == b.m_time;
99 inline bool operator!=(const OldDateTime& a, const OldDateTime& b) noexcept
101 return a.m_time != b.m_time;
104 inline bool operator<(const OldDateTime& a, const OldDateTime& b) noexcept
106 return a.m_time < b.m_time;
109 inline bool operator<=(const OldDateTime& a, const OldDateTime& b) noexcept
111 return a.m_time <= b.m_time;
114 inline bool operator>(const OldDateTime& a, const OldDateTime& b) noexcept
116 return a.m_time > b.m_time;
119 inline bool operator>=(const OldDateTime& a, const OldDateTime& b) noexcept
121 return a.m_time >= b.m_time;
124 inline OldDateTime::operator int_fast64_t() noexcept
129 inline OldDateTime::OldDateTime(int year, int month, int day, int hours, int minutes, int seconds)
130 : m_time(assemble(year, month, day, hours, minutes, seconds))
134 template <class Ch, class Tr>
135 inline std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& out, const OldDateTime& d)
137 out << "OldDateTime(" << d.m_time << ")";
141 inline std::time_t OldDateTime::assemble(int year, int month, int day, int hours, int minutes, int seconds)
144 local_time.tm_year = year - 1900;
145 local_time.tm_mon = month - 1;
146 local_time.tm_mday = day;
147 local_time.tm_hour = hours;
148 local_time.tm_min = minutes;
149 local_time.tm_sec = seconds;
150 local_time.tm_isdst = -1;
151 return std::mktime(&local_time);
157 #endif // REALM_DATETIME_HPP