Search code examples
poco-libraries

Poco::LocalDateTime::timestamp() not converting timestamps to UTC


According to the header file of Poco::Timestamp, timestamps are in UTC, see Timestamp documentation. If timestamps are in UTC, shouldn't a method converting a Poco::LocalDateTime to Poco::Timestamp make sure that the returned timestamp is in UTC? Currently, Poco::LocalDateTime::timestamp() does not do this, and the returned timestamp is in local time.

It's especially strange since the assignment operator Poco::LocalDateTime::operator = (const Timestamp& timestamp) does a UTC to local time conversion. The following code asserts because of this:

Poco::LocalDateTime local1 = Poco::LocalDateTime( 2020, 1, 30 );
Poco::Timestamp timestamp = local1.timestamp();
Poco::LocalDateTime local2 = timestamp;
assert( local1 == local2 );

local1 will not have the same value as local2 in this example. Am I the only one who think this is strange behavior?


Solution

  • If you look at LocalDateTime::timestamp() you will see that it converts the timestamp before returning via Timestamp::fromUtcTime so that function returns a Timestamp in Local time, not UTC time.

    You can use the Timestamp::utcTime() function or the Timestamp::raw() function but those return different types to prevent you from accidentally doing the wrong thing.

    What are you actually trying to achieve here?