added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / olddatetime.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 Realm Inc.
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  **************************************************************************/
18
19 #ifndef REALM_DATETIME_HPP
20 #define REALM_DATETIME_HPP
21
22 #include <ctime>
23 #include <ostream>
24
25 namespace realm {
26
27
28 class OldDateTime {
29 public:
30     OldDateTime() noexcept
31         : m_time(0)
32     {
33     }
34
35     /// Construct from the number of seconds since Jan 1 00:00:00 UTC
36     /// 1970.
37     /// FIXME: See if we can make this private again. Required by query_expression.hpp
38     OldDateTime(int_fast64_t d) noexcept
39         : m_time(d)
40     {
41     }
42
43     /// Return the time as seconds since Jan 1 00:00:00 UTC 1970.
44     int_fast64_t get_olddatetime() const noexcept
45     {
46         return m_time;
47     }
48
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;
55
56     /// Construct from broken down local time.
57     ///
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
61     /// setting.
62     ///
63     /// \param year The year (the minimum valid value is 1970).
64     ///
65     /// \param month The month in the range [1, 12].
66     ///
67     /// \param day The day of the month in the range [1, 31].
68     ///
69     /// \param hours Hours since midnight in the range [0, 23].
70     ///
71     /// \param minutes Minutes after the hour in the range [0, 59].
72     ///
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);
76
77     template <class Ch, class Tr>
78     friend std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& out, const OldDateTime&);
79
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;
83
84 private:
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);
87     template <typename T>
88     friend class Value;
89 };
90
91
92 // Implementation:
93
94 inline bool operator==(const OldDateTime& a, const OldDateTime& b) noexcept
95 {
96     return a.m_time == b.m_time;
97 }
98
99 inline bool operator!=(const OldDateTime& a, const OldDateTime& b) noexcept
100 {
101     return a.m_time != b.m_time;
102 }
103
104 inline bool operator<(const OldDateTime& a, const OldDateTime& b) noexcept
105 {
106     return a.m_time < b.m_time;
107 }
108
109 inline bool operator<=(const OldDateTime& a, const OldDateTime& b) noexcept
110 {
111     return a.m_time <= b.m_time;
112 }
113
114 inline bool operator>(const OldDateTime& a, const OldDateTime& b) noexcept
115 {
116     return a.m_time > b.m_time;
117 }
118
119 inline bool operator>=(const OldDateTime& a, const OldDateTime& b) noexcept
120 {
121     return a.m_time >= b.m_time;
122 }
123
124 inline OldDateTime::operator int_fast64_t() noexcept
125 {
126     return m_time;
127 }
128
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))
131 {
132 }
133
134 template <class Ch, class Tr>
135 inline std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& out, const OldDateTime& d)
136 {
137     out << "OldDateTime(" << d.m_time << ")";
138     return out;
139 }
140
141 inline std::time_t OldDateTime::assemble(int year, int month, int day, int hours, int minutes, int seconds)
142 {
143     std::tm local_time;
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);
152 }
153
154
155 } // namespace realm
156
157 #endif // REALM_DATETIME_HPP