跨平台的方式来获得一天的时间? [英] Cross Platform way to get the time of day?

查看:274
本文介绍了跨平台的方式来获得一天的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来获取时间的时间(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.

这篇关于跨平台的方式来获得一天的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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