Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
6ddfd1e0
Commit
6ddfd1e0
authored
Aug 25, 2016
by
Trent Houliston
Browse files
Add in casts for c++11s chrono classes to pythons datetime
parent
29b5064e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
0 deletions
+110
-0
include/pybind11/cast.h
include/pybind11/cast.h
+109
-0
include/pybind11/common.h
include/pybind11/common.h
+1
-0
No files found.
include/pybind11/cast.h
View file @
6ddfd1e0
...
...
@@ -472,6 +472,115 @@ public:
PYBIND11_TYPE_CASTER
(
bool
,
_
(
"bool"
));
};
template
<
typename
Rep
,
typename
Period
>
class
type_caster
<
std
::
chrono
::
duration
<
Rep
,
Period
>>
{
public:
typedef
std
::
chrono
::
duration
<
Rep
,
Period
>
type
;
typedef
std
::
chrono
::
duration
<
std
::
chrono
::
hours
::
rep
,
std
::
ratio
<
86400
>>
days
;
bool
load
(
handle
src
,
bool
)
{
using
namespace
std
::
chrono
;
PyDateTime_IMPORT
;
if
(
!
src
)
return
false
;
if
(
PyDelta_Check
(
src
.
ptr
()))
{
const
PyDateTime_Delta
*
delta
=
reinterpret_cast
<
PyDateTime_Delta
*>
(
src
.
ptr
());
value
=
duration_cast
<
duration
<
Rep
,
Period
>>
(
days
(
delta
->
days
)
+
seconds
(
delta
->
seconds
)
+
microseconds
(
delta
->
microseconds
));
return
true
;
}
else
return
false
;
}
static
handle
cast
(
const
std
::
chrono
::
duration
<
Rep
,
Period
>
&
src
,
return_value_policy
/* policy */
,
handle
/* parent */
)
{
using
namespace
std
::
chrono
;
PyDateTime_IMPORT
;
int
dd
=
duration_cast
<
days
>
(
src
).
count
();
int
ss
=
duration_cast
<
seconds
>
(
src
%
days
(
1
)).
count
();
int
us
=
duration_cast
<
microseconds
>
(
src
%
seconds
(
1
)).
count
();
return
PyDelta_FromDSU
(
dd
,
ss
,
us
);
}
PYBIND11_TYPE_CASTER
(
type
,
_
(
"datetime.timedelta"
));
};
template
<
typename
Duration
>
class
type_caster
<
std
::
chrono
::
time_point
<
std
::
chrono
::
system_clock
,
Duration
>>
{
public:
typedef
std
::
chrono
::
time_point
<
std
::
chrono
::
system_clock
,
Duration
>
type
;
bool
load
(
handle
src
,
bool
)
{
using
namespace
std
::
chrono
;
PyDateTime_IMPORT
;
if
(
!
src
)
return
false
;
if
(
PyDateTime_Check
(
src
.
ptr
()))
{
std
::
tm
cal
;
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
());
cal
.
tm_isdst
=
-
1
;
value
=
system_clock
::
from_time_t
(
mktime
(
&
cal
));
return
true
;
}
else
return
false
;
}
static
handle
cast
(
const
std
::
chrono
::
time_point
<
std
::
chrono
::
system_clock
,
Duration
>
&
src
,
return_value_policy
/* policy */
,
handle
/* parent */
)
{
using
namespace
std
::
chrono
;
PyDateTime_IMPORT
;
time_t
tt
=
system_clock
::
to_time_t
(
src
);
tm
*
ltime
=
localtime
(
&
tt
);
// this function uses static memory so it's best
tm
localtime
=
*
ltime
;
// to copy it out asap just in case
return
PyDateTime_FromDateAndTime
(
localtime
.
tm_year
,
localtime
.
tm_mon
+
1
,
localtime
.
tm_mday
,
localtime
.
tm_hour
,
localtime
.
tm_min
,
localtime
.
tm_sec
,
(
duration_cast
<
microseconds
>
(
src
.
time_since_epoch
())
%
seconds
(
1
)).
count
());
}
PYBIND11_TYPE_CASTER
(
type
,
_
(
"datetime.datetime"
));
};
template
<
typename
Clock
,
typename
Duration
>
class
type_caster
<
std
::
chrono
::
time_point
<
Clock
,
Duration
>>
{
public:
typedef
std
::
chrono
::
time_point
<
Clock
,
Duration
>
type
;
bool
load
(
handle
src
,
bool
)
{
using
namespace
std
::
chrono
;
PyDateTime_IMPORT
;
if
(
!
src
)
return
false
;
if
(
PyTime_Check
(
src
.
ptr
()))
{
value
=
type
(
duration_cast
<
Duration
>
(
hours
(
PyDateTime_TIME_GET_HOUR
(
src
.
ptr
()))
+
minutes
(
PyDateTime_TIME_GET_MINUTE
(
src
.
ptr
()))
+
seconds
(
PyDateTime_TIME_GET_SECOND
(
src
.
ptr
()))
+
microseconds
(
PyDateTime_TIME_GET_MICROSECOND
(
src
.
ptr
()))
));
return
true
;
}
else
return
false
;
}
static
handle
cast
(
const
std
::
chrono
::
time_point
<
Clock
,
Duration
>
&
src
,
return_value_policy
/* policy */
,
handle
/* parent */
)
{
using
namespace
std
::
chrono
;
PyDateTime_IMPORT
;
Duration
d
=
src
.
time_since_epoch
();
return
PyTime_FromTime
(
duration_cast
<
hours
>
(
d
).
count
()
,
duration_cast
<
minutes
>
(
d
%
hours
(
1
)).
count
()
,
duration_cast
<
seconds
>
(
d
%
minutes
(
1
)).
count
()
,
duration_cast
<
microseconds
>
(
d
%
seconds
(
1
)).
count
());
}
PYBIND11_TYPE_CASTER
(
type
,
_
(
"datetime.time"
));
};
template
<
>
class
type_caster
<
std
::
string
>
{
public:
bool
load
(
handle
src
,
bool
)
{
...
...
include/pybind11/common.h
View file @
6ddfd1e0
...
...
@@ -46,6 +46,7 @@
#endif
#include <Python.h>
#include <datetime.h>
#include <frameobject.h>
#include <pythread.h>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment