20 #ifndef PyDateTime_DELTA_GET_DAYS 21 #define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days) 23 #ifndef PyDateTime_DELTA_GET_SECONDS 24 #define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds) 26 #ifndef PyDateTime_DELTA_GET_MICROSECONDS 27 #define PyDateTime_DELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta*)o)->microseconds) 35 typedef typename type::rep
rep;
36 using period =
typename type::period;
38 using days = std::chrono::duration<uint_fast32_t, std::ratio<86400>>;
41 using namespace std::chrono;
44 if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
46 if (!src)
return false;
48 if (PyDelta_Check(src.
ptr())) {
49 value =
type(duration_cast<duration<rep, period>>(
56 else if (PyFloat_Check(src.
ptr())) {
57 value =
type(duration_cast<duration<rep, period>>(duration<double>(PyFloat_AsDouble(src.
ptr()))));
64 static const std::chrono::duration<rep, period>&
get_duration(
const std::chrono::duration<rep, period> &src) {
69 template <
typename Clock>
static std::chrono::duration<rep, period>
get_duration(
const std::chrono::time_point<Clock, std::chrono::duration<rep, period>> &src) {
70 return src.time_since_epoch();
74 using namespace std::chrono;
78 auto d = get_duration(src);
81 if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
84 using dd_t = duration<int, std::ratio<86400>>;
85 using ss_t = duration<int, std::ratio<1>>;
86 using us_t = duration<int, std::micro>;
88 auto dd = duration_cast<dd_t>(
d);
90 auto ss = duration_cast<ss_t>(subd);
91 auto us = duration_cast<us_t>(subd -
ss);
92 return PyDelta_FromDSU(dd.count(),
ss.count(), us.count());
99 template <
typename Duration>
class type_caster<
std::chrono::time_point<std::chrono::system_clock, Duration>> {
101 using type = std::chrono::time_point<std::chrono::system_clock, Duration>;
103 using namespace std::chrono;
106 if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
108 if (!src)
return false;
113 if (PyDateTime_Check(src.
ptr())) {
114 cal.tm_sec = PyDateTime_DATE_GET_SECOND(src.
ptr());
115 cal.tm_min = PyDateTime_DATE_GET_MINUTE(src.
ptr());
116 cal.tm_hour = PyDateTime_DATE_GET_HOUR(src.
ptr());
117 cal.tm_mday = PyDateTime_GET_DAY(src.
ptr());
118 cal.tm_mon = PyDateTime_GET_MONTH(src.
ptr()) - 1;
119 cal.tm_year = PyDateTime_GET_YEAR(src.
ptr()) - 1900;
121 msecs = microseconds(PyDateTime_DATE_GET_MICROSECOND(src.
ptr()));
122 }
else if (PyDate_Check(src.
ptr())) {
126 cal.tm_mday = PyDateTime_GET_DAY(src.
ptr());
127 cal.tm_mon = PyDateTime_GET_MONTH(src.
ptr()) - 1;
128 cal.tm_year = PyDateTime_GET_YEAR(src.
ptr()) - 1900;
130 msecs = microseconds(0);
131 }
else if (PyTime_Check(src.
ptr())) {
132 cal.tm_sec = PyDateTime_TIME_GET_SECOND(src.
ptr());
133 cal.tm_min = PyDateTime_TIME_GET_MINUTE(src.
ptr());
134 cal.tm_hour = PyDateTime_TIME_GET_HOUR(src.
ptr());
139 msecs = microseconds(PyDateTime_TIME_GET_MICROSECOND(src.
ptr()));
143 value = time_point_cast<Duration>(system_clock::from_time_t(std::mktime(&cal)) + msecs);
148 using namespace std::chrono;
151 if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
155 using us_t = duration<int, std::micro>;
156 auto us = duration_cast<us_t>(src.time_since_epoch() % seconds(1));
163 std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us));
166 std::tm localtime = *std::localtime(&tt);
168 return PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
169 localtime.tm_mon + 1,
182 template <
typename Clock,
typename Duration>
class type_caster<
std::chrono::time_point<Clock, Duration>>
186 template <
typename Rep,
typename Period>
class type_caster<std::chrono::duration<Rep, Period>>
PyObject * ptr() const
Return the underlying PyObject * pointer.
static handle cast(const type &src, return_value_policy, handle)
#define PyDateTime_DELTA_GET_SECONDS(o)
#define PYBIND11_NAMESPACE
#define PyDateTime_DELTA_GET_DAYS(o)
typename std::chrono::duration< Rep, Period >::period period
static std::stringstream ss
bool load(handle src, bool)
static const std::chrono::duration< rep, period > & get_duration(const std::chrono::duration< rep, period > &src)
#define PyDateTime_DELTA_GET_MICROSECONDS(o)
bool load(handle src, bool)
std::chrono::duration< uint_fast32_t, std::ratio< 86400 >> days
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
static handle cast(const std::chrono::time_point< std::chrono::system_clock, Duration > &src, return_value_policy, handle)
#define PYBIND11_TYPE_CASTER(type, py_name)
#define PYBIND11_NAMESPACE_END(name)
static std::chrono::duration< rep, period > get_duration(const std::chrono::time_point< Clock, std::chrono::duration< rep, period >> &src)
#define PYBIND11_NAMESPACE_BEGIN(name)