Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11_abseil
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11_abseil
Commits
0bb93688
Commit
0bb93688
authored
Nov 02, 2022
by
Xiaofei Wang
Committed by
Copybara-Service
Nov 02, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Internal change
PiperOrigin-RevId: 485653868
parent
6a97f0a6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
1 deletions
+32
-1
pybind11_abseil/absl_casters.h
+10
-1
pybind11_abseil/tests/absl_example.cc
+3
-0
pybind11_abseil/tests/absl_test.py
+19
-0
No files found.
pybind11_abseil/absl_casters.h
View file @
0bb93688
...
@@ -161,7 +161,16 @@ struct type_caster<absl::Time> {
...
@@ -161,7 +161,16 @@ struct type_caster<absl::Time> {
// Conversion part 1 (Python->C++)
// Conversion part 1 (Python->C++)
bool
load
(
handle
src
,
bool
convert
)
{
bool
load
(
handle
src
,
bool
convert
)
{
// Ensure that absl::Duration is converted from a Python datetime.date.
if
(
convert
)
{
if
(
PyLong_Check
(
src
.
ptr
()))
{
value
=
absl
::
FromUnixSeconds
(
src
.
cast
<
int64_t
>
());
return
true
;
}
else
if
(
PyFloat_Check
(
src
.
ptr
()))
{
value
=
absl
::
time_internal
::
FromUnixDuration
(
absl
::
Seconds
(
src
.
cast
<
double
>
()));
return
true
;
}
}
if
(
!
hasattr
(
src
,
"year"
)
||
!
hasattr
(
src
,
"month"
)
||
if
(
!
hasattr
(
src
,
"year"
)
||
!
hasattr
(
src
,
"month"
)
||
!
hasattr
(
src
,
"day"
))
{
!
hasattr
(
src
,
"day"
))
{
return
false
;
return
false
;
...
...
pybind11_abseil/tests/absl_example.cc
View file @
0bb93688
...
@@ -295,6 +295,9 @@ PYBIND11_MODULE(absl_example, m) {
...
@@ -295,6 +295,9 @@ PYBIND11_MODULE(absl_example, m) {
m
.
def
(
"check_duration"
,
&
CheckDuration
,
arg
(
"duration"
),
arg
(
"secs"
));
m
.
def
(
"check_duration"
,
&
CheckDuration
,
arg
(
"duration"
),
arg
(
"secs"
));
m
.
def
(
"make_datetime"
,
&
MakeTime
,
arg
(
"secs"
));
m
.
def
(
"make_datetime"
,
&
MakeTime
,
arg
(
"secs"
));
m
.
def
(
"check_datetime"
,
&
CheckDatetime
,
arg
(
"datetime"
),
arg
(
"secs"
));
m
.
def
(
"check_datetime"
,
&
CheckDatetime
,
arg
(
"datetime"
),
arg
(
"secs"
));
m
.
def
(
"absl_time_overloads"
,
[](
const
absl
::
Time
&
)
{
return
"absl::Time"
;
});
m
.
def
(
"absl_time_overloads"
,
[](
int
)
{
return
"int"
;
});
m
.
def
(
"absl_time_overloads"
,
[](
float
)
{
return
"float"
;
});
// absl::TimeZone bindings
// absl::TimeZone bindings
m
.
def
(
"roundtrip_timezone"
,
&
RoundtripTimeZone
,
arg
(
"timezone"
));
m
.
def
(
"roundtrip_timezone"
,
&
RoundtripTimeZone
,
arg
(
"timezone"
));
...
...
pybind11_abseil/tests/absl_test.py
View file @
0bb93688
...
@@ -125,6 +125,25 @@ class AbslTimeTest(parameterized.TestCase):
...
@@ -125,6 +125,25 @@ class AbslTimeTest(parameterized.TestCase):
secs
=
dt
.
timestamp
()
secs
=
dt
.
timestamp
()
self
.
assertTrue
(
absl_example
.
check_datetime
(
dt
,
secs
))
self
.
assertTrue
(
absl_example
.
check_datetime
(
dt
,
secs
))
def
test_absl_time_overloads
(
self
):
self
.
assertEqual
(
absl_example
.
absl_time_overloads
(
10
),
'int'
)
self
.
assertEqual
(
absl_example
.
absl_time_overloads
(
10.0
),
'float'
)
self
.
assertEqual
(
absl_example
.
absl_time_overloads
(
self
.
TEST_DATE
),
'absl::Time'
)
self
.
assertEqual
(
absl_example
.
absl_time_overloads
(
self
.
TEST_DATETIME
),
'absl::Time'
)
def
test_pass_int_as_absl_time
(
self
):
secs
=
int
(
datetime
.
datetime
(
self
.
TEST_DATE
.
year
,
self
.
TEST_DATE
.
month
,
self
.
TEST_DATE
.
day
)
.
timestamp
())
self
.
assertTrue
(
absl_example
.
check_datetime
(
secs
,
secs
))
def
test_pass_float_as_absl_time
(
self
):
secs
=
datetime
.
datetime
(
self
.
TEST_DATE
.
year
,
self
.
TEST_DATE
.
month
,
self
.
TEST_DATE
.
day
)
.
timestamp
()
self
.
assertIsInstance
(
secs
,
float
)
self
.
assertTrue
(
absl_example
.
check_datetime
(
secs
,
secs
))
def
test_return_civilsecond
(
self
):
def
test_return_civilsecond
(
self
):
# We need to use a timezone aware datetime here, otherwise
# We need to use a timezone aware datetime here, otherwise
# datetime.timestamp converts to localtime. UTC is chosen as the convention
# datetime.timestamp converts to localtime. UTC is chosen as the convention
...
...
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