C++ 正在尝试将postgres时间戳字符串转换为c++;时间戳
我有PostgreSQL时间戳字符串:C++ 正在尝试将postgres时间戳字符串转换为c++;时间戳,c++,C++,我有PostgreSQL时间戳字符串: 2020-07-06 09:30:00.646533 我尝试将其转换为timevalstruct,我尝试使用,但得到以下输出: 1970年1月1日星期四00:33:40 这是我的代码: #include <iostream> int main() { std::string ss("2020-07-06 09:30:00.646533"); auto t = atoll(ss.c_str()); tim
2020-07-06 09:30:00.646533
timeval
struct,我尝试使用,但得到以下输出:
1970年1月1日星期四00:33:40
#include <iostream>
int main()
{
std::string ss("2020-07-06 09:30:00.646533");
auto t = atoll(ss.c_str());
time_t time = atoi(ss.c_str());
std::cout << asctime(gmtime(&time));
return 0;
}
#包括
int main()
{
标准:字符串ss(“2020-07-06 09:30:00.646533”);
auto t=环礁(ss.c_str());
time_t time=atoi(ss.c_str());
std::coutatol
和atoi
只是从一个字符串中解析不同大小的整数。它们通常不是很好的函数,因为它们无法表示解析字符串失败。朋友是更好的函数。但是在这种情况下,我们没有数字,我们有一个日期字符串,所以std::stoi
也不会起作用(但它至少可以告诉您它不起作用)
提供了更好的日期支持,在此之前,Howard Hinnant提供了相同的功能:
#include "date.h"
#include <iostream>
#include <sstream>
#include <chrono>
int main()
{
std::stringstream ss("2020-07-06 09:30:00.646533");
// convert string to date time
std::chrono::system_clock::time_point time;
ss >> date::parse("%F %T", time);
if (!ss) {
std::cout << "invalid date\n";
return 1;
}
// get the amount of time since the epoch, assumes std::chrono::system_clock uses the same epoch as timeval
auto sinceEpoch = time.time_since_epoch();
// get the whole number of seconds
auto seconds = date::floor<std::chrono::seconds>(sinceEpoch);
// get the remaining microseconds
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(sinceEpoch - seconds);
std::cout << seconds.count() << ", " << microseconds.count() << "\n";
return 0;
}
#包括“date.h”
#包括
#包括
#包括
int main()
{
标准:stringstream ss(“2020-07-06 09:30:00.646533”);
//将字符串转换为日期时间
标准:时钟:系统时钟:时间点时间;
ss>>日期::解析(“%F%T”,时间);
如果(!ss){
std::cout您可以这样使用:
#include<ctime>
#include<iotream>
int main()
{
std::string ss = "2020-07-06 09:30:00.646533";
auto i = ss.find_first_of('.');
std::string line(ss.begin()+(i+1),ss.end());
std::tm tm = {};
tm.tm_isdst = -1; // <- to set not to use day lghite saveing.
strptime(ss.c_str(), "%F %H:%M:%S", &tm); //<-enter the data to tm
start.tv_sec = mktime(&tm); //<-convert tm to time_t
start.tv_usec = stoi(line); // <- set the usec from the stirng
//IF you want the other why around
strftime(tmbuf, sizeof tmbuf, " %F %H:%M:%S", localtime(&start.tv_sec));
snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, start.tv_usec);
std::cout << tmbuf;
return 0;
}
#包括
#包括
int main()
{
std::string ss=“2020-07-06 09:30:00.646533”;
自动i=ss.find_first_of('.');
字符串行(ss.begin()+(i+1),ss.end());
std::tm={};
tm.tm_isdst=-1;//为什么您认为以ASCII to long和ASCII to integer这两个短语命名的函数会将文本转换为时间?它们被清楚地记录为用于解析整数。不仅如此,您从不分配结果数字(或不分配结果数字)对于 TM,也不尝试显示……没有任何意义,对于“代码> TM <代码>的注释,它是从不同尝试中留下的,我是C++的新成员,在我附加了使用的AtOII64的答案中,所以我尝试了它,谢谢,但是我只能使用标准的LIB和C++ 14,为什么我尝试使用AN?我附加的是什么?日期库是一个单独的头文件,在c++14中工作,没有理由不使用它。我在我的答案顶部解释了为什么你的代码不工作。c++14中没有用小数秒解析日期的函数,这是最好的
#include<ctime>
#include<iotream>
int main()
{
std::string ss = "2020-07-06 09:30:00.646533";
auto i = ss.find_first_of('.');
std::string line(ss.begin()+(i+1),ss.end());
std::tm tm = {};
tm.tm_isdst = -1; // <- to set not to use day lghite saveing.
strptime(ss.c_str(), "%F %H:%M:%S", &tm); //<-enter the data to tm
start.tv_sec = mktime(&tm); //<-convert tm to time_t
start.tv_usec = stoi(line); // <- set the usec from the stirng
//IF you want the other why around
strftime(tmbuf, sizeof tmbuf, " %F %H:%M:%S", localtime(&start.tv_sec));
snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, start.tv_usec);
std::cout << tmbuf;
return 0;
}