概述
时间格式:"2022-07-11_19:03:56.458"
C++98
#include <iostream>
#include <chrono>
#include <ctime>
int main() {
std::string date_time_str = "2022-07-11_19:03:56.458";
int year, month, day, hour, minute, second;
sscanf(date_time_str.c_str(), "%d-%d-%d_%d:%d:%d", &year, &month, &day, &hour, &minute, &second);
std::tm date_time = {};
date_time.tm_year = year - 1900;
date_time.tm_mon = month - 1;
date_time.tm_mday = day;
date_time.tm_hour = hour;
date_time.tm_min = minute;
date_time.tm_sec = second;
auto timestamp = std::chrono::system_clock::from_time_t(std::mktime(&date_time));
auto duration = timestamp.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
std::cout << millis << "n";
return 0;
}
C++14
#include <iostream>
#include <chrono>
#include <ctime>
int main() {
std::string date_time_str = "2022-07-11_19:03:56.458";
int year, month, day, hour, minute, second;
sscanf_s(date_time_str.c_str(), "%d-%d-%d_%d:%d:%d", &year, &month, &day, &hour, &minute, &second);
std::tm date_time = {};
date_time.tm_year = year - 1900;
date_time.tm_mon = month - 1;
date_time.tm_mday = day;
date_time.tm_hour = hour;
date_time.tm_min = minute;
date_time.tm_sec = second;
auto timestamp = std::chrono::system_clock::from_time_t(std::mktime(&date_time));
auto duration = timestamp.time_since_epoch();
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
std::cout << millis << "n";
return 0;
}
最后
以上就是单薄牛排为你收集整理的C++时间转换成时间戳形式的全部内容,希望文章能够帮你解决C++时间转换成时间戳形式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复