是否有一个内置的替代std :: put_time GCC< 5? [英] Is there a builtin alternative to std::put_time for GCC <5?

查看:455
本文介绍了是否有一个内置的替代std :: put_time GCC< 5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

暂时停留在GCC4.8上。我想打印出当前时间,而不是秒。如果 put_time 工作,我的代码将是简单的,如下:

I'm stuck on GCC4.8 for the time being. I want to print out the current time as something other than seconds. If put_time worked, my code would be something simple, like this:

std::cout << std::setw(24) << std::put_time(c_time, "[%T%z %F] ");

没有 put_time 手动访问 c_time 的元素,并手动进行所有格式化,这将是一个痛苦的a **,是我真的很想避免,如果可能的话。请注意,这 不是 意味着我从来不想以任何方式与C进行交互,即使是间接的 - 我只是希望尽可能避免在C中直接编码。

Without put_time, I'd have to access the elements of c_time manually and do all the formatting manually, which would be a pain in the a** and is something I'm much rather avoid if possible. Note that this does not mean that I never want to interact with C in any way, even indirectly -- I'd just like to avoid coding in C directly if possible.

但是,除了 std :: put_time > strftime ,我想避免,因为它需要几乎双倍的代码,是一个很难阅读,至少对我来说。此外,这是C ++,而不是C,所以我想尽可能避开C函数。

However, I can't find any alternatives to std::put_time, aside from strftime, which I'd like to avoid because it requires almost double the lines of code and is a lot harder to read, at least for me. Also, this is C++, not C, so I'd like to steer clear of C functions whenever possible.

我错过了什么?是否有一个内置的替代 std :: put_time 在GCC 4.8下工作?

Am I missing something? Is there a builtin alternative to std::put_time which works under GCC 4.8?

请注意,必须以完全相同的方式工作 - 如果它,直接打印到输出,而不是一个流操纵器,这将是完全精细,作为一个函数返回一个 std :: string

Note that it doesn't have to work in exactly the same way -- if it, say, printed it directly to the output, rather than being a stream manipulator, that would be perfectly fine too, as would a function which returned a std::string containing the formatted time.

我已经做了一个Googling的小部分,发现 < chrono> ,但这不工作,因为它不有什么要自动格式化时间。我仍然必须手动进行,我确定这将是更多工作,因为我必须解析从纪元以来的秒数到一年,月,日,等。

I've done a good bit of Googling and found <chrono>, but that doesn't work because it doesn't have anything to format the time automatically. I'd still have to do it manually, and I'm pretty sure it would be more work, since I'd have to parse the number of seconds since the epoch into a year, month, day, etc.

推荐答案

除了 put_time en.cppreference.com/w/cpp/header/chronorel =nofollow> chrono iomanip 库。

There are no functions other than put_time for the outputing of time provided in the chrono or the iomanip library.

ctime 库确实提供: strftime ctime asctime

The ctime library does provide: strftime, ctime, and asctime.

由于 http://stackoverflow.com 不允许提出有关寻找3个 rd 派对图书馆的问题,我想你只是要求某人指导你上使用 strftime std :: put_time(c_time,[%T%z%F])可以写成以下格式:

Since http://stackoverflow.com does not permit questions about finding 3rd party libraries, I'm going to guess that you're just asking for someone to direct you on the use of strftime? std::put_time(c_time, "[%T%z %F] ") could be written in the format:

char foo[24];

if(0 < strftime(foo, sizeof(foo), "[%T%z %F] ", c_time)) cout << foo << endl;

这篇关于是否有一个内置的替代std :: put_time GCC&lt; 5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆