【问题标题】:Calculate the daily midnight time from current timestamp从当前时间戳计算每日午夜时间
【发布时间】:2021-05-16 17:17:15
【问题描述】:

假设当前时间是 2021 年 5 月 16 日 22:42,那么午夜时间是 2021 年 5 月 17 日 12 点 00:00:00,此时时间会发生变化。

我想计算从当前时间到午夜剩下的时间。

【问题讨论】:

  • 我对poco库不熟悉,它包含时间能力吗?快速浏览一下文档让我倾向于否定,但我可能很容易忽略了一些东西。
  • 你可以使用c++标准和std::chrono
  • 那么为什么要标记 poco 库?
  • 我们在开发中主要使用 poco 及其 c++ 包装器,但时间戳等可以很容易地计算出来。

标签: c++ c++17 poco-libraries


【解决方案1】:

你可以这样做:

#include <iostream>
#include <ctime>
#include <sstream>

int main() {
    struct tm tm;
    std::string s("2013-12-04 15:03");
    strptime(s.c_str(), "%Y-%m-%d %H:%M", &tm);
    struct tm tm1 = tm;
    tm1.tm_mday = tm1.tm_mday +1;
    tm1.tm_hour = 0;
    tm1.tm_min = 0;
    double secs = std::difftime(mktime(&tm1), mktime(&tm));
    std::cout<<secs<<" seconds\n";
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多