最近,我要连接到PostgreSQL,我需要将日期/时间存储在对象中,以传递给查询以进行插入和更新某些表。
但是在c++中没有明确的方法来存储和检索日期/时间。
任何意见?
最佳答案
PostgreSQL TimeFormat 9+版本https://www.postgresql.org/docs/9.1/datatype-datetime.html
这是8byte int(64 bit)
精度的microsecond
时间格式,不带时区的UTC(从表格顶部开始)。
创建表时,可以通过PostgreSQL current_timestamp
为记录添加时间戳,或者以整数64bit microsecond format
插入表中。由于PostgreSQL有多种时间格式,因此您应该从表中选择所需的任何时间格式
"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"
auto/int64_t cppTime = get64bitMicrosecondFormat from some library`
类似的答案:Getting an accurate execution time in C++ (micro seconds)
然后将您的对象/记录推送到PostGRESQL,以微秒为单位进行检索,调整精度
/1000 for milliseconds
等。