c++ - C++ 如何获取本地时间

标签 c++

我需要获取当前时间(小时、分钟和秒)并添加 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/

相关文章:

c++ - 模板类的 Visual Studio C++ 链接器问题

c++ - 为什么我可以在 C++ 中从外部访问私有(private)方法?

python - 百分点函数的 T 分布公式

c++ - 如何将 wxString 转换为 Int 以使用算术运算?

c++ - 前/后增量的左值和右值

c++ - 如何让C++随机按键

c++ - 为什么 Boost Format 和 printf 在相同的格式字符串上表现不同

javascript - 获取浏览器和选项卡的焦点

C++ - 需要左值作为赋值的左操作数

c++ - 使用 ncurses 时未定义对 `stdscr' 的引用