datetime.hpp
4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*************************************************************************
*
* REALM CONFIDENTIAL
* __________________
*
* [2011] - [2015] Realm Inc
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Realm Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Realm Incorporated
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Realm Incorporated.
*
**************************************************************************/
#ifndef REALM_DATETIME_HPP
#define REALM_DATETIME_HPP
#include <ctime>
#include <ostream>
namespace realm {
class DateTime {
public:
DateTime() noexcept: m_time(0) {}
/// Construct from the number of seconds since Jan 1 00:00:00 UTC
/// 1970.
/// FIXME: See if we can make this private again. Required by query_expression.hpp
DateTime(int_fast64_t d) noexcept : m_time(d) {}
/// Return the time as seconds since Jan 1 00:00:00 UTC 1970.
int_fast64_t get_datetime() const noexcept { return m_time; }
friend bool operator==(const DateTime&, const DateTime&) noexcept;
friend bool operator!=(const DateTime&, const DateTime&) noexcept;
friend bool operator< (const DateTime&, const DateTime&) noexcept;
friend bool operator<= (const DateTime&, const DateTime&) noexcept;
friend bool operator> (const DateTime&, const DateTime&) noexcept;
friend bool operator>= (const DateTime&, const DateTime&) noexcept;
/// Construct from broken down local time.
///
/// \note This constructor uses std::mktime() to convert the
/// specified local time to seconds since the Epoch, that is, the
/// result depends on the current globally specified time zone
/// setting.
///
/// \param year The year (the minimum valid value is 1970).
///
/// \param month The month in the range [1, 12].
///
/// \param day The day of the month in the range [1, 31].
///
/// \param hours Hours since midnight in the range [0, 23].
///
/// \param minutes Minutes after the hour in the range [0, 59].
///
/// \param seconds Seconds after the minute in the range [0,
/// 60]. Note that the range allows for leap seconds.
DateTime(int year, int month, int day, int hours = 0, int minutes = 0, int seconds = 0);
template<class Ch, class Tr>
friend std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& out, const DateTime&);
// This is used by query_expression.hpp to generalize its templates and simplify the code *alot*; it is needed
// because DateTime is internally stored in an int64_t column.
operator int_fast64_t() noexcept;
private:
int_fast64_t m_time; // Seconds since Jan 1 00:00:00 UTC 1970.
static std::time_t assemble(int year, int month, int day, int hours, int minutes, int seconds);
template<typename T>
friend class Value;
};
// Implementation:
inline bool operator==(const DateTime& a, const DateTime& b) noexcept
{
return a.m_time == b.m_time;
}
inline bool operator!=(const DateTime& a, const DateTime& b) noexcept
{
return a.m_time != b.m_time;
}
inline bool operator<(const DateTime& a, const DateTime& b) noexcept
{
return a.m_time < b.m_time;
}
inline bool operator<=(const DateTime& a, const DateTime& b) noexcept
{
return a.m_time <= b.m_time;
}
inline bool operator>(const DateTime& a, const DateTime& b) noexcept
{
return a.m_time > b.m_time;
}
inline bool operator>=(const DateTime& a, const DateTime& b) noexcept
{
return a.m_time >= b.m_time;
}
inline DateTime::operator int_fast64_t() noexcept
{
return m_time;
}
inline DateTime::DateTime(int year, int month, int day, int hours, int minutes, int seconds):
m_time(assemble(year, month, day, hours, minutes, seconds)) {}
template<class Ch, class Tr>
inline std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& out, const DateTime& d)
{
out << "DateTime("<<d.m_time<<")";
return out;
}
inline std::time_t DateTime::assemble(int year, int month, int day, int hours, int minutes, int seconds)
{
std::tm local_time;
local_time.tm_year = year - 1900;
local_time.tm_mon = month - 1;
local_time.tm_mday = day;
local_time.tm_hour = hours;
local_time.tm_min = minutes;
local_time.tm_sec = seconds;
local_time.tm_isdst = -1;
return std::mktime(&local_time);
}
} // namespace realm
#endif // REALM_DATETIME_HPP