在C++ 11中获取日期和时间为字符串的最先进的方式是什么?C++ 11获取当前日期和时间作为字符串
我知道std::put_time
,但参考文献说我只能在流中使用它。
有std::chrono::system_clock
它提供to_time_t
返回时间为time_t
和缺少日期,不是吗?
我可以使用像bames53:Outputting Date and Time in C++ using std::chrono stringstream但似乎是一种解决方法。
在C++ 11中获取日期和时间为字符串的最先进的方式是什么?C++ 11获取当前日期和时间作为字符串
我知道std::put_time
,但参考文献说我只能在流中使用它。
有std::chrono::system_clock
它提供to_time_t
返回时间为time_t
和缺少日期,不是吗?
我可以使用像bames53:Outputting Date and Time in C++ using std::chrono stringstream但似乎是一种解决方法。
您可以使用下面给出的代码片段,因为它可以满足您的目的。这里使用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;
}
首先,std::time_t
确实抓住日期和时间,因为它通常代表秒内从1月1日,1970年
有在C++ 11处理日期没有大支持。如果你不希望这样做,大部分都是手动的,你仍然需要依赖提升。以下是如何手动完成的。
你可以在一个线程安全的方式—任何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';
}
}
以类似方式的日期。
简单:
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()
返回一个指向内部结构的指针,因此我怀疑这对于多线程代码不太适用。
使用Howard Hinnant's free, open-source header-only datetime library,即可获得当前UTC时间为std::string
中的一行代码:
std::string s = date::format("%F %T", std::chrono::system_clock::now());
我只是跑这一点,该字符串包含:
2017-03-30 17:05:13.400455
这是正确的它甚至可以给你完整的精度。如果你不喜欢那种格式,所有的strftime
格式标志都可用。如果你想要你的当地时间,还有一个timezone library也可用,但它不只是标题。
输出:
2017-03-30 13:05:13.400455 EDT
正在使用您的库线程安全吗? – carlo
是的,date.h是100%线程安全的。它不使用底层线程不安全的C API,也不依赖线程不安全的实践,例如设置环境变量。时区库也是线程安全的,包括初次使用时区库(取决于C++ 11线程安全函数本地静态)的初始化。 –
依赖于用户提供的同步的唯一部分是在第一次使用后重新加载时区数据库(在应用程序运行数月之后可能会用到的)时很少使用的功能。而这个特点是完整的记录:https://howardhinnant.github.io/date/tz.html#database –
见http://en.cppreference.com/w/cpp/chrono/c/strftime。 – Lingxi
就我个人而言,我会远离std :: chrono,但这是你的电话。 abhishek ratore的答案在这方面是非常好的。 – SenselessCoder