Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug roundtripping datetime.time objects after midnight in eastern hemisphere timezones (#2417) #2438

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions include/pybind11/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,28 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
// Lazy initialise the PyDateTime import
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src));
// Get out microseconds, and make sure they are positive, to avoid bug in eastern hemisphere time zones
// (cfr. https://github.com/pybind/pybind11/issues/2417)
using us_t = duration<int, std::micro>;
auto us = duration_cast<us_t>(src.time_since_epoch() % seconds(1));
if (us.count() < 0)
us += seconds(1);

// Subtract microseconds BEFORE `system_clock::to_time_t`, because:
// > If std::time_t has lower precision, it is implementation-defined whether the value is rounded or truncated.
// (https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t)
std::time_t tt = system_clock::to_time_t(time_point_cast<system_clock::duration>(src - us));
// this function uses static memory so it's best to copy it out asap just in case
// otherwise other code that is using localtime may break this (not just python code)
std::tm localtime = *std::localtime(&tt);

// Declare these special duration types so the conversions happen with the correct primitive types (int)
using us_t = duration<int, std::micro>;

return PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
localtime.tm_mon + 1,
localtime.tm_mday,
localtime.tm_hour,
localtime.tm_min,
localtime.tm_sec,
(duration_cast<us_t>(src.time_since_epoch() % seconds(1))).count());
us.count());
}
PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
};
Expand Down
1 change: 1 addition & 0 deletions tests/test_chrono.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "pybind11_tests.h"
#include <pybind11/chrono.h>
#include <chrono>

TEST_SUBMODULE(chrono, m) {
using system_time = std::chrono::system_clock::time_point;
Expand Down
29 changes: 27 additions & 2 deletions tests/test_chrono.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-
from pybind11_tests import chrono as m
import datetime
import pytest

import env # noqa: F401


def test_chrono_system_clock():
Expand Down Expand Up @@ -70,8 +73,30 @@ def test_chrono_system_clock_roundtrip_date():
assert time2.microsecond == 0


def test_chrono_system_clock_roundtrip_time():
time1 = datetime.datetime.today().time()
SKIP_TZ_ENV_ON_WIN = pytest.mark.skipif(
"env.WIN", reason="TZ environment variable only supported on POSIX"
)


@pytest.mark.parametrize("time1", [
datetime.datetime.today().time(),
datetime.time(0, 0, 0),
datetime.time(0, 0, 0, 1),
datetime.time(0, 28, 45, 109827),
datetime.time(0, 59, 59, 999999),
datetime.time(1, 0, 0),
datetime.time(5, 59, 59, 0),
datetime.time(5, 59, 59, 1),
])
@pytest.mark.parametrize("tz", [
None,
pytest.param("Europe/Brussels", marks=SKIP_TZ_ENV_ON_WIN),
pytest.param("Asia/Pyongyang", marks=SKIP_TZ_ENV_ON_WIN),
pytest.param("America/New_York", marks=SKIP_TZ_ENV_ON_WIN),
])
def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch):
if tz is not None:
monkeypatch.setenv("TZ", "/usr/share/zoneinfo/{}".format(tz))

# Roundtrip the time
datetime2 = m.test_chrono2(time1)
Expand Down