Skip to content
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
18 changes: 3 additions & 15 deletions lib/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
*
* This file is part of pyosmium. (https://osmcode.org/pyosmium/)
*
* Copyright (C) 2024 Sarah Hoffmann <lonvia@denofr.de> and others.
* Copyright (C) 2025 Sarah Hoffmann <lonvia@denofr.de> and others.
* For a full list of authors see the git log.
*/
#ifndef PYOSMIUM_CAST_H
#define PYOSMIUM_CAST_H

#include <datetime.h>
#include <chrono>

#include <pybind11/pybind11.h>
#include <osmium/osm.hpp>
Expand Down Expand Up @@ -44,25 +43,14 @@ namespace pybind11 { namespace detail {

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

std::time_t tt = src.seconds_since_epoch();
std::tm localtime = *std::gmtime(&tt);
handle pydate = PyDateTime_FromDateAndTime(localtime.tm_year + 1900,
localtime.tm_mon + 1,
localtime.tm_mday,
localtime.tm_hour,
localtime.tm_min,
localtime.tm_sec,
0);

static auto utc = module::import("datetime").attr("timezone").attr("utc");
using namespace literals;
handle with_utc = pydate.attr("replace")("tzinfo"_a=utc).inc_ref();
pydate.dec_ref();
return with_utc;

return PyDateTime_FromTimestamp(pybind11::make_tuple(tt, utc).ptr());
}

PYBIND11_TYPE_CASTER(type, _("datetime.datetime"));
Expand Down
6 changes: 3 additions & 3 deletions test/test_osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,14 @@ def area(o):
area=area)


def test_changest_attributes(area_importer):
def test_changeset_attributes(area_importer):
def changeset(c):
assert 34 == c.id
assert 1 == c.uid
assert not c.user_is_anonymous()
assert "Steve" == c.user
assert mkdate(2005, 4, 9, 19, 54, 13), c.created_at
assert mkdate(2005, 4, 9, 20, 54, 39), c.closed_at
assert mkdate(2005, 4, 9, 19, 54, 13) == c.created_at
assert mkdate(2005, 4, 9, 20, 54, 39) == c.closed_at
assert not c.open
assert 2 == c.num_changes
assert 0 == len(c.tags)
Expand Down