c++ - 在类/对象中存储日期和时间的最佳做法是什么?

标签 c++ postgresql date datetime time

最近我要连接到 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/

相关文章:

c++ - 如何用范围内的随机值填充数组? (重复是可以的。)

sql - 如何从函数中选择多个值到单独的列中?

php - MySQL 加入速度慢。任何加速的方法

c++ - 如何在开始时停止程序以将GDB附加到该程序?

c++ - 方括号 [] 运算符重载 c++

PostgreSQL:删除除最近日期以外的所有日期

postgresql - Kafka Connect JDBC Sink quote.sql.identifiers 不工作

date - Golang Time.Sub 返回小时、分钟、秒 - 如何以天或月为单位获取?

postgresql - 在postgres中获取月份的第一个日期

c++ - 是否有不是以 C/C++ 风格编写的 OpenCL 绑定(bind)?