C++11 获取当前日期和时间作为字符串

C++11 get current date and time as string

本文关键字:字符串 时间 获取 当前日期 C++11      更新时间:2023-10-16

c++11 中以字符串形式获取日期和时间的最新方法是什么?

我知道std::put_time,但参考资料说我只会在流中使用它。

std::chrono::system_clock提供to_time_t返回时间作为time_t而缺少日期,不是吗?

我可以使用像 bames53 这样的字符串流:使用 std::chrono 在C++中输出日期和时间,但这似乎是一种解决方法。

使用 Howard Hinnant 的免费开源标头日期时间库,您可以在一行代码中获取当前 UTC 时间作为std::string

std::string s = date::format("%F %T", std::chrono::system_clock::now());

我刚刚运行了这个,字符串包含:

2017-03-30 17:05:13.400455

没错,它甚至可以为您提供完全的精度。 如果您不喜欢这种格式,可以使用所有strftime格式标志。 如果您想要本地时间,还有一个时区库可用,尽管它不仅仅是标题。

std::string s = date::format("%F %T %Z", date::make_zoned(date::current_zone(),
                                         std::chrono::system_clock::now()));

输出:

2017-03-30 13:05:13.400455 EDT

针对 C++20 的更新

现在可以使用 std::lib 标头<chrono><format>来完成此操作:

#include <chrono>
#include <format>
#include <string>
int
main()
{
    std::string s1 = std::format("{:%F %T}", std::chrono::system_clock::now());
    std::string s2 = std::format("{:%F %T %Z}",
        std::chrono::zoned_time{std::chrono::current_zone(),
                                std::chrono::system_clock::now()});
}

演示。

更简单:

string CurrentDate()
{
    std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    char buf[100] = {0};
    std::strftime(buf, sizeof(buf), "%Y-%m-%d", std::localtime(&now));
    return buf;
}

也根据时间调整格式。

请注意,我怀疑这不适用于多线程代码,因为std::localtime()返回指向内部结构的指针。

首先,std::time_t确实捕获了日期和时间,因为它通常表示从 1970 年 1 月 1 日开始的秒数。

处理 C++11 中的日期没有很好的支持。如果您不想这样做,您仍然必须依赖 boost,主要是手动的。以下是手动操作的方法。

您可以以线程安全的方式将其与任何std::chrono::*clock一起使用,例如 std::system_clock ,如下所示:

std::string get_date_string(std::chrono::time_point t) {
  auto as_time_t = std::chrono::system_clock::to_time_t(t);
  struct tm tm;
  if (::gmtime_r(&as_time_t, &tm))
    if (std::strftime(some_buffer, sizeof(some_buffer), "%F", &tm))
      return std::string{some_buffer};
  throw std::runtime_error("Failed to get current date as string");
}

在其他地方,您可以发布:

get_date_string(std::system_clock::now());

这个解决方案的相对好处是,在API级别,您仍在使用现代,可移植的C++概念,例如std::chrono::time_point,当然还有std::string

这样的时间戳;

#include <iostream>
#include <chrono>
#include <ctime>
int main() {
    std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
    std::time_t start_time = std::chrono::system_clock::to_time_t(now);
    char timedisplay[100];
    struct tm buf;
    errno_t err = localtime_s(&buf, &start_time);
    if (std::strftime(timedisplay, sizeof(timedisplay), "%H:%M:%S", &buf)) {
        std::cout << timedisplay << 'n';
    }
}

以类似的方式约会。

您可以使用下面给出的代码片段,因为它将满足您的目的。这里使用 time.h 头文件作为所需的 localtime() 函数,然后使用带有所需参数的 strftime() 函数将给出输出并将其作为字符串返回。

#include <iostream>
#include <string>
#include <time.h>
std::string current_date();
std::string current_time();
int main(){
    std::cout<<"Current date => "<<current_date()<<"n";
    std::cout<<"Current time => "<<current_time()<<"n";
}
std::string current_date(){
    time_t now = time(NULL);
    struct tm tstruct;
    char buf[40];
    tstruct = *localtime(&now);
    //format: day DD-MM-YYYY
    strftime(buf, sizeof(buf), "%A %d/%m/%Y", &tstruct);
    return buf;
}
std::string current_time(){
    time_t now = time(NULL);
    struct tm tstruct;
    char buf[40];
    tstruct = *localtime(&now);
    //format: HH:mm:ss
    strftime(buf, sizeof(buf), "%X", &tstruct);
    return buf;
}