2016-08-25 13:08:04 +00:00
|
|
|
/*
|
|
|
|
pybind11/chrono.h: Transparent conversion between std::chrono and python's datetime
|
|
|
|
|
|
|
|
Copyright (c) 2016 Trent Houliston <trent@houliston.me> and
|
|
|
|
Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "pybind11.h"
|
2016-09-14 09:27:53 +00:00
|
|
|
#include <cmath>
|
2016-09-27 15:01:44 +00:00
|
|
|
#include <ctime>
|
2016-08-25 13:08:04 +00:00
|
|
|
#include <chrono>
|
2016-08-26 05:40:13 +00:00
|
|
|
#include <datetime.h>
|
2016-08-25 13:08:04 +00:00
|
|
|
|
2016-09-27 15:01:44 +00:00
|
|
|
// Backport the PyDateTime_DELTA functions from Python3.3 if required
|
|
|
|
#ifndef PyDateTime_DELTA_GET_DAYS
|
|
|
|
#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days)
|
|
|
|
#endif
|
|
|
|
#ifndef PyDateTime_DELTA_GET_SECONDS
|
|
|
|
#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds)
|
|
|
|
#endif
|
|
|
|
#ifndef PyDateTime_DELTA_GET_MICROSECONDS
|
|
|
|
#define PyDateTime_DELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta*)o)->microseconds)
|
|
|
|
#endif
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
2016-08-25 13:08:04 +00:00
|
|
|
|
2016-09-27 15:01:44 +00:00
|
|
|
template <typename type> class duration_caster {
|
2016-08-25 13:08:04 +00:00
|
|
|
public:
|
2020-11-09 18:10:19 +00:00
|
|
|
using rep = typename type::rep;
|
2020-09-11 02:57:10 +00:00
|
|
|
using period = typename type::period;
|
2016-09-27 15:01:44 +00:00
|
|
|
|
2020-09-11 02:57:10 +00:00
|
|
|
using days = std::chrono::duration<uint_fast32_t, std::ratio<86400>>;
|
2016-08-25 13:08:04 +00:00
|
|
|
|
|
|
|
bool load(handle src, bool) {
|
|
|
|
using namespace std::chrono;
|
2016-09-13 10:40:28 +00:00
|
|
|
|
|
|
|
// Lazy initialise the PyDateTime import
|
2016-09-14 09:27:53 +00:00
|
|
|
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
|
2016-08-25 13:08:04 +00:00
|
|
|
|
|
|
|
if (!src) return false;
|
2016-09-27 15:01:44 +00:00
|
|
|
// If invoked with datetime.delta object
|
2016-08-25 13:08:04 +00:00
|
|
|
if (PyDelta_Check(src.ptr())) {
|
2016-09-27 15:01:44 +00:00
|
|
|
value = type(duration_cast<duration<rep, period>>(
|
|
|
|
days(PyDateTime_DELTA_GET_DAYS(src.ptr()))
|
|
|
|
+ seconds(PyDateTime_DELTA_GET_SECONDS(src.ptr()))
|
|
|
|
+ microseconds(PyDateTime_DELTA_GET_MICROSECONDS(src.ptr()))));
|
2016-08-25 13:08:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-27 15:01:44 +00:00
|
|
|
// If invoked with a float we assume it is seconds and convert
|
2016-09-13 10:40:28 +00:00
|
|
|
else if (PyFloat_Check(src.ptr())) {
|
2016-09-27 15:01:44 +00:00
|
|
|
value = type(duration_cast<duration<rep, period>>(duration<double>(PyFloat_AsDouble(src.ptr()))));
|
2016-09-13 10:40:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-08-25 13:08:04 +00:00
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2016-09-27 15:01:44 +00:00
|
|
|
// If this is a duration just return it back
|
|
|
|
static const std::chrono::duration<rep, period>& get_duration(const std::chrono::duration<rep, period> &src) {
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a time_point get the time_since_epoch
|
|
|
|
template <typename Clock> static std::chrono::duration<rep, period> get_duration(const std::chrono::time_point<Clock, std::chrono::duration<rep, period>> &src) {
|
|
|
|
return src.time_since_epoch();
|
|
|
|
}
|
|
|
|
|
|
|
|
static handle cast(const type &src, return_value_policy /* policy */, handle /* parent */) {
|
2016-08-25 13:08:04 +00:00
|
|
|
using namespace std::chrono;
|
2016-09-27 15:01:44 +00:00
|
|
|
|
|
|
|
// Use overloaded function to get our duration from our source
|
|
|
|
// Works out if it is a duration or time_point and get the duration
|
|
|
|
auto d = get_duration(src);
|
|
|
|
|
|
|
|
// Lazy initialise the PyDateTime import
|
2016-09-14 09:27:53 +00:00
|
|
|
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
|
2016-08-25 13:08:04 +00:00
|
|
|
|
|
|
|
// Declare these special duration types so the conversions happen with the correct primitive types (int)
|
2016-09-13 10:40:28 +00:00
|
|
|
using dd_t = duration<int, std::ratio<86400>>;
|
|
|
|
using ss_t = duration<int, std::ratio<1>>;
|
|
|
|
using us_t = duration<int, std::micro>;
|
2016-08-25 13:08:04 +00:00
|
|
|
|
2017-03-12 02:29:25 +00:00
|
|
|
auto dd = duration_cast<dd_t>(d);
|
|
|
|
auto subd = d - dd;
|
|
|
|
auto ss = duration_cast<ss_t>(subd);
|
|
|
|
auto us = duration_cast<us_t>(subd - ss);
|
|
|
|
return PyDelta_FromDSU(dd.count(), ss.count(), us.count());
|
2016-08-25 13:08:04 +00:00
|
|
|
}
|
2016-09-27 15:01:44 +00:00
|
|
|
|
2016-08-25 13:08:04 +00:00
|
|
|
PYBIND11_TYPE_CASTER(type, _("datetime.timedelta"));
|
|
|
|
};
|
|
|
|
|
2016-09-13 10:40:28 +00:00
|
|
|
// This is for casting times on the system clock into datetime.datetime instances
|
2016-08-25 13:08:04 +00:00
|
|
|
template <typename Duration> class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>> {
|
|
|
|
public:
|
2020-09-11 02:57:10 +00:00
|
|
|
using type = std::chrono::time_point<std::chrono::system_clock, Duration>;
|
2016-08-25 13:08:04 +00:00
|
|
|
bool load(handle src, bool) {
|
|
|
|
using namespace std::chrono;
|
2016-09-13 10:40:28 +00:00
|
|
|
|
|
|
|
// Lazy initialise the PyDateTime import
|
2016-09-14 09:27:53 +00:00
|
|
|
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
|
2016-08-25 13:08:04 +00:00
|
|
|
|
|
|
|
if (!src) return false;
|
2019-07-19 09:28:48 +00:00
|
|
|
|
|
|
|
std::tm cal;
|
|
|
|
microseconds msecs;
|
|
|
|
|
2016-08-25 13:08:04 +00:00
|
|
|
if (PyDateTime_Check(src.ptr())) {
|
|
|
|
cal.tm_sec = PyDateTime_DATE_GET_SECOND(src.ptr());
|
|
|
|
cal.tm_min = PyDateTime_DATE_GET_MINUTE(src.ptr());
|
|
|
|
cal.tm_hour = PyDateTime_DATE_GET_HOUR(src.ptr());
|
|
|
|
cal.tm_mday = PyDateTime_GET_DAY(src.ptr());
|
|
|
|
cal.tm_mon = PyDateTime_GET_MONTH(src.ptr()) - 1;
|
|
|
|
cal.tm_year = PyDateTime_GET_YEAR(src.ptr()) - 1900;
|
|
|
|
cal.tm_isdst = -1;
|
2019-07-19 09:28:48 +00:00
|
|
|
msecs = microseconds(PyDateTime_DATE_GET_MICROSECOND(src.ptr()));
|
|
|
|
} else if (PyDate_Check(src.ptr())) {
|
|
|
|
cal.tm_sec = 0;
|
|
|
|
cal.tm_min = 0;
|
|
|
|
cal.tm_hour = 0;
|
|
|
|
cal.tm_mday = PyDateTime_GET_DAY(src.ptr());
|
|
|
|
cal.tm_mon = PyDateTime_GET_MONTH(src.ptr()) - 1;
|
|
|
|
cal.tm_year = PyDateTime_GET_YEAR(src.ptr()) - 1900;
|
|
|
|
cal.tm_isdst = -1;
|
|
|
|
msecs = microseconds(0);
|
|
|
|
} else if (PyTime_Check(src.ptr())) {
|
|
|
|
cal.tm_sec = PyDateTime_TIME_GET_SECOND(src.ptr());
|
|
|
|
cal.tm_min = PyDateTime_TIME_GET_MINUTE(src.ptr());
|
|
|
|
cal.tm_hour = PyDateTime_TIME_GET_HOUR(src.ptr());
|
|
|
|
cal.tm_mday = 1; // This date (day, month, year) = (1, 0, 70)
|
|
|
|
cal.tm_mon = 0; // represents 1-Jan-1970, which is the first
|
|
|
|
cal.tm_year = 70; // earliest available date for Python's datetime
|
|
|
|
cal.tm_isdst = -1;
|
|
|
|
msecs = microseconds(PyDateTime_TIME_GET_MICROSECOND(src.ptr()));
|
2016-08-25 13:08:04 +00:00
|
|
|
}
|
|
|
|
else return false;
|
2019-07-19 09:28:48 +00:00
|
|
|
|
2020-09-13 14:24:00 +00:00
|
|
|
value = time_point_cast<Duration>(system_clock::from_time_t(std::mktime(&cal)) + msecs);
|
2019-07-19 09:28:48 +00:00
|
|
|
return true;
|
2016-08-25 13:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static handle cast(const std::chrono::time_point<std::chrono::system_clock, Duration> &src, return_value_policy /* policy */, handle /* parent */) {
|
|
|
|
using namespace std::chrono;
|
2016-09-13 10:40:28 +00:00
|
|
|
|
|
|
|
// Lazy initialise the PyDateTime import
|
2016-09-14 09:27:53 +00:00
|
|
|
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
|
2016-08-25 13:08:04 +00:00
|
|
|
|
2020-08-28 13:21:43 +00:00
|
|
|
// Get out microseconds, and make sure they are positive, to avoid bug in eastern hemisphere time zones
|
|
|
|
// (cfr. https://github.com/pybind/pybind11/issues/2417)
|
|
|
|
using us_t = duration<int, std::micro>;
|
|
|
|
auto us = duration_cast<us_t>(src.time_since_epoch() % seconds(1));
|
|
|
|
if (us.count() < 0)
|
|
|
|
us += seconds(1);
|
|
|
|
|
|
|
|
// Subtract microseconds BEFORE `system_clock::to_time_t`, because:
|
|
|
|
// > If std::time_t has lower precision, it is implementation-defined whether the value is rounded or truncated.
|
|
|
|
// (https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t)
|
|
|
|
std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us));
|
2016-08-25 13:08:04 +00:00
|
|
|
// this function uses static memory so it's best to copy it out asap just in case
|
2016-09-27 15:01:44 +00:00
|
|
|
// otherwise other code that is using localtime may break this (not just python code)
|
|
|
|
std::tm localtime = *std::localtime(&tt);
|
2016-08-25 13:08:04 +00:00
|
|
|
|
2016-09-27 15:01:44 +00:00
|
|
|
return PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
|
|
|
|
localtime.tm_mon + 1,
|
|
|
|
localtime.tm_mday,
|
|
|
|
localtime.tm_hour,
|
|
|
|
localtime.tm_min,
|
|
|
|
localtime.tm_sec,
|
2020-08-28 13:21:43 +00:00
|
|
|
us.count());
|
2016-08-25 13:08:04 +00:00
|
|
|
}
|
|
|
|
PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
|
|
|
|
};
|
|
|
|
|
2016-09-13 10:40:28 +00:00
|
|
|
// Other clocks that are not the system clock are not measured as datetime.datetime objects
|
|
|
|
// since they are not measured on calendar time. So instead we just make them timedeltas
|
|
|
|
// Or if they have passed us a time as a float we convert that
|
2016-09-27 15:01:44 +00:00
|
|
|
template <typename Clock, typename Duration> class type_caster<std::chrono::time_point<Clock, Duration>>
|
|
|
|
: public duration_caster<std::chrono::time_point<Clock, Duration>> {
|
|
|
|
};
|
2016-09-13 10:40:28 +00:00
|
|
|
|
2016-09-27 15:01:44 +00:00
|
|
|
template <typename Rep, typename Period> class type_caster<std::chrono::duration<Rep, Period>>
|
|
|
|
: public duration_caster<std::chrono::duration<Rep, Period>> {
|
2016-08-25 13:08:04 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|