recently i'm going to connect to PostgreSQL and I need to store date/time in my object to pass to the query for insert and update some table. but there is not clear way in c++ to store and retrieve date/time. any comment?
-
2You can covert it to milliseconds from epoch. You might want to look at `chrono` namespace – Fureeish Dec 08 '18 at 23:23
-
With what precision was the data stored in the database? if it's +-1 second, well, then a `time_t`willl do. – Ted Lyngmo Dec 09 '18 at 01:16
1 Answers
PostgreSQL TimeFormat 9+ version https://www.postgresql.org/docs/9.1/datatype-datetime.html
Which is a 8byte int(64 bit)
time format in microsecond
precision, UTC without timezone (from top of the table).
When you create a table you can either time-stamp the record by PostgreSQL current_timestamp
, OR insert into table as integer 64bit microsecond format
. since PostgreSQL has multiple time format you should decide time any the format you want from table
PostgreSQL approach CREATE,INSERT,RETRIEVE
"CREATE TABLE example_table(update_time_column TIMESTAMP DEFAULT CURRENT_TIMESTAMP)"
"INSERT INTO example_table(update_time_column) VALUES update_time_column=CURRENT_TIMESTAMP"
"SELECT (EXTRACT(epoch FROM update_time_column)*1000000) FROM example_table"
C++ approach
auto/int64_t cppTime = get64bitMicrosecondFormat from some library`
something similar to this answer: Getting an accurate execution time in C++ (micro seconds)
Then push your object / record to PostGRESQL, when retrieve in microseconds, adjust precision /1000 for milliseconds
etc.
Just don't forget to synchronize PostgreSQL and C++ timestamp length (eg. 8byte - 8byte each side) otherwise, your time will be thresholded either side, and you will lose precision / get unexpected time.

- 86
- 1
- 3