Skip to content

Commit 82865cd

Browse files
authored
Merge pull request #299 from lonvia/make-timestamp-thread-safe
Make Timstamp cast thread-safe
2 parents 24ea43a + 11ab606 commit 82865cd

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

lib/cast.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
*
33
* This file is part of pyosmium. (https://osmcode.org/pyosmium/)
44
*
5-
* Copyright (C) 2024 Sarah Hoffmann <[email protected]> and others.
5+
* Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
66
* For a full list of authors see the git log.
77
*/
88
#ifndef PYOSMIUM_CAST_H
99
#define PYOSMIUM_CAST_H
1010

1111
#include <datetime.h>
12-
#include <chrono>
1312

1413
#include <pybind11/pybind11.h>
1514
#include <osmium/osm.hpp>
@@ -44,25 +43,14 @@ namespace pybind11 { namespace detail {
4443

4544
static handle cast(type const &src, return_value_policy, handle)
4645
{
47-
using namespace std::chrono;
4846
// Lazy initialise the PyDateTime import
4947
if (!PyDateTimeAPI) { PyDateTime_IMPORT; }
5048

5149
std::time_t tt = src.seconds_since_epoch();
52-
std::tm localtime = *std::gmtime(&tt);
53-
handle pydate = PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
54-
localtime.tm_mon + 1,
55-
localtime.tm_mday,
56-
localtime.tm_hour,
57-
localtime.tm_min,
58-
localtime.tm_sec,
59-
0);
6050

6151
static auto utc = module::import("datetime").attr("timezone").attr("utc");
62-
using namespace literals;
63-
handle with_utc = pydate.attr("replace")("tzinfo"_a=utc).inc_ref();
64-
pydate.dec_ref();
65-
return with_utc;
52+
53+
return PyDateTime_FromTimestamp(pybind11::make_tuple(tt, utc).ptr());
6654
}
6755

6856
PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));

test/test_osm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,14 @@ def area(o):
304304
area=area)
305305

306306

307-
def test_changest_attributes(area_importer):
307+
def test_changeset_attributes(area_importer):
308308
def changeset(c):
309309
assert 34 == c.id
310310
assert 1 == c.uid
311311
assert not c.user_is_anonymous()
312312
assert "Steve" == c.user
313-
assert mkdate(2005, 4, 9, 19, 54, 13), c.created_at
314-
assert mkdate(2005, 4, 9, 20, 54, 39), c.closed_at
313+
assert mkdate(2005, 4, 9, 19, 54, 13) == c.created_at
314+
assert mkdate(2005, 4, 9, 20, 54, 39) == c.closed_at
315315
assert not c.open
316316
assert 2 == c.num_changes
317317
assert 0 == len(c.tags)

0 commit comments

Comments
 (0)