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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/DateTime.h>
#include <chrono>
namespace Aws
{
namespace Crt
{
DateTime::DateTime() noexcept : m_good(true)
{
std::chrono::system_clock::time_point time;
aws_date_time_init_epoch_millis(
&m_date_time,
static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(time.time_since_epoch()).count()));
}
DateTime::DateTime(const std::chrono::system_clock::time_point &timepointToAssign) noexcept : m_good(true)
{
aws_date_time_init_epoch_millis(
&m_date_time,
static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(timepointToAssign.time_since_epoch())
.count()));
}
DateTime::DateTime(uint64_t millisSinceEpoch) noexcept : m_good(true)
{
aws_date_time_init_epoch_millis(&m_date_time, millisSinceEpoch);
}
DateTime::DateTime(double epoch_millis) noexcept : m_good(true)
{
aws_date_time_init_epoch_secs(&m_date_time, epoch_millis);
}
DateTime::DateTime(const char *timestamp, DateFormat format) noexcept
{
ByteBuf timeStampBuf = ByteBufFromCString(timestamp);
m_good =
(aws_date_time_init_from_str(&m_date_time, &timeStampBuf, static_cast<aws_date_format>(format)) ==
AWS_ERROR_SUCCESS);
}
bool DateTime::operator==(const DateTime &other) const noexcept
{
return aws_date_time_diff(&m_date_time, &other.m_date_time) == 0;
}
bool DateTime::operator<(const DateTime &other) const noexcept
{
return aws_date_time_diff(&m_date_time, &other.m_date_time) < 0;
}
bool DateTime::operator>(const DateTime &other) const noexcept
{
return aws_date_time_diff(&m_date_time, &other.m_date_time) > 0;
}
bool DateTime::operator!=(const DateTime &other) const noexcept { return !(*this == other); }
bool DateTime::operator<=(const DateTime &other) const noexcept
{
return aws_date_time_diff(&m_date_time, &other.m_date_time) <= 0;
}
bool DateTime::operator>=(const DateTime &other) const noexcept
{
return aws_date_time_diff(&m_date_time, &other.m_date_time) >= 0;
}
DateTime DateTime::operator+(const std::chrono::milliseconds &a) const noexcept
{
auto currentTime = aws_date_time_as_millis(&m_date_time);
currentTime += a.count();
return {currentTime};
}
DateTime DateTime::operator-(const std::chrono::milliseconds &a) const noexcept
{
auto currentTime = aws_date_time_as_millis(&m_date_time);
currentTime -= a.count();
return {currentTime};
}
DateTime &DateTime::operator=(double secondsSinceEpoch) noexcept
{
aws_date_time_init_epoch_secs(&m_date_time, secondsSinceEpoch);
m_good = true;
return *this;
}
DateTime &DateTime::operator=(uint64_t millisSinceEpoch) noexcept
{
aws_date_time_init_epoch_millis(&m_date_time, millisSinceEpoch);
m_good = true;
return *this;
}
DateTime &DateTime::operator=(const std::chrono::system_clock::time_point &timepointToAssign) noexcept
{
aws_date_time_init_epoch_millis(
&m_date_time,
static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::milliseconds>(timepointToAssign.time_since_epoch())
.count()));
m_good = true;
return *this;
}
DateTime &DateTime::operator=(const char *timestamp) noexcept
{
ByteBuf timeStampBuf = aws_byte_buf_from_c_str(timestamp);
m_good = aws_date_time_init_from_str(
&m_date_time, &timeStampBuf, static_cast<aws_date_format>(DateFormat::AutoDetect)) ==
AWS_ERROR_SUCCESS;
return *this;
}
DateTime::operator bool() const noexcept { return m_good; }
int DateTime::GetLastError() const noexcept { return aws_last_error(); }
bool DateTime::ToLocalTimeString(DateFormat format, ByteBuf &outputBuf) const noexcept
{
return (
aws_date_time_to_local_time_str(&m_date_time, static_cast<aws_date_format>(format), &outputBuf) ==
AWS_ERROR_SUCCESS);
}
bool DateTime::ToGmtString(DateFormat format, ByteBuf &outputBuf) const noexcept
{
return (
aws_date_time_to_utc_time_str(&m_date_time, static_cast<aws_date_format>(format), &outputBuf) ==
AWS_ERROR_SUCCESS);
}
double DateTime::SecondsWithMSPrecision() const noexcept { return aws_date_time_as_epoch_secs(&m_date_time); }
uint64_t DateTime::Millis() const noexcept { return aws_date_time_as_millis(&m_date_time); }
std::chrono::system_clock::time_point DateTime::UnderlyingTimestamp() const noexcept
{
return std::chrono::system_clock::from_time_t(m_date_time.timestamp);
}
uint16_t DateTime::GetYear(bool localTime) const noexcept
{
return aws_date_time_year(&m_date_time, localTime);
}
Month DateTime::GetMonth(bool localTime) const noexcept
{
return static_cast<Month>(aws_date_time_month(&m_date_time, localTime));
}
uint8_t DateTime::GetDay(bool localTime) const noexcept
{
return aws_date_time_month_day(&m_date_time, localTime);
}
DayOfWeek DateTime::GetDayOfWeek(bool localTime) const noexcept
{
return static_cast<DayOfWeek>(aws_date_time_day_of_week(&m_date_time, localTime));
}
uint8_t DateTime::GetHour(bool localTime) const noexcept { return aws_date_time_hour(&m_date_time, localTime); }
uint8_t DateTime::GetMinute(bool localTime) const noexcept
{
return aws_date_time_minute(&m_date_time, localTime);
}
uint8_t DateTime::GetSecond(bool localTime) const noexcept
{
return aws_date_time_second(&m_date_time, localTime);
}
bool DateTime::IsDST(bool localTime) const noexcept { return aws_date_time_dst(&m_date_time, localTime); }
DateTime DateTime::Now() noexcept
{
DateTime dateTime;
aws_date_time_init_now(&dateTime.m_date_time);
return dateTime;
}
std::chrono::milliseconds DateTime::operator-(const DateTime &other) const noexcept
{
auto diff = aws_date_time_diff(&m_date_time, &other.m_date_time);
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::seconds(diff));
}
} // namespace Crt
} // namespace Aws
|