Library of Assembled Shared Sources
export_traits_chrono.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2022-2025 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#pragma once
44
45#include "python_common.h"
46#include "export_traits.h"
47#include <chrono>
48
49namespace lass
50{
51namespace python
52{
53
54namespace impl
55{
56 LASS_PYTHON_DLL PyObject* buildTimedelta(int days, int secs, int usecs);
57 LASS_PYTHON_DLL int getTimedelta(PyObject* obj, int &days, int &secs, int &usecs);
58}
59
60/** std::chrono::duration is mapped on datetime.timedelta by copy.
61 *
62 * @ingroup PyExportTraits
63 */
64template <typename Rep, typename Period>
65struct PyExportTraits<std::chrono::duration<Rep, Period>>
66{
67 constexpr static const char* py_typing = "datetime.timedelta";
68
69 using TDuration = std::chrono::duration<Rep, Period>;
70
71 static PyObject* build(TDuration v)
72 {
73 using namespace std::chrono;
74 using TDays = duration<int, std::ratio<86400>>;
75 using TSeconds = duration<int>;
76 using TMicroSeconds = duration<num::Tint64, std::micro>;
77
78 TMicroSeconds usecs = duration_cast<TMicroSeconds>(v);
79 const TDays days = duration_cast<TDays>(usecs);
80 usecs -= days;
81 const TSeconds secs = duration_cast<TSeconds>(usecs);
82 usecs -= secs;
83
84 return impl::buildTimedelta(days.count(), secs.count(), static_cast<int>(usecs.count()));
85 }
86 static int get(PyObject* obj, TDuration& v)
87 {
88 using namespace std::chrono;
89 using TDays = duration<int, std::ratio<86400>>;
90
91 int days, secs, usecs;
92 if (impl::getTimedelta(obj, days, secs, usecs) != 0)
93 {
94 return 1;
95 }
96 v = duration_cast<TDuration>(TDays(days)) +
97 duration_cast<TDuration>(seconds(secs)) +
98 duration_cast<TDuration>(microseconds(usecs));
99 return 0;
100 }
101};
102
103
104/** std::chrono::time_point<std::chrono::system_clock> is mapped on a
105 * timezone-unaware datetime.datetime instance by copy, in local time.
106 *
107 * datetime.datetime instances with a timezone will correctly be
108 * converted when interpreted as a time_point.
109 *
110 * @ingroup PyExportTraits
111 */
112template <>
113struct PyExportTraits<std::chrono::time_point<std::chrono::system_clock>>
114{
115 constexpr static const char* py_typing = "datetime.datetime";
116 constexpr static const char* py_typing_preamble = "import datetime";
117
118 using TClock = std::chrono::system_clock;
119 using TDuration = TClock::duration;
120 using TTimePoint = std::chrono::time_point<TClock>;
121 LASS_PYTHON_DLL static PyObject* build(const TTimePoint& v);
122 LASS_PYTHON_DLL static int get(PyObject* obj, TTimePoint& v);
123};
124
125
126#ifdef LASS_HAVE_STD_CHRONO_CPP20
127
128
129/** std::chrono::utc_clock::time_point is mapped on a timezone-aware
130 * datetime.datetime instance by copy, in UTC.
131 *
132 * datetime.datetime instances with other timezones will correctly be converted
133 * when interpreted as a time_point.
134 *
135 * Naive datetime.datetime instances without timezone will be interpreted as
136 * local time.
137 *
138 * @ingroup PyExportTraits
139 */
140template <>
141struct PyExportTraits<std::chrono::utc_clock::time_point>
142{
143 constexpr static const char* py_typing = "datetime.datetime";
144 constexpr static const char* py_typing_preamble = "import datetime";
145
146 LASS_PYTHON_DLL static PyObject* build(const std::chrono::utc_clock::time_point& v);
147 LASS_PYTHON_DLL static int get(PyObject* obj, std::chrono::utc_clock::time_point& v);
148};
149
150
151
152/** std::chrono::gps_clock::time_point is mapped on a timezone-aware
153 * datetime.datetime instance by copy, in UTC.
154 *
155 * datetime.datetime instances with other timezones will correctly be converted
156 * when interpreted as a time_point.
157 *
158 * Naive datetime.datetime instances without timezone will be interpreted as
159 * local time.
160 *
161 * @ingroup PyExportTraits
162 */
163template <>
164struct PyExportTraits<std::chrono::gps_clock::time_point>
165{
166 constexpr static const char* py_typing = "datetime.datetime";
167 constexpr static const char* py_typing_preamble = "import datetime";
168
169 LASS_PYTHON_DLL static PyObject* build(const std::chrono::gps_clock::time_point& v);
170 LASS_PYTHON_DLL static int get(PyObject* obj, std::chrono::gps_clock::time_point& v);
171};
172
173
174
175/** std::chrono::tai_clock::time_point is mapped on a timezone-aware
176 * datetime.datetime instance by copy, in UTC.
177 *
178 * datetime.datetime instances with other timezones will correctly be converted
179 * when interpreted as a time_point.
180 *
181 * Naive datetime.datetime instances without timezone will be interpreted as
182 * local time.
183 *
184 * @ingroup PyExportTraits
185 */
186template <>
187struct PyExportTraits<std::chrono::tai_clock::time_point>
188{
189 constexpr static const char* py_typing = "datetime.datetime";
190 constexpr static const char* py_typing_preamble = "import datetime";
191
192 LASS_PYTHON_DLL static PyObject* build(const std::chrono::tai_clock::time_point& v);
193 LASS_PYTHON_DLL static int get(PyObject* obj, std::chrono::tai_clock::time_point& v);
194};
195
196
197
198/** std::chrono::file_clock::time_point is mapped on a timezone-aware
199 * datetime.datetime instance by copy, in UTC.
200 *
201 * datetime.datetime instances with other timezones will correctly be converted
202 * when interpreted as a time_point.
203 *
204 * Naive datetime.datetime instances without timezone will be interpreted as
205 * local time.
206 *
207 * @ingroup PyExportTraits
208 */
209template <>
210struct PyExportTraits<std::chrono::file_clock::time_point>
211{
212 constexpr static const char* py_typing = "datetime.datetime";
213 constexpr static const char* py_typing_preamble = "import datetime";
214
215 LASS_PYTHON_DLL static PyObject* build(const std::chrono::file_clock::time_point& v);
216 LASS_PYTHON_DLL static int get(PyObject* obj, std::chrono::file_clock::time_point& v);
217};
218
219
220
221/** std::chrono::year_month_day is mapped on a datetime.date instance by copy.
222 *
223 * @ingroup PyExportTraits
224 */
225template <>
226struct PyExportTraits<std::chrono::year_month_day>
227{
228 constexpr static const char* py_typing = "datetime.date";
229 constexpr static const char* py_typing_preamble = "import datetime";
230
231 LASS_PYTHON_DLL static PyObject* build(const std::chrono::year_month_day& v);
232 LASS_PYTHON_DLL static int get(PyObject* obj, std::chrono::year_month_day& v);
233};
234
235
236#endif
237
238
239}
240}
241
242// EOF
Comprehensive C++ to Python binding library.
Library for Assembled Shared Sources.
Definition config.h:53
by copy, general case assumes shadow type or PyObjectPlus based type.