C++ 如何获取当地时间c++;
我需要获取当前时间(小时、分钟和秒)并添加30分钟。最后,我需要将结果保存到一个整数变量 怎么可能呢 我现在的代码是:C++ 如何获取当地时间c++;,c++,C++,我需要获取当前时间(小时、分钟和秒)并添加30分钟。最后,我需要将结果保存到一个整数变量 怎么可能呢 我现在的代码是: int main() { time_t currentTime; struct tm *localTime; time( ¤tTime ); localTime = localtime( ¤tTime ); int Hour = localTime->tm_hour; int Min = lo
int main()
{
time_t currentTime;
struct tm *localTime;
time( ¤tTime );
localTime = localtime( ¤tTime );
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
std::cout << "Execute at: " << Hour << ":" << Min << ":" << Sec << std::endl;
return 0;
}
intmain()
{
时间-当前时间;
struct tm*本地时间;
时间(¤tTime);
localTime=localTime(¤tTime);
整小时=本地时间->整小时;
int Min=localTime->tm\u Min;
int Sec=localTime->tm_Sec;
std::cout您可以执行以下操作以比当地时间提前30分钟获得时间:
time_t currentTime;
struct tm *localTime;
time( ¤tTime );
currentTime += 1*30*60;
localTime= localtime(&utc);
localTime结构现在比当前本地时间提前30分钟。您可以像以前一样以整数形式获取值
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
如果您需要从中提取字符串,您可以这样做
std::string strAheadTime = asctime(localTime);
你可以像这样达到同样的效果-
#include <iostream>
#include <ctime>
int main()
{
time_t currentTime;
struct tm *localTime;
time( ¤tTime ); // Get the current time
localTime = localtime( ¤tTime ); // Convert the current time to the local time
int Hour = localTime->tm_hour;
int Min = localTime->tm_min;
int Sec = localTime->tm_sec;
std::cout << "The current time is : " << Hour << ":" << Min << ":" << Sec << std::endl;
//increase local time by 30 minutes and adjust minutes and hour.
int IncMin = Min + 30;
if(IncMin >= 60){
int res = IncMin/60;
int newMin = IncMin % 60;
Hour += res;
if(Hour > 23){
Hour = 00;
}
std::cout << "The updated time is : " << Hour << ":" << newMin << ":" << Sec << std::endl;
}
return 0;
}
#包括
#包括
int main()
{
时间-当前时间;
struct tm*本地时间;
时间(¤tTime);//获取当前时间
localTime=localTime(¤tTime);//将当前时间转换为本地时间
整小时=本地时间->整小时;
int Min=localTime->tm\u Min;
int Sec=localTime->tm_Sec;
std::您的预期输出是否正确?您的意思是将时间增加30分钟并打印输出吗?我的预期输出是当前本地数据时间(小时:分钟:ss)加上30分钟,例如当前时间:13:16:50我想要的是:13:46:50谢谢。无法将最终结果转换为整数?我的预期输出是当前本地数据时间(hour:min:ss)加上30分钟,例如当前时间:13:16:50我想要的:13:46:50你想要一个整数格式的hh:mm:ss吗,这就是你想要说的吗?你可以用int来表示从历元开始的秒数,但不能用你想要的格式表示时间。\n你可以用hh:mm:ss来表示字符串。好的,我会解释我的所有目标:)我正在做一个API,它生成一个随机的sessionId,过期日期为30分钟此时我需要增加30分钟,即14:18:00,并创建sessionId。但在其他函数中,我收到sessionId并需要检查过期日期,因此我需要比较当前日期(例如:14:30:00)和sessionId过期日期。您明白我需要什么吗?