24 template <
typename type>
27 using rep =
typename type::rep;
28 using period =
typename type::period;
31 using days = std::chrono::duration<int_least32_t, std::ratio<86400>>;
34 using namespace std::chrono;
45 if (PyDelta_Check(src.
ptr())) {
46 value =
type(duration_cast<duration<rep, period>>(
47 days(PyDateTime_DELTA_GET_DAYS(src.
ptr()))
48 + seconds(PyDateTime_DELTA_GET_SECONDS(src.
ptr()))
49 + microseconds(PyDateTime_DELTA_GET_MICROSECONDS(src.
ptr()))));
53 if (PyFloat_Check(src.
ptr())) {
54 value =
type(duration_cast<duration<rep, period>>(
55 duration<double>(PyFloat_AsDouble(src.
ptr()))));
62 static const std::chrono::duration<rep, period> &
68 template <
typename Clock>
69 static std::chrono::duration<rep, period>
70 get_duration(
const std::chrono::time_point<Clock, std::chrono::duration<rep, period>> &src) {
71 return src.time_since_epoch();
75 using namespace std::chrono;
88 using dd_t = duration<int, std::ratio<86400>>;
89 using ss_t = duration<int, std::ratio<1>>;
90 using us_t = duration<int, std::micro>;
92 auto dd = duration_cast<dd_t>(
d);
94 auto ss = duration_cast<ss_t>(subd);
95 auto us = duration_cast<us_t>(subd -
ss);
96 return PyDelta_FromDSU(dd.count(),
ss.count(), us.count());
103 #if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || defined(_MSC_VER)
104 if (localtime_s(buf,
time))
108 static std::mutex mtx;
109 std::lock_guard<std::mutex> lock(mtx);
110 std::tm *tm_ptr = std::localtime(
time);
111 if (tm_ptr !=
nullptr) {
119 template <
typename Duration>
120 class type_caster<
std::chrono::time_point<std::chrono::system_clock, Duration>> {
122 using type = std::chrono::time_point<std::chrono::system_clock, Duration>;
124 using namespace std::chrono;
127 if (!PyDateTimeAPI) {
138 if (PyDateTime_Check(src.
ptr())) {
139 cal.tm_sec = PyDateTime_DATE_GET_SECOND(src.
ptr());
140 cal.tm_min = PyDateTime_DATE_GET_MINUTE(src.
ptr());
141 cal.tm_hour = PyDateTime_DATE_GET_HOUR(src.
ptr());
142 cal.tm_mday = PyDateTime_GET_DAY(src.
ptr());
143 cal.tm_mon = PyDateTime_GET_MONTH(src.
ptr()) - 1;
144 cal.tm_year = PyDateTime_GET_YEAR(src.
ptr()) - 1900;
146 msecs = microseconds(PyDateTime_DATE_GET_MICROSECOND(src.
ptr()));
147 }
else if (PyDate_Check(src.
ptr())) {
151 cal.tm_mday = PyDateTime_GET_DAY(src.
ptr());
152 cal.tm_mon = PyDateTime_GET_MONTH(src.
ptr()) - 1;
153 cal.tm_year = PyDateTime_GET_YEAR(src.
ptr()) - 1900;
155 msecs = microseconds(0);
156 }
else if (PyTime_Check(src.
ptr())) {
157 cal.tm_sec = PyDateTime_TIME_GET_SECOND(src.
ptr());
158 cal.tm_min = PyDateTime_TIME_GET_MINUTE(src.
ptr());
159 cal.tm_hour = PyDateTime_TIME_GET_HOUR(src.
ptr());
164 msecs = microseconds(PyDateTime_TIME_GET_MICROSECOND(src.
ptr()));
169 value = time_point_cast<Duration>(system_clock::from_time_t(std::mktime(&
cal)) + msecs);
173 static handle cast(
const std::chrono::time_point<std::chrono::system_clock, Duration> &src,
176 using namespace std::chrono;
179 if (!PyDateTimeAPI) {
185 using us_t = duration<int, std::micro>;
186 auto us = duration_cast<us_t>(src.time_since_epoch() % seconds(1));
187 if (us.count() < 0) {
195 = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us));
199 if (!localtime_ptr) {
200 throw cast_error(
"Unable to represent system_clock in local time");
202 return PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
203 localtime.tm_mon + 1,
216 template <
typename Clock,
typename Duration>
220 template <
typename Rep,
typename Period>