【问题标题】:Adding and subtracting time with tm用 tm 加减时间
【发布时间】:2019-06-17 21:56:33
【问题描述】:

假设我将 tm 中的时间设置为 23:00:00

ptm->tm_hour = 23; ptm->tm_min = 0; ptm->tm_sec = 0;

我想让用户从中减去时间

ptm->tm_hour -= hourinput; ptm->tm_min -= minuteinput; ptm->tm_sec -= secondinput;

如果用户减去 0 小时 5 分 5 秒,而不是显示为 22:54:55,它将显示为 23:-5:-5。

我想我可以做一堆 if 语句来检查 ptm 是否低于 0 并解决这个问题,但是有没有更有效的方法来获得正确的时间?

【问题讨论】:

  • 是的,你需要“做一堆 if 语句来检查 ptm 是否低于 0 并解决这个问题”。
  • 不要以这种方式操纵时间,始终根据纪元时间进行操作,这意味着您可以调整自 1970 年以来的毫秒数并使用标准函数调用转换回 HMS,让生活变得更加轻松。

标签: c++ time time-t


【解决方案1】:

是的,您可以为此使用std::mktime。它不仅将std::tm 转换为std::time_t,它还修复了tm 如果某些字段超出范围。考虑这个例子,我们取当前时间并加上 1000 秒。

#include <iostream>
#include <iomanip> // put_time
#include <ctime>

int main(int argc, char **argv) {
    std::time_t t = std::time(nullptr);
    std::tm tm = *std::localtime(&t);
    std::cout << "Time: " << std::put_time(&tm, "%c %Z") << std::endl;
    tm.tm_sec += 1000; // the seconds are now out of range
    //std::cout << "Time in 1000 sec" << std::put_time(&tm, "%c %Z") << std::endl; this would crash!
    std::mktime(&tm); // also returns a time_t, but we don't need that here
    std::cout << "Time in 1000 sec: " << std::put_time(&tm, "%c %Z") << std::endl;
    return 0;
}

我的输出:

时间:2019 年 1 月 24 日 09:26:46 W. 欧洲标准时间

1000 秒内的时间:2019 年 1 月 24 日 09:43:26 W. 欧洲标准时间

如您所见,时间从09:26:46 变为09:43:26

【讨论】:

    【解决方案2】:

    这是使用 Howard Hinnant's date library 的另一种方法,它正在进入 C++2a。

    #include <iostream>
    #include "date/date.h"
    
    using namespace std::chrono_literals;
    
    // Time point representing the start of today:
    auto today = date::floor<date::days>(std::chrono::system_clock::now());
    
    auto someTp = today + 23h; // Today, at 23h00
    auto anotherTp = someTp - 5min - 5s; // ... should be self-explanatory now :)
    
    std::cout << date::format("%b-%d-%Y %T\n", anotherTp);
    

    如果您想通过用户界面公开时间点的操作,编译时构造23h5min 等当然不可用。这些文字构造了std::chrono::duration 对象,因此您需要一种机制将用户输入转换为等效实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      相关资源
      最近更新 更多