chrono.h revision 12391:ceeca8b41e4b
112855Sgabeblack@google.com/* 212855Sgabeblack@google.com pybind11/chrono.h: Transparent conversion between std::chrono and python's datetime 312855Sgabeblack@google.com 412855Sgabeblack@google.com Copyright (c) 2016 Trent Houliston <trent@houliston.me> and 512855Sgabeblack@google.com Wenzel Jakob <wenzel.jakob@epfl.ch> 612855Sgabeblack@google.com 712855Sgabeblack@google.com All rights reserved. Use of this source code is governed by a 812855Sgabeblack@google.com BSD-style license that can be found in the LICENSE file. 912855Sgabeblack@google.com*/ 1012855Sgabeblack@google.com 1112855Sgabeblack@google.com#pragma once 1212855Sgabeblack@google.com 1312855Sgabeblack@google.com#include "pybind11.h" 1412855Sgabeblack@google.com#include <cmath> 1512855Sgabeblack@google.com#include <ctime> 1612855Sgabeblack@google.com#include <chrono> 1712855Sgabeblack@google.com#include <datetime.h> 1812855Sgabeblack@google.com 1912855Sgabeblack@google.com// Backport the PyDateTime_DELTA functions from Python3.3 if required 2012855Sgabeblack@google.com#ifndef PyDateTime_DELTA_GET_DAYS 2112855Sgabeblack@google.com#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days) 2212855Sgabeblack@google.com#endif 2312855Sgabeblack@google.com#ifndef PyDateTime_DELTA_GET_SECONDS 2412855Sgabeblack@google.com#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds) 2512855Sgabeblack@google.com#endif 2612855Sgabeblack@google.com#ifndef PyDateTime_DELTA_GET_MICROSECONDS 2712855Sgabeblack@google.com#define PyDateTime_DELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta*)o)->microseconds) 2812855Sgabeblack@google.com#endif 2912855Sgabeblack@google.com 3012855Sgabeblack@google.comNAMESPACE_BEGIN(PYBIND11_NAMESPACE) 3112855Sgabeblack@google.comNAMESPACE_BEGIN(detail) 3212855Sgabeblack@google.com 3312855Sgabeblack@google.comtemplate <typename type> class duration_caster { 3412855Sgabeblack@google.compublic: 3512855Sgabeblack@google.com typedef typename type::rep rep; 3612855Sgabeblack@google.com typedef typename type::period period; 3712855Sgabeblack@google.com 3812855Sgabeblack@google.com typedef std::chrono::duration<uint_fast32_t, std::ratio<86400>> days; 3912855Sgabeblack@google.com 4012855Sgabeblack@google.com bool load(handle src, bool) { 4112855Sgabeblack@google.com using namespace std::chrono; 4212855Sgabeblack@google.com 4312855Sgabeblack@google.com // Lazy initialise the PyDateTime import 4412855Sgabeblack@google.com if (!PyDateTimeAPI) { PyDateTime_IMPORT; } 4512855Sgabeblack@google.com 4612855Sgabeblack@google.com if (!src) return false; 4712855Sgabeblack@google.com // If invoked with datetime.delta object 4812855Sgabeblack@google.com if (PyDelta_Check(src.ptr())) { 4912855Sgabeblack@google.com value = type(duration_cast<duration<rep, period>>( 5012855Sgabeblack@google.com days(PyDateTime_DELTA_GET_DAYS(src.ptr())) 5112855Sgabeblack@google.com + seconds(PyDateTime_DELTA_GET_SECONDS(src.ptr())) 5212855Sgabeblack@google.com + microseconds(PyDateTime_DELTA_GET_MICROSECONDS(src.ptr())))); 5313158Sgabeblack@google.com return true; 5412855Sgabeblack@google.com } 5512855Sgabeblack@google.com // If invoked with a float we assume it is seconds and convert 5612855Sgabeblack@google.com else if (PyFloat_Check(src.ptr())) { 5712855Sgabeblack@google.com value = type(duration_cast<duration<rep, period>>(duration<double>(PyFloat_AsDouble(src.ptr())))); 58 return true; 59 } 60 else return false; 61 } 62 63 // If this is a duration just return it back 64 static const std::chrono::duration<rep, period>& get_duration(const std::chrono::duration<rep, period> &src) { 65 return src; 66 } 67 68 // If this is a time_point get the time_since_epoch 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(); 71 } 72 73 static handle cast(const type &src, return_value_policy /* policy */, handle /* parent */) { 74 using namespace std::chrono; 75 76 // Use overloaded function to get our duration from our source 77 // Works out if it is a duration or time_point and get the duration 78 auto d = get_duration(src); 79 80 // Lazy initialise the PyDateTime import 81 if (!PyDateTimeAPI) { PyDateTime_IMPORT; } 82 83 // Declare these special duration types so the conversions happen with the correct primitive types (int) 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>; 87 88 auto dd = duration_cast<dd_t>(d); 89 auto subd = d - dd; 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()); 93 } 94 95 PYBIND11_TYPE_CASTER(type, _("datetime.timedelta")); 96}; 97 98// This is for casting times on the system clock into datetime.datetime instances 99template <typename Duration> class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>> { 100public: 101 typedef std::chrono::time_point<std::chrono::system_clock, Duration> type; 102 bool load(handle src, bool) { 103 using namespace std::chrono; 104 105 // Lazy initialise the PyDateTime import 106 if (!PyDateTimeAPI) { PyDateTime_IMPORT; } 107 108 if (!src) return false; 109 if (PyDateTime_Check(src.ptr())) { 110 std::tm cal; 111 cal.tm_sec = PyDateTime_DATE_GET_SECOND(src.ptr()); 112 cal.tm_min = PyDateTime_DATE_GET_MINUTE(src.ptr()); 113 cal.tm_hour = PyDateTime_DATE_GET_HOUR(src.ptr()); 114 cal.tm_mday = PyDateTime_GET_DAY(src.ptr()); 115 cal.tm_mon = PyDateTime_GET_MONTH(src.ptr()) - 1; 116 cal.tm_year = PyDateTime_GET_YEAR(src.ptr()) - 1900; 117 cal.tm_isdst = -1; 118 119 value = system_clock::from_time_t(std::mktime(&cal)) + microseconds(PyDateTime_DATE_GET_MICROSECOND(src.ptr())); 120 return true; 121 } 122 else return false; 123 } 124 125 static handle cast(const std::chrono::time_point<std::chrono::system_clock, Duration> &src, return_value_policy /* policy */, handle /* parent */) { 126 using namespace std::chrono; 127 128 // Lazy initialise the PyDateTime import 129 if (!PyDateTimeAPI) { PyDateTime_IMPORT; } 130 131 std::time_t tt = system_clock::to_time_t(src); 132 // this function uses static memory so it's best to copy it out asap just in case 133 // otherwise other code that is using localtime may break this (not just python code) 134 std::tm localtime = *std::localtime(&tt); 135 136 // Declare these special duration types so the conversions happen with the correct primitive types (int) 137 using us_t = duration<int, std::micro>; 138 139 return PyDateTime_FromDateAndTime(localtime.tm_year + 1900, 140 localtime.tm_mon + 1, 141 localtime.tm_mday, 142 localtime.tm_hour, 143 localtime.tm_min, 144 localtime.tm_sec, 145 (duration_cast<us_t>(src.time_since_epoch() % seconds(1))).count()); 146 } 147 PYBIND11_TYPE_CASTER(type, _("datetime.datetime")); 148}; 149 150// Other clocks that are not the system clock are not measured as datetime.datetime objects 151// since they are not measured on calendar time. So instead we just make them timedeltas 152// Or if they have passed us a time as a float we convert that 153template <typename Clock, typename Duration> class type_caster<std::chrono::time_point<Clock, Duration>> 154: public duration_caster<std::chrono::time_point<Clock, Duration>> { 155}; 156 157template <typename Rep, typename Period> class type_caster<std::chrono::duration<Rep, Period>> 158: public duration_caster<std::chrono::duration<Rep, Period>> { 159}; 160 161NAMESPACE_END(detail) 162NAMESPACE_END(PYBIND11_NAMESPACE) 163