Skip to content

Commit 4cca280

Browse files
committed
Fix bug roundtripping datetime.time objects after midnight in eastern hemisphere timezones (pybind#2417)
1 parent 9b8cb02 commit 4cca280

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

include/pybind11/chrono.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,30 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
150150
// Lazy initialise the PyDateTime import
151151
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
152152

153-
std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src));
153+
// Declare these special duration types so the conversions happen with the correct primitive types (int)
154+
using us_t = duration<int, std::micro>;
155+
156+
// Get out microseconds, and make sure they are positive, to avoid bug in eastern hemisphere time zones
157+
// (cfr. https://github.com/pybind/pybind11/issues/2417)
158+
auto us = duration_cast<us_t>(src.time_since_epoch() % seconds(1));
159+
if (us.count() < 0)
160+
us += seconds(1);
161+
162+
// Subtract microseconds BEFORE `system_clock::to_time_t`, because:
163+
// > If std::time_t has lower precision, it is implementation-defined whether the value is rounded or truncated.
164+
// (https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t)
165+
std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us));
154166
// this function uses static memory so it's best to copy it out asap just in case
155167
// otherwise other code that is using localtime may break this (not just python code)
156168
std::tm localtime = *std::localtime(&tt);
157169

158-
// Declare these special duration types so the conversions happen with the correct primitive types (int)
159-
using us_t = duration<int, std::micro>;
160-
161170
return PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
162171
localtime.tm_mon + 1,
163172
localtime.tm_mday,
164173
localtime.tm_hour,
165174
localtime.tm_min,
166175
localtime.tm_sec,
167-
(duration_cast<us_t>(src.time_since_epoch() % seconds(1))).count());
176+
us.count());
168177
}
169178
PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
170179
};

tests/test_chrono.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "pybind11_tests.h"
1212
#include <pybind11/chrono.h>
13+
#include <chrono>
1314

1415
TEST_SUBMODULE(chrono, m) {
1516
using system_time = std::chrono::system_clock::time_point;

tests/test_chrono.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from pybind11_tests import chrono as m
33
import datetime
4+
import pytest
45

56

67
def test_chrono_system_clock():
@@ -70,9 +71,17 @@ def test_chrono_system_clock_roundtrip_date():
7071
assert time2.microsecond == 0
7172

7273

73-
def test_chrono_system_clock_roundtrip_time():
74-
time1 = datetime.datetime.today().time()
75-
74+
@pytest.mark.parametrize("time1", [
75+
datetime.datetime.today().time(),
76+
datetime.time(0, 0, 0),
77+
datetime.time(0, 0, 0, 1),
78+
datetime.time(0, 28, 45, 109827),
79+
datetime.time(0, 59, 59, 999999),
80+
datetime.time(1, 0, 0),
81+
datetime.time(5, 59, 59, 0),
82+
datetime.time(5, 59, 59, 1),
83+
])
84+
def test_chrono_system_clock_roundtrip_time(time1):
7685
# Roundtrip the time
7786
datetime2 = m.test_chrono2(time1)
7887
date2 = datetime2.date()

0 commit comments

Comments
 (0)