Skip to content

Commit

Permalink
Fix review remarks: remove useless comment and skip setting TZ enviro…
Browse files Browse the repository at this point in the history
…nment variable on Windows
  • Loading branch information
YannickJadoul committed Aug 27, 2020
1 parent 8a301ec commit 85e4632
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
4 changes: 1 addition & 3 deletions include/pybind11/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,9 @@ template <typename Duration> class type_caster<std::chrono::time_point<std::chro
// Lazy initialise the PyDateTime import
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }

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

// 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);
Expand Down
15 changes: 11 additions & 4 deletions tests/test_chrono.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_chrono_system_clock_roundtrip_date():
assert time2.microsecond == 0


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),
Expand All @@ -82,12 +87,14 @@ def test_chrono_system_clock_roundtrip_date():
datetime.time(5, 59, 59, 1),
])
@pytest.mark.parametrize("tz", [
"Europe/Brussels",
"Asia/Pyongyang",
"America/New_York",
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):
monkeypatch.setenv("TZ", "/usr/share/zoneinfo/{}".format(tz))
if tz is not None:
monkeypatch.setenv("TZ", "/usr/share/zoneinfo/{}".format(tz))

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

0 comments on commit 85e4632

Please sign in to comment.