最近我要连接到 PostgreSQL,我需要在我的对象中存储日期/时间以传递给插入和更新某些表的查询。 但在 C++ 中没有明确的方法来存储和检索日期/时间。 有什么意见吗?
最佳答案
PostgreSQL TimeFormat 9+ 版本 https://www.postgresql.org/docs/9.1/datatype-datetime.html
8byte int(64 位)
时间格式,微秒
精度,UTC 不带时区(从表顶部开始)。
当您创建一个表时,您可以通过 PostgreSQL current_timestamp
为记录添加时间戳,或者以整数 64 位微秒格式
插入表中。因为 PostgreSQL 有多种时间格式,你应该从表中决定你想要的任何格式的时间
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`
类似于此答案的内容:Getting an accurate execution time in C++ (micro seconds)
然后将您的对象/记录推送到 PostGRESQL,当以微秒为单位检索时,调整精度 /1000 为毫秒
等。
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.
关于c++ - 在类/对象中存储日期和时间的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53687870/