mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Added ability to convert from datetime.date to system_clock::time_point (#1848)
* Added ability to convert from Python datetime.date and datetime.time to C++ system_clock::time_point
This commit is contained in:
@@ -40,6 +40,62 @@ def test_chrono_system_clock_roundtrip():
|
||||
assert diff.microseconds == 0
|
||||
|
||||
|
||||
def test_chrono_system_clock_roundtrip_date():
|
||||
date1 = datetime.date.today()
|
||||
|
||||
# Roundtrip the time
|
||||
datetime2 = m.test_chrono2(date1)
|
||||
date2 = datetime2.date()
|
||||
time2 = datetime2.time()
|
||||
|
||||
# The returned value should be a datetime
|
||||
assert isinstance(datetime2, datetime.datetime)
|
||||
assert isinstance(date2, datetime.date)
|
||||
assert isinstance(time2, datetime.time)
|
||||
|
||||
# They should be identical (no information lost on roundtrip)
|
||||
diff = abs(date1 - date2)
|
||||
assert diff.days == 0
|
||||
assert diff.seconds == 0
|
||||
assert diff.microseconds == 0
|
||||
|
||||
# Year, Month & Day should be the same after the round trip
|
||||
assert date1.year == date2.year
|
||||
assert date1.month == date2.month
|
||||
assert date1.day == date2.day
|
||||
|
||||
# There should be no time information
|
||||
assert time2.hour == 0
|
||||
assert time2.minute == 0
|
||||
assert time2.second == 0
|
||||
assert time2.microsecond == 0
|
||||
|
||||
|
||||
def test_chrono_system_clock_roundtrip_time():
|
||||
time1 = datetime.datetime.today().time()
|
||||
|
||||
# Roundtrip the time
|
||||
datetime2 = m.test_chrono2(time1)
|
||||
date2 = datetime2.date()
|
||||
time2 = datetime2.time()
|
||||
|
||||
# The returned value should be a datetime
|
||||
assert isinstance(datetime2, datetime.datetime)
|
||||
assert isinstance(date2, datetime.date)
|
||||
assert isinstance(time2, datetime.time)
|
||||
|
||||
# Hour, Minute, Second & Microsecond should be the same after the round trip
|
||||
assert time1.hour == time2.hour
|
||||
assert time1.minute == time2.minute
|
||||
assert time1.second == time2.second
|
||||
assert time1.microsecond == time2.microsecond
|
||||
|
||||
# There should be no date information (i.e. date = python base date)
|
||||
assert date2.year == 1970
|
||||
assert date2.month == 1
|
||||
assert date2.day == 1
|
||||
|
||||
|
||||
def test_chrono_duration_roundtrip():
|
||||
|
||||
# Get the difference between two times (a timedelta)
|
||||
@@ -70,6 +126,19 @@ def test_chrono_duration_subtraction_equivalence():
|
||||
assert cpp_diff.microseconds == diff.microseconds
|
||||
|
||||
|
||||
def test_chrono_duration_subtraction_equivalence_date():
|
||||
|
||||
date1 = datetime.date.today()
|
||||
date2 = datetime.date.today()
|
||||
|
||||
diff = date2 - date1
|
||||
cpp_diff = m.test_chrono4(date2, date1)
|
||||
|
||||
assert cpp_diff.days == diff.days
|
||||
assert cpp_diff.seconds == diff.seconds
|
||||
assert cpp_diff.microseconds == diff.microseconds
|
||||
|
||||
|
||||
def test_chrono_steady_clock():
|
||||
time1 = m.test_chrono5()
|
||||
assert isinstance(time1, datetime.timedelta)
|
||||
|
||||
Reference in New Issue
Block a user