PostgreSQL
 SQL >> Datenbank >  >> RDS >> PostgreSQL

PostgreSQL-Timestamp in einen std::chrono-Wert umwandeln

Der C++20-Standard führt eine Familie von chrono::time_points namens local_time ein:

// [time.clock.local], local time
struct local_t { };

template<class Duration>
using local_time = time_point<local_t, Duration>;

using local_seconds = local_time<seconds>;
using local_days    = local_time<days>;

Diese time_points repräsentieren einen „Zeitstempel ohne Zeitzone“ – genau das, was PostgreSQL ohne with time zone liefert.

Eine freie, quelloffene Vorschau dieser C++20-Bibliothek gibt es hier:

https://github.com/HowardHinnant/date

Sie wird bereits weltweit in anderen Projekten eingesetzt und weicht in Details vom C++20-Standard ab, etwa indem alles in namespace date statt namespace std::chrono liegt.

Beispielprogramm:

#include "date/date.h"
#include <iostream>

int main() {
    using namespace date;
    using namespace std::chrono;

    int y = 2019;
    int m = 8;
    int d = 28;
    int H = 14;
    int M = 42;
    int S = 16;
    int US = 500'000;

    local_time<microseconds> lt =
        local_days{year{y}/m/d} + hours{H} + minutes{M}
        + seconds{S} + microseconds{US};

    std::cout << lt << '\n';
}

Ausgabe:

2019-08-28 14:42:16.500000

Fehlt in der Datenbank with time zone, kombinieren Sie stattdessen sys_time mit einer expliziten Zeitzone über zoned_time.