1. 对时间的增加(小时)
#include <iostream>
#include <chrono>
int main() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
// 增加一小时
std::chrono::hours oneHour{1};
std::chrono::system_clock::time_point future = now + oneHour;
// 减去一天
std::chrono::hours oneDay{24};
std::chrono::system_clock::time_point past = now - oneDay;
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
std::time_t future_time = std::chrono::system_clock::to_time_t(future);
std::time_t past_time = std::chrono::system_clock::to_time_t(past);
std::cout << "当前时间:" << std::ctime(&now_time);
std::cout << "加一小时后的时间:" << std::ctime(&future_time);
std::cout << "减一天后的时间:" << std::ctime(&past_time);
return 0;
}
输出结果:
当前时间:Tue Jul 11 19:48:45 2023
加一小时后的时间:Tue Jul 11 20:48:45 2023
减一天后的时间:Mon Jul 10 19:48:45 2023
字符串转标准时间 然后对时间进行加减,然后转回字符串
#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
// 将字符串解析为时间点
std::chrono::system_clock::time_point parseTime(const std::string& timeStr) {
std::tm tm = {};
std::istringstream ss(timeStr);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
std::time_t tt = std::mktime(&tm);
return std::chrono::system_clock::from_time_t(tt);
}
// 将时间点转换为字符串
std::string formatTime(const std::chrono::system_clock::time_point& timePoint) {
std::time_t tt = std::chrono::system_clock::to_time_t(timePoint);
// std::tm* tm = std::gmtime(&tt); // 得到世界时间
std::tm* tm = std::localtime(&time); // 得到当地时间
std::ostringstream oss;
oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
return oss.str();
}
int main() {
std::string timeStr = "2023-07-10 15:30:00";
std::chrono::system_clock::time_point timePoint = parseTime(timeStr);
// 加上一小时
std::chrono::hours oneHour{1};
timePoint += oneHour;
// 减去一天
std::chrono::hours oneDay{24};
timePoint -= oneDay;
std::string resultStr = formatTime(timePoint);
std::cout << "加上一小时并减去一天后的时间:" << resultStr << std::endl;
return 0;
}
转为一个函数
std::string turn(std::string time(1999-08-07 12:34:00))
std::tm tm = {};
std::istringstream ss(time);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
std::time_t tt = std::mktime(&tm);
std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::from_time_t(tt);
std::chrono::hours oneHour(more);
timePoint += oneHour;
std::time_t mm = std::chrono::system_clock::to_time_t(timePoint);
//std::tm* tm2 = std::gmtime(&mm);
std::tm* tm2 = std::localtime(&mm);
std::ostringstream oss;
oss << std::put_time(tm2, "%Y-%m-%d %H:%M:%S");
std::string result = oss.str();
要将std::chrono::system_clock::time_point
类型转换为字符串,你可以使用std::put_time
函数或其他字符串格式化方法,例如使用std::strftime
函数。
下面是使用std::put_time
函数将std::chrono::system_clock::time_point
类型转换为字符串的示例代码:
#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>
int main() {
std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
std::tm* tm = std::localtime(&time);
std::stringstream ss;
ss << std::put_time(tm, "%Y-%m-%d %H:%M:%S");
std::string str = ss.str();
std::cout << "转换后的时间字符串为:" << str << std::endl;
return 0;
}
在这个示例中,首先获取当前时间的std::chrono::system_clock::time_point
类型的时间点timePoint
。然后,使用std::chrono::system_clock::to_time_t
函数将时间点转换为std::time_t
类型的时间。接下来,使用std::localtime
函数将std::time_t
类型的时间转换为std::tm*
结构体指针,以便获取日期和时间的各个组成部分。
最后,使用std::put_time
函数和std::stringstream
来将时间转换为指定格式的字符串。std::put_time
函数接受std::tm*
结构体指针和格式字符串作为参数,可以将时间转换为指定格式的字符串。将格式化后的字符串存储在std::string
类型的变量str
中。
输出结果将是格式为"YYYY-MM-DD HH:MM:SS"的时间字符串。
请注意,这个示例中使用了本地时间(localtime)来格式化时间。你也可以使用其他时间标准或自定义格式来进行格式化。另外,确保包含了相关的头文件 <iomanip>
, <chrono>
, <ctime>
和 <sstream>
。
2. 获取当前时间(标准时间格式2023 03:14 12:45)
// 获取当前时间
#include <chrono>
#include <iomanip>
#include <sstream>
#include <filesystem>
std::string now_time()
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t tt = std::chrono::system_clock::to_time_t(now);
std::tm* ptm = std::localtime(&tt);
std::ostringstream oss;
oss << std::put_time(ptm, "%Y-%m-%d %H:%M:%S");
std::string result = oss.str();
return result;
}
int main()
{
std::string time_now;
time_now = now_time();
std::cout << time_now << std::endl;
}
结果:2023-07-18 16:45:58
3. 获取当前时间,对当前时间取整
// 获取当前整点的时间
#include <chrono>
#include <iomanip>
#include <sstream>
#include <filesystem>
std::string now_time()
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t tt = std::chrono::system_clock::to_time_t(now);
std::tm* ptm = std::localtime(&tt);
ptm->tm_min = 0; // 去掉分钟 -------------------------
ptm->tm_sec = 0; // 去掉秒----------------------------
std::ostringstream oss;
oss << std::put_time(ptm, "%Y-%m-%d %H:%M:%S");
std::string result = oss.str();
return result;
}
int main()
{
std::string time_now;
time_now = now_time();
std::cout << time_now << std::endl;
}
结果:2023-07-18 16:00:00
3. 时间相减
方法一文章来源:https://www.toymoban.com/news/detail-561313.html
// 时间相减
#include <chrono>
#include <iomanip>
#include <sstream>
#include <filesystem>
int now_subtraction_time(std::string time_1)
{
cout << "time1:" << time_1 << endl;
std::tm tm = {};
std::istringstream ss(time_1);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
std::time_t tt = std::mktime(&tm);
std::tm* ptm = std::localtime(&tt);
int a = ptm->tm_hour;
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t tt2 = std::chrono::system_clock::to_time_t(now);
std::tm* ptm2 = std::localtime(&tt2);
std::ostringstream oss;
oss << std::put_time(ptm2, "%Y-%m-%d %H:%M:%S");
std::string result = oss.str();
cout << "new:" << result << endl;
int b = ptm2->tm_hour;
int sub = b - a;
return sub;
}
int main()
{
std::string time1 = "2023-07-19 15:00:00";
int a1 = now_subtraction_time(time1);
std::cout << a1 << std::endl;
std::string time12 = "2023-07-19 14:00:00";
int a12 = now_subtraction_time(time12);
std::cout << a12 << std::endl;
std::string time13 = "2023-07-19 13:00:00";
int a13 = now_subtraction_time(time13);
std::cout << a13 << std::endl;
}
结果
time1:2023-07-19 15:00:00
new:2023-07-19 17:38:59
2
time1:2023-07-19 14:00:00
new:2023-07-19 17:38:59
3
time1:2023-07-19 13:00:00
new:2023-07-19 17:38:59
4
方法二文章来源地址https://www.toymoban.com/news/detail-561313.html
// 时间相减
// 时间相减
#include <chrono>
#include <iomanip>
#include <sstream>
#include <filesystem>
double now_subtraction_time(std::string time_1)
{
cout << "time1:" << time_1 << endl;
std::tm tm = {};
std::istringstream ss(time_1);
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
std::time_t tt = std::mktime(&tm);
std::tm* ptm = std::localtime(&tt);
int a = ptm->tm_hour;
// 获取现在时间
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t tt2 = std::chrono::system_clock::to_time_t(now);
double dTime = difftime(tt2, tt); //返回值单位为秒
double iHour = dTime / 60 / 60; //计算小时
//double iMin = (dTime - iHour * 60 * 60) / 60; //小时的余数再除分钟的分钟数
return iHour;
}
int main()
{
std::string time1 = "2023-07-21 10:00:00";
double a1 = now_subtraction_time(time1);
std::cout << a1 << std::endl;
std::string time12 = "2023-07-21 11:00:00";
double a12 = now_subtraction_time(time12);
std::cout << a12 << std::endl;
std::string time13 = "2023-07-21 12:00:00";
double a13 = now_subtraction_time(time13);
std::cout << a13 << std::endl;
}
nowtime: 2023-07-21 14:19:00
time1:2023-07-21 10:00:00
4.31417
time1:2023-07-21 11:00:00
3.31417
time1:2023-07-21 12:00:00
2.31417
到了这里,关于c++对时间的操作(chrono库)——字符创转时间,时间加减的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!