问题描述
有一个简单的方法来获取时间的时间(17:30,01:20 ...等),将工作在iOS,OSX,Linux和Windows?
Is there a simple way to get time time of day (17:30, 01:20...etc) that would work on iOS, OSX, Linux and Windows?
如果没有,是否有Windows方式和posix方式或某物?
If not is there a Windows way and a posix way or something?
感谢
推荐答案
您可以使用 time_t now = time(NULL); 或时间(& now)检索时间;
然后你通常转换为本地时间 struct tm * tm_now = localtime(& now); 。 struct tm 包含年,月,日,星期,小时,分钟和秒的字段。如果你想产生可打印输出, strftime 支持直接。
You then usually convert to local time with struct tm *tm_now = localtime(&now);. A struct tm contains fields for the year, month, day, day of week, hour, minute, and second. If you want to produce printable output, strftime supports that directly.
这两个都在C和C +
Both of these are in the C and C++ standards, so they are available on most normal platforms.
如果你真的只关心C ++,你可以使用 std :: put_time ,类似于 strftime ,但对于将输出写入流的典型情况,使用起来稍微简单一些。
If you really only care about C++, you can use std::put_time, which is similar to strftime, but a little bit simpler to use for the typical case of writing the output to a stream.
这篇关于跨平台的方式来获得一天的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!