gpt4 book ai didi

c++ - C++获取本地时间的方法

转载 作者:行者123 更新时间:2023-11-28 05:11:00 25 4
gpt4 key购买 nike

我需要获取当前时间(小时、分钟和秒)并加上 30 分钟。最后,我需要将结果保存到一个整数变量中。

这怎么可能?

我目前的代码:

int main()
{

time_t currentTime;
struct tm *localTime;

time( &currentTime );
localTime = localtime( &currentTime );

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;

}

谢谢

最佳答案

您可以执行以下操作以获得比本地时间早 30 分钟的时间:

    time_t currentTime;
struct tm *localTime;
time( &currentTime );
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);

关于c++ - C++获取本地时间的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43493794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com