仅将纪元字符串保存到 mysql 日期时间年/月/日,但没有小时分钟秒
Save epoch string to mysql datetime only year/month/date but no hour minute seconds
我有一些C++代码可以将纪元字符串(从time_t
对象转换(保存到MySQL数据库time
列(数据类型datetime
(。
C++ 中的纪元时间字符串是1559333386
的,但是,当保存到 MySQL 时,它只显示日期,小时、分钟和秒都显示为零:2019-05-31 00:00:00
。
问题是如何将包含完整信息的纪元时间戳以year-month-day hour:minute:second
格式保存到MySQL数据库?
谢谢。
这是代码:
std::time_t t = std::stof(time);
std::stringstream sstime;
sstime << std::put_time(std::gmtime(&t), "%F") << 'n';
Poco::Data::Statement sql_insert(*session);
sql_insert << "INSERT into history (uid, time)
VALUES (?,?)",
Poco::Data::Keywords::bind(uid),
Poco::Data::Keywords::bind(sstime.str());
以下是两种可能的解决方案:
一种是对纪元时间戳进行一些预处理,将其格式更改为 YYYY-MM-DD HH24:MI:SS
,然后将其绑定到 SQL 语句。
另一个在我看来有点奇怪和冒险,我在MySQL网站上的DATETIME文档的评论部分找到了它,方法如下:
SET @@timestamp := 1559333386;
INSERT INTO history (time) VALUES (NOW());
SET @@timestamp := DEFAULT;
有关此内容的更多信息,请参阅时间戳系统变量的MySQL文档。
以上只是一些建议,我没有在没有所需环境的情况下进行任何测试和验证,请在任何地方实施之前对其进行彻底测试。
相关文章:
- 将秒转换为小时、分钟、秒.百分之一秒
- 以C++为单位将毫秒转换为小时:分钟:秒:毫秒
- 仅将纪元字符串保存到 mysql 日期时间年/月/日,但没有小时分钟秒
- 将时间转换为小时、分钟和秒
- QTimeEdit 当分钟递增时如何递增到下一个小时
- 解析日期字符串以从时间戳中删除小时、分钟和秒
- 将几秒钟转换为几天,小时,分钟,秒格式(C )
- 从STD :: String Boost C 获取小时和几分钟
- 将42010958毫秒到小时的最干净的方法是什么:分钟:QT中的秒
- 将年龄在几年,小时,几分钟和秒的时间内转换
- C++从字符串中获取小时和分钟
- 使用boost::chrono,如何从单独的年、月、日、小时、分钟、秒计算自epoch UTC以来的毫秒
- C++ 秒分钟小时时为 0.
- 如何为此添加模量?(小时:分钟:秒)
- 要求分钟,小时,上午和下午的程序
- 将十进制时间转换为小时和分钟,以便以C++显示
- C++ 自纪元以来给定小时和分钟的秒数
- 使用chrono::time_point获取当前小时和分钟数
- 将秒转换为小时以及分钟和秒
- 小时,分钟,秒到Time_t