获取c++中自epoch以来的天数(跨平台)
Get number of days since epoch in C++ (cross platform)
如何在c++中获得自epoch以来的天数计数,我知道我应该使用mktime
函数,但我不明白如何实现它
谢谢!
修改cplusplus.com的一些示例代码:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t seconds;
seconds = time (NULL);
int daysSinceEpoch = seconds/(60*60*24);
printf ("%ld days since January 1, 1970", daysSinceEpoch);
return 0;
}
日期不容易正确处理。目前的标准库不提供正确执行此操作的能力。您应该使用合适的日期库,例如boost::date或Howard Hinnant的<date>
。
date epoch = year(1970)/jan/day(1); // Assuming you're referring to the traditional Unix epoch (some systems such as Cocoa on OS X use the first day of the millenium, Jan 1, 2001 as their epoch)
days d = date::today() - epoch;
通过time(NULL)
获取当前时间。将该值传递给gmtime
,它将返回一个tm*
。阅读tm
的文档
相关文章:
- 通过网络、跨平台传递std::变体是否安全
- 如何跨平台将二进制资源构建到程序中?
- 以跨平台的方式将图像源路径从C++发送到 QML
- 跨平台 GPU 计算
- 在哪里存储跨平台C++应用存储?
- 如何将外部依赖引入跨平台CMake项目?
- 将 VS Code 用于跨平台可缩放C++项目
- 创建跨平台 C++ 触摸管理器.在 c++ 中传递 Objective-c 对象涉及代码
- 使用通配符的跨平台文件列表
- 如何使用跨平台代码在C++中设置系统时间?
- 跨平台 c++ 和 Visual Studio 2017?
- 跨平台随机重现性
- c++中的交叉编译(作为跨平台应用程序的一部分或作为服务)
- 使用Visual Studio进行跨平台C++开发
- Visual Studio跨平台生成文件项目,找不到命令
- 如何制作跨平台图书馆
- 在VS2019中包含nuget包C++跨平台程序
- 跨平台 mySQL 与字符集不一致
- 只有一个可执行文件即可进行跨平台 SIMD 调用?
- 使特定于Unix的CMake过程跨平台